Skip to content

Commit

Permalink
Merge pull request #92 from danielebarbaro/fix-contrast-tests
Browse files Browse the repository at this point in the history
Test Enhancements for Contrast Calculations (and mini refactor for contrast class)
  • Loading branch information
freekmurze authored Dec 23, 2024
2 parents dde546a + fe44719 commit e0cdcdb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
37 changes: 17 additions & 20 deletions src/Contrast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions tests/ContrastTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Spatie\Color\Hex;
use function PHPUnit\Framework\assertSame;

use Spatie\Color\Color;
Expand All @@ -12,3 +13,17 @@
it('can calculate contrast from another format', function (Color $a, Color $b, float $contrast) {
assertSame($contrast, Contrast::ratio($a->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);
});
6 changes: 3 additions & 3 deletions tests/Datasets/Colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit e0cdcdb

Please sign in to comment.