Skip to content

Commit

Permalink
Contrast and Luminance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielebarbaro committed Dec 23, 2024
1 parent 3ba39e7 commit fe44719
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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 fe44719

Please sign in to comment.