Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Feature: Luminance Scale #44

Closed
wants to merge 14 commits into from
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php" : "^7.3|^8.0"
"php": "^7.3|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5||^9.0"
Expand All @@ -38,5 +38,5 @@
},
"config": {
"sort-packages": true
}
}
}
19 changes: 19 additions & 0 deletions src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function green();

public function blue();

public function luminance(): float;

public function contrastRatio(): float;

public function toHex(): Hex;

public function toHsl(): Hsl;
Expand All @@ -22,5 +26,20 @@ public function toRgb(): Rgb;

public function toRgba(float $alpha = 1): Rgba;

public function toLuminanceScale(
array $scale = [
50 => 93.0,
100 => 86.0,
200 => 74.0,
300 => 59.0,
400 => 39.0,
500 => 24.0,
600 => 15.0,
700 => 11.5,
800 => 7.0,
900 => 3.0,
]
): array;

public function __toString(): string;
}
55 changes: 55 additions & 0 deletions src/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,59 @@ public static function rgbValueToHsl($red, $green, $blue): array

return [$hue, min($saturation, 1) * 100, min($lightness, 1) * 100];
}

public static function rgbValueToLuminance($red, $green, $blue): float
{
$red /= 255;
$green /= 255;
$blue /= 255;

$red = $red < 0.03928 ? $red / 12.92 : pow(($red + 0.055) / 1.055, 2.4);
$green = $green < 0.03928 ? $green / 12.92 : pow(($green + 0.055) / 1.055, 2.4);
$blue = $blue < 0.03928 ? $blue / 12.92 : pow(($blue + 0.055) / 1.055, 2.4);

return 21.26 * $red + 71.52 * $green + 7.22 * $blue;
}

public static function hslValueToLuminance(
float $hue,
float $saturation,
float $lightness
): float {
[$red, $green, $blue] = self::hslValueToRgb($hue, $saturation, $lightness);

return self::rgbValueToLuminance($red, $green, $blue);
}

public static function hslValueFromLuminance(
float $hue,
float $saturation,
float $luminance,
float $precision = 0.01
): array {
$closest = 100;
$lightness = 100;

for ($sampleLightness = 100; $sampleLightness >= 0; $sampleLightness--) {
$sampleLuminance = self::hslValueToLuminance($hue, $saturation, $sampleLightness);
$difference = abs($luminance - $sampleLuminance);
if ($difference < $closest) {
$lightness = $sampleLightness;
$closest = $difference;
}
}

$max = $closest + $precision * 100;
$min = $closest - $precision * 100;
for ($sampleLightness = $max; $sampleLightness >= $min; $sampleLightness -= $precision) {
$sampleLuminance = self::hslValueToLuminance($hue, $saturation, $sampleLightness);
$difference = abs($luminance - $sampleLuminance);
if ($difference < $closest) {
$lightness = $sampleLightness;
$closest = $difference;
}
}

return [$hue, $saturation, $lightness];
}
}
51 changes: 51 additions & 0 deletions src/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ public function blue(): string
return $this->blue;
}

public function luminance(): float
{
$rgb = $this->toRgb();
return Convert::rgbValueToLuminance($rgb->red(), $rgb->green(), $rgb->blue());
}

public function contrastRatio(): float
{
$luminance = $this->luminance() / 100;
$black = new Rgb(0, 0, 0);
$blackLuminance = $black->luminance() / 100;

$contrastRatio = 0;
if ($luminance > $blackLuminance) {
$contrastRatio = (int) (($luminance + 0.05) / ($blackLuminance + 0.05));
} else {
$contrastRatio = (int) (($blackLuminance + 0.05) / ($luminance + 0.05));
}

return $contrastRatio;
}

public function toHex(): self
{
return new self($this->red, $this->green, $this->blue);
Expand Down Expand Up @@ -85,6 +107,35 @@ public function toRgba(float $alpha = 1): Rgba
return $this->toRgb()->toRgba($alpha);
}

public function toLuminanceScale(
array $scale = [
50 => 93.0,
100 => 86.0,
200 => 74.0,
300 => 59.0,
400 => 39.0,
500 => 24.0,
600 => 15.0,
700 => 11.5,
800 => 7.0,
900 => 3.0,
]
): array {
$palette = [];
$hsl = $this->toHsl();
foreach ($scale as $key => $luminance) {
[$hue, $saturation, $lightness] = Convert::hslValueFromLuminance(
$hsl->hue(),
$hsl->saturation(),
$luminance
);
$newHsl = new Hsl($hue, $saturation, $lightness);
$palette[$key] = $newHsl->toHex();
}

return $palette;
}

public function __toString(): string
{
return "#{$this->red}{$this->green}{$this->blue}";
Expand Down
49 changes: 49 additions & 0 deletions src/Hsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ public function blue(): int
return Convert::hslValueToRgb($this->hue, $this->saturation, $this->lightness)[2];
}

public function luminance(): float
{
$rgb = $this->toRgb();
return Convert::rgbValueToLuminance($rgb->red(), $rgb->green(), $rgb->blue());
}

public function contrastRatio(): float
{
$luminance = $this->luminance() / 100;
$black = new Rgb(0, 0, 0);
$blackLuminance = $black->luminance() / 100;

$contrastRatio = 0;
if ($luminance > $blackLuminance) {
$contrastRatio = (int) (($luminance + 0.05) / ($blackLuminance + 0.05));
} else {
$contrastRatio = (int) (($blackLuminance + 0.05) / ($luminance + 0.05));
}

return $contrastRatio;
}

public function toHex(): Hex
{
return new Hex(
Expand Down Expand Up @@ -88,6 +110,33 @@ public function toRgba(float $alpha = 1): Rgba
return new Rgba($this->red(), $this->green(), $this->blue(), $alpha);
}

public function toLuminanceScale(
array $scale = [
50 => 93.0,
100 => 86.0,
200 => 74.0,
300 => 59.0,
400 => 39.0,
500 => 24.0,
600 => 15.0,
700 => 11.5,
800 => 7.0,
900 => 3.0,
]
): array {
$palette = [];
foreach ($scale as $key => $luminance) {
[$hue, $saturation, $lightness] = Convert::hslValueFromLuminance(
$hsl->hue(),
$hsl->saturation(),
$luminance
);
$palette[$key] = new self($hue, $saturation, $lightness);
}

return $palette;
}

public function __toString(): string
{
$hue = round($this->hue);
Expand Down
50 changes: 50 additions & 0 deletions src/Hsla.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ public function alpha(): float
return $this->alpha;
}

public function luminance(): float
{
$rgb = $this->toRgb();
return Convert::rgbValueToLuminance($rgb->red(), $rgb->green(), $rgb->blue());
}

public function contrastRatio(): float
{
$luminance = $this->luminance() / 100;
$black = new Rgb(0, 0, 0);
$blackLuminance = $black->luminance() / 100;

$contrastRatio = 0;
if ($luminance > $blackLuminance) {
$contrastRatio = (int) (($luminance + 0.05) / ($blackLuminance + 0.05));
} else {
$contrastRatio = (int) (($blackLuminance + 0.05) / ($luminance + 0.05));
}

return $contrastRatio;
}

public function toHex(): Hex
{
return new Hex(
Expand Down Expand Up @@ -96,6 +118,34 @@ public function toRgba(float $alpha = 1): Rgba
return new Rgba($this->red(), $this->green(), $this->blue(), $alpha);
}

public function toLuminanceScale(
array $scale = [
50 => 93.0,
100 => 86.0,
200 => 74.0,
300 => 59.0,
400 => 39.0,
500 => 24.0,
600 => 15.0,
700 => 11.5,
800 => 7.0,
900 => 3.0,
]
): array {
$palette = [];
$hsl = $this->toHsl();
foreach ($scale as $key => $luminance) {
[$hue, $saturation, $lightness] = Convert::hslValueFromLuminance(
$hsl->hue(),
$hsl->saturation(),
$luminance
);
$palette[$key] = new Hsla($hue, $saturation, $lightness);
}

return $palette;
}

public function __toString(): string
{
$hue = round($this->hue);
Expand Down
50 changes: 50 additions & 0 deletions src/Rgb.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ public function blue(): int
return $this->blue;
}

public function luminance(): float
{
return Convert::rgbValueToLuminance($this->red, $this->green, $this->blue);
}

public function contrastRatio(): float
{
$luminance = $this->luminance() / 100;
$black = new self(0, 0, 0);
$blackLuminance = $black->luminance() / 100;

$contrastRatio = 0;
if ($luminance > $blackLuminance) {
$contrastRatio = (int) (($luminance + 0.05) / ($blackLuminance + 0.05));
} else {
$contrastRatio = (int) (($blackLuminance + 0.05) / ($luminance + 0.05));
}

return $contrastRatio;
}

public function toHex(): Hex
{
return new Hex(
Expand Down Expand Up @@ -89,6 +110,35 @@ public function toRgba(float $alpha = 1): Rgba
return new Rgba($this->red, $this->green, $this->blue, $alpha);
}

public function toLuminanceScale(
array $scale = [
50 => 93.0,
100 => 86.0,
200 => 74.0,
300 => 59.0,
400 => 39.0,
500 => 24.0,
600 => 15.0,
700 => 11.5,
800 => 7.0,
900 => 3.0,
]
): array {
$palette = [];
$hsl = $this->toHsl();
foreach ($scale as $key => $luminance) {
[$hue, $saturation, $lightness] = Convert::hslValueFromLuminance(
$hsl->hue(),
$hsl->saturation(),
$luminance
);
$newHsl = new Hsl($hue, $saturation, $lightness);
$palette[$key] = $newHsl->toRgb();
}

return $palette;
}

public function __toString(): string
{
return "rgb({$this->red},{$this->green},{$this->blue})";
Expand Down
Loading