From 3ba39e7e1029539789cec094f0e8899a50151f6a Mon Sep 17 00:00:00 2001 From: Daniele Barbaro Date: Mon, 23 Dec 2024 11:42:58 +0100 Subject: [PATCH 1/2] Refactor Contrast class for a better readability --- src/Contrast.php | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/Contrast.php b/src/Contrast.php index a64e44f..8721166 100644 --- a/src/Contrast.php +++ b/src/Contrast.php @@ -6,28 +6,25 @@ class Contrast { public static function ratio(Color $a, Color $b): float { - if (! $a instanceof Hex) { - $a = $a->toHex(); - } + $a = $a instanceof Hex ? $a : $a->toHex(); + $b = $b instanceof Hex ? $b : $b->toHex(); - if (! $b instanceof Hex) { - $b = $b->toHex(); - } + $l1 = self::calculateLuminance($a); + $l2 = self::calculateLuminance($b); - $l1 = - 0.2126 * pow(hexdec($a->red()) / 255, 2.2) + - 0.7152 * pow(hexdec($a->green()) / 255, 2.2) + - 0.0722 * pow(hexdec($a->blue()) / 255, 2.2); - - $l2 = - 0.2126 * pow(hexdec($b->red()) / 255, 2.2) + - 0.7152 * pow(hexdec($b->green()) / 255, 2.2) + - 0.0722 * pow(hexdec($b->blue()) / 255, 2.2); + return round( + ($l1 > $l2) + ? ($l1 + 0.05) / ($l2 + 0.05) + : ($l2 + 0.05) / ($l1 + 0.05), + 2 + ); + } - if ($l1 > $l2) { - return (float) (($l1 + 0.05) / ($l2 + 0.05)); - } else { - return (float) (($l2 + 0.05) / ($l1 + 0.05)); - } + public static function calculateLuminance(Hex $color): float + { + return + 0.2126 * pow(hexdec($color->red()) / 255, 2.2) + + 0.7152 * pow(hexdec($color->green()) / 255, 2.2) + + 0.0722 * pow(hexdec($color->blue()) / 255, 2.2); } } From fe44719aa787d03d0875d82917e9ff3b9ba4e1cd Mon Sep 17 00:00:00 2001 From: Daniele Barbaro Date: Mon, 23 Dec 2024 11:47:49 +0100 Subject: [PATCH 2/2] Contrast and Luminance tests --- tests/ContrastTest.php | 15 +++++++++++++++ tests/Datasets/Colors.php | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/ContrastTest.php b/tests/ContrastTest.php index d8f8453..d361124 100644 --- a/tests/ContrastTest.php +++ b/tests/ContrastTest.php @@ -1,5 +1,6 @@ toRgba(), $b->toHsl())); })->with('contrast_colors'); + +it('calculates the luminance correctly for a white color', function () { + $white = Hex::fromString('#FFFFFF'); + $luminance = Contrast::calculateLuminance($white); + + expect($luminance)->toBe(1.0); +}); + +it('calculates the luminance correctly for a black color', function () { + $black = Hex::fromString('#000000'); + $luminance = Contrast::calculateLuminance($black); + + expect($luminance)->toBe(0.0); +}); diff --git a/tests/Datasets/Colors.php b/tests/Datasets/Colors.php index b1dd759..ae2d1c0 100644 --- a/tests/Datasets/Colors.php +++ b/tests/Datasets/Colors.php @@ -69,9 +69,9 @@ [Hex::fromString('#ffffff'), Hex::fromString('#ffffff'), 1.0], [Hex::fromString('#ffffff'), Hex::fromString('#000000'), 21.0], [Hex::fromString('#000000'), Hex::fromString('#000000'), 1.0], - [Hex::fromString('#faebd7'), Hex::fromString('#8a2be2'), 5.0], - [Hex::fromString('#ff1493'), Hex::fromString('#cd5c5c'), 1.0], - [Hex::fromString('#f0fff0'), Hex::fromString('#191970'), 15.0], + [Hex::fromString('#faebd7'), Hex::fromString('#8a2be2'), 5.16], + [Hex::fromString('#ff1493'), Hex::fromString('#cd5c5c'), 1.08], + [Hex::fromString('#f0fff0'), Hex::fromString('#191970'), 15.05], ]); dataset('hsla_string_and_rgb_values', function () {