Basic colors test add started

Also fixes for various things that come up during test writing

Test phpunit not yet finished (exceptions, etc)

Note: a lot of checks for extreme values are (int) so we do not fail
for small float values
This commit is contained in:
Clemens Schwaighofer
2024-11-13 19:19:35 +09:00
parent 2bd68f32ac
commit 32c192a362
7 changed files with 432 additions and 62 deletions

View File

@@ -0,0 +1,345 @@
<?php
declare(strict_types=1);
namespace tests;
use PHPUnit\Framework\TestCase;
use CoreLibs\Convert\Color;
/**
* Test class for Convert\Color\Color
* @coversDefaultClass \CoreLibs\Convert\Color\Color
* @testdox \CoreLibs\Convert\Color\Color method tests
*/
final class CoreLibsConvertColorTest extends TestCase
{
// 12 precision allowed, RGB and back has a lot of float imprecisions here
private const DELTA = 0.000000000001;
// sRGB base convert test, should round around and come out the same
// use for RGB 0, 0, 0 in 60 steps and then max 255
// for Hxx 0 + 60 until 360 == 0
// other values for this depend
// convert to/from oklab/cielab: just from RGB (or HSL) as pre conversion are all the same
public static function adjustToIntForRgb(array $values): array
{
return array_map(
fn ($v) => round($v),
$values
);
}
public function testSingle()
{
$this->assertTrue(true, 'Single test');
// $rgb = Color\Coordinates\RGB::__constructFromArray([0, 0, 60]);
// print "IN: " . print_r($rgb, true) . "\n";
// $hsl = Color\Color::rgbToHsl($rgb);
// print "to HSL: " . print_r($hsl, true) . "\n";
// $hsb = Color\Color::hslToHsb($hsl);
// print "to HSB: " . print_r($hsb, true) . "\n";
// $hwb = Color\Color::hsbToHwb($hsb);
// print "to HWB: " . print_r($hwb, true) . "\n";
// // and reverse
// $hsb_r = Color\Color::hwbToHsb($hwb);
// print "R to HSB: " . print_r($hsb_r, true) . "\n";
// $hsl_r = Color\Color::hsbToHsl($hsb_r);
// print "R to HSB: " . print_r($hsl_r, true) . "\n";
// $rgb_r = Color\Color::hslToRgb($hsl_r);
// print "R to RGB: " . print_r($rgb_r, true) . "\n";
// $hsl = Color\Coordinates\HSL::__constructFromArray([0, 0, 0]);
// print "IN HSL: " . print_r($hsl, true) . "\n";
// $hsb = Color\Color::hslToHsb($hsl);
// print "to HSB: " . print_r($hsb, true) . "\n";
// $hwb = Color\Color::hsbToHwb($hsb);
// print "to HWB: " . print_r($hwb, true) . "\n";
// // and reverse
// $hsb_r = Color\Color::hwbToHsb($hwb);
// print "R to HSB: " . print_r($hsb_r, true) . "\n";
// $hsl_r = Color\Color::hsbToHsl($hsb_r);
// print "R to HSL: " . print_r($hsl_r, true) . "\n";
// print "--------\n";
// $hsb = Color\Coordinates\HSB::__constructFromArray([0, 20, 0]);
// print "IN HSB: " . print_r($hsb, true) . "\n";
// $hsl = Color\Color::hsbToHsl($hsb);
// print "to HSL: " . print_r($hsl, true) . "\n";
// $hwb = Color\Color::hslToHwb($hsl);
// print "to HWB: " . print_r($hwb, true) . "\n";
// // and reverse
// $hsl_r = Color\Color::hwbToHsl($hwb);
// print "R to HSB: " . print_r($hsb_r, true) . "\n";
// $hsb_r = Color\Color::hslToHsb($hsl_r);
// print "R to HSL: " . print_r($hsb_r, true) . "\n";
// print "--------\n";
// $hwb = Color\Coordinates\HWB::__constructFromArray([0, 20, 100]);
// print "IN: " . print_r($hwb, true) . "\n";
// $hsl = Color\Color::hwbToHsl($hwb);
// print "to HSL: " . print_r($hsl, true) . "\n";
// $hwb_r = Color\Color::hslToHwb($hsl);
// print "HSL to HWB: " . print_r($hwb_r, true) . "\n";
// $hsb = Color\Color::hwbToHsb($hwb);
// print "to HSB: " . print_r($hsb, true) . "\n";
// $hwb_r = Color\Color::hsbToHwb($hsb);
// print "HSL to HWB: " . print_r($hwb_r, true) . "\n";
}
/**
* From/To RGB <-> ... conversion tests
*
* @covers ::rgbToHsb
* @covers ::rgbToHsl
* @covers ::rgbToHwb
* @covers ::hsbToRgb
* @covers ::hslToRgb
* @covers ::hwebToRgb
* @testdox Convert from and to RGB via HSL, HWB, HSB/V
*
* @return void
*/
public function testRgbColorCoordinateConvertToAndBack(): void
{
for ($r = 0; $r <= 300; $r += 60) {
for ($g = 0; $g <= 300; $g += 60) {
for ($b = 0; $b <= 300; $b += 60) {
// for this test we stay in the correct lane
if ($r > 255) {
$r = 255;
}
if ($g > 255) {
$g = 255;
}
if ($b > 255) {
$b = 255;
}
// base is always the same
$color = Color\Coordinates\RGB::__constructFromArray([$r, $g, $b]);
$base = 'rgb';
foreach (['hsb', 'hsl', 'hwb'] as $coord) {
// print "COORD: " . $coord . ", RGB: " . print_r($color->returnAsArray(), true) . "\n";
// rgb to X and back must be same
$target = $base . 'To' . ucfirst($coord);
$source = $coord . 'To' . ucfirst($base);
$converted_color = Color\Color::$target($color);
$color_b = Color\Color::$source($converted_color);
// $converted_color = Color\Color::rgbToHsb($color);
// $rgb_b = Color\Color::hsbToRgb($converted_color);
$this->assertEqualsWithDelta(
$color->returnAsArray(),
$color_b->returnAsArray(),
self::DELTA,
'Convert ' . $base . ' to ' . $coord . ': ' . print_r($color->returnAsArray(), true) . '/'
. print_r($color_b->returnAsArray(), true)
);
}
}
}
}
}
// HSL / HSB / HWB conversion are not reversable if
// HSL: lightness 0 or 100
// HSB: saturation or brightness 0
// HWB: blackness >= 80 and whitness >= 20 or B>=20 & W>=20 or B>=50 & W>=50
/**
* Undocumented function
*
* @covers ::hslToHsb
* @covers ::hsbToHsl
* @covers ::hslToHwb
* @covers ::hwbToHsl
* @testdox Convert from and to HSL via RGB, HWB, HSB/V
*
* @return void
*/
public function testHslColorCoordinateConvertToAndBack(): void
{
for ($H = 0; $H <= 360; $H += 60) {
for ($S = 0; $S <= 100; $S += 20) {
for ($L = 0; $L <= 100; $L += 20) {
// if lightness 0 or 100 then we cannot reverse (B/W)
if (($L == 0 or $L == 100)) {
continue;
}
$color = Color\Coordinates\HSL::__constructFromArray([$H, $S, $L]);
$base = 'hsl';
foreach (['hsb', 'hwb', 'rgb'] as $coord) {
// for rgb hue on S = 0 is irrelevant (B/W)
if ($H > 0 && $coord == 'rgb') {
continue;
}
$target = $base . 'To' . ucfirst($coord);
$source = $coord . 'To' . ucfirst($base);
$converted_color = Color\Color::$target($color);
$color_b = Color\Color::$source($converted_color);
// print "COORD: " . $coord . ", HSL: " . print_r($color->returnAsArray(), true) . "\n";
$this->assertEqualsWithDelta(
$color->returnAsArray(),
$color_b->returnAsArray(),
self::DELTA,
'Convert HSL to ' . $coord . ': ' . print_r($color->returnAsArray(), true) . '/'
. print_r($color_b->returnAsArray(), true)
);
}
}
}
}
}
/**
* Undocumented function
*
* @covers ::hsbToHsl
* @covers ::hslToHsb
* @covers ::hsbToHwb
* @covers ::hwbToHsb
* @testdox Convert from and to HSB via RGB, HWB, HSL
*
* @return void
*/
public function testHsbColorCoordinateConvertToAndBack(): void
{
for ($H = 0; $H <= 360; $H += 60) {
for ($S = 0; $S <= 100; $S += 20) {
for ($B = 0; $B <= 100; $B += 20) {
// if sat or brightness is 0 then we cannot reverse correctly (B/W)
if ($S == 0 or $B == 0) {
continue;
}
$color = Color\Coordinates\HSB::__constructFromArray([$H, $S, $B]);
$base = 'hsb';
foreach (['hwb', 'hsl', 'rgb'] as $coord) {
$target = $base . 'To' . ucfirst($coord);
$source = $coord . 'To' . ucfirst($base);
$converted_color = Color\Color::$target($color);
$color_b = Color\Color::$source($converted_color);
// print "COORD: " . $coord . ", HSL: " . print_r($color->returnAsArray(), true) . "\n";
$this->assertEqualsWithDelta(
$color->returnAsArray(),
$color_b->returnAsArray(),
self::DELTA,
'Convert ' . $base . ' to ' . $coord . ': ' . print_r($color->returnAsArray(), true) . '/'
. print_r($color_b->returnAsArray(), true)
);
}
}
}
}
}
/**
* Undocumented function
*
* @covers ::hwbToHsl
* @covers ::hslToHwb
* @covers ::hwbToHsb
* @covers ::hsbToHwb
* @testdox Convert from and to HWB via RGB, HSL, HSB/V
*
* @return void
*/
public function testHwbColorCoordinateConvertToAndBack(): void
{
for ($H = 0; $H <= 360; $H += 60) {
for ($W = 0; $W <= 100; $W += 20) {
for ($B = 0; $B <= 100; $B += 20) {
// if W>=20 and B>=80 or B>=20 and W>=20 or both >=50
// we cannot reverse correctl (B/W)
if (
($W >= 20 && $B >= 80) ||
($W >= 80 && $B >= 20) ||
($W >= 50 && $B >= 50)
) {
continue;
}
$base = 'hwb';
$color = Color\Coordinates\HWB::__constructFromArray([$H, $W, $B]);
foreach (['hsl', 'hsb', 'rgb'] as $coord) {
// for rgb hue on S = 0 is irrelevant (B/W)
if ($H > 0 && $coord == 'rgb') {
continue;
}
$target = $base . 'To' . ucfirst($coord);
$source = $coord . 'To' . ucfirst($base);
$converted_color = Color\Color::$target($color);
$color_b = Color\Color::$source($converted_color);
// print "COORD: " . $coord . ", HSL: " . print_r($color->returnAsArray(), true) . "\n";
$this->assertEqualsWithDelta(
$color->returnAsArray(),
$color_b->returnAsArray(),
self::DELTA,
'Convert ' . $base . ' to ' . $coord . ': ' . print_r($color->returnAsArray(), true) . '/'
. print_r($color_b->returnAsArray(), true)
);
}
}
}
}
}
/**
* Undocumented function
*
* covers ::returnAsHex()
* covers ::__constructFromHexString()
* @testdox Convert from RGB to hex and back
*
* @return void
*/
public function testRgbToFromHex(): void
{
for ($r = 0; $r <= 300; $r += 60) {
for ($g = 0; $g <= 300; $g += 60) {
for ($b = 0; $b <= 300; $b += 60) {
// for this test we stay in the correct lane
if ($r > 255) {
$r = 255;
}
if ($g > 255) {
$g = 255;
}
if ($b > 255) {
$b = 255;
}
// with or without prefix
foreach ([true, false] as $hex_prefix) {
$hex_color = Color\Coordinates\RGB::__constructFromArray([$r, $g, $b])
->returnAsHex($hex_prefix);
// parse into hex to rgb and see if we get the same r/g/b
$color = Color\Coordinates\RGB::__constructFromHexString($hex_color)->returnAsArray();
//
$this->assertEquals(
[$r, $g, $b],
$color,
'Convert rgb to hex and back: ' . print_r([$r, $g, $b], true) . '/'
. print_r($color, true)
);
}
}
}
}
}
// oklab
// cie lab
// create exceptions for all color spaces
public function testExceptionHSB(): void
{
// for H/S/B exception the same
$this->expectException(\LengthException::class);
Color\Coordinates\HSB::__constructFromArray([900, 10, 10]);
// invalid access to class
// $this->expectException(\ErrorException::class);
}
}
// __END__

View File

@@ -68,8 +68,63 @@ print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
// out of bounds test
// define a list of from to color sets for conversion test
$hwb = Color::hsbToHwb(Coordinates\HSB::__constructFromArray([
160,
0,
50,
]));
print "HWB: " . DgS::printAr($hwb) . "<br>";
$hsb = Color::hwbToHsb($hwb);
print "HSB: " . DgS::printAr($hsb) . "<br>";
$oklch = Color::rgbToOkLch(Coordinates\RGB::__constructFromArray([
250,
0,
0
]));
print "OkLch: " . DgS::printAr($oklch) . "<br>";
$rgb = Color::okLchToRgb($oklch);
print "OkLch -> RGB: " . DgS::printAr($rgb) . "<br>";
$oklab = Color::rgbToOkLab(Coordinates\RGB::__constructFromArray([
250,
0,
0
]));
print "OkLab: " . DgS::printAr($oklab) . "<br>";
print display($oklab->toCssString(), $oklab->toCssString(), 'Oklab');
$rgb = Color::okLabToRgb($oklab);
print "OkLab -> RGB: " . DgS::printAr($rgb) . "<br>";
print display($rgb->toCssString(), $rgb->toCssString(), 'OkLab to RGB');
$rgb = Coordinates\RGB::__constructFromArray([250, 100, 10])->toLinear();
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
$rgb = Coordinates\RGB::__constructFromArray([0, 0, 0])->toLinear();
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
$cie_lab = Color::okLabToLab($oklab);
print "CieLab: " . DgS::printAr($cie_lab) . "<br>";
print display($cie_lab->toCssString(), $cie_lab->toCssString(), 'OkLab to Cie Lab');
$rgb = Coordinates\RGB::__constructFromArray([0, 0, 60]);
$hsb = Color::rgbToHsb($rgb);
$rgb_b = Color::hsbToRgb($hsb);
print "RGB: " . DgS::printAr($rgb) . "<br>";
print "RGB->HSB: " . DgS::printAr($hsb) . "<br>";
print "HSB->RGB: " . DgS::printAr($rgb_b) . "<br>";
$hsl = Coordinates\HSL::__constructFromArray([0, 20, 0]);
$hsb = Coordinates\HSB::__constructFromArray([0, 20, 0]);
$hsl_from_hsb = Color::hsbToHsl($hsb);
print "HSL from HSB: " . DgS::printAr($hsl_from_hsb) . "<br>";
print "<hr>";
print "<h2>LEGACY</h2>";
// A(out of bounds)
try {
print "C::S/COLOR invalid rgb->hex (gray 125): -1, -1, -1: "
@@ -83,6 +138,7 @@ try {
} catch (\LengthException $e) {
print "**Exception: " . $e->getMessage() . "<br><pre>" . print_r($e, true) . "</pre><br>";
}
print "<hr>";
// B(valid)
$rgb = [50, 20, 30];
$hex = '#0a141e';
@@ -125,6 +181,8 @@ print "S::COLOR hsb->rgb: $hsb[0], $hsb[1], $hsb[2]: "
Colors::hsb2rgb($hsb[0], $hsb[1], $hsb[2])
)) . "<br>";
print "<hr>";
// Random text
$h = rand(0, 359);
$s = rand(15, 70);
@@ -134,49 +192,12 @@ print "RANDOM IN: H: " . $h . ", S: " . $s . ", B/L: " . $b . "/" . $l . "<br>";
print "RANDOM hsb->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsb2rgb($h, $s, $b))) . "</pre><br>";
print "RANDOM hsl->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsl2rgb($h, $s, $l))) . "</pre><br>";
print "<hr>";
$rgb = [0, 0, 0];
print "rgb 0,0,0: " . Dgs::printAr($rgb) . " => " . Dgs::printAr(Colors::rgb2hsb($rgb[0], $rgb[1], $rgb[2])) . "<br>";
// TODO: run compare check input must match output
$hwb = Color::hsbToHwb(Coordinates\HSB::__constructFromArray([
160,
0,
50,
]));
print "HWB: " . DgS::printAr($hwb) . "<br>";
$hsb = Color::hwbToHsb($hwb);
print "HSB: " . DgS::printAr($hsb) . "<br>";
$oklch = Color::rgbToOkLch(Coordinates\RGB::__constructFromArray([
250,
0,
0
]));
print "OkLch: " . DgS::printAr($oklch) . "<br>";
$rgb = Color::okLchToRgb($oklch);
print "OkLch -> RGB: " . DgS::printAr($rgb) . "<br>";
$oklab = Color::rgbToOkLab(Coordinates\RGB::__constructFromArray([
250,
0,
0
]));
print "OkLab: " . DgS::printAr($oklab) . "<br>";
print display($oklab->toCssString(), $oklab->toCssString(), 'Oklab');
$rgb = Color::okLabToRgb($oklab);
print "OkLab -> RGB: " . DgS::printAr($rgb) . "<br>";
print display($rgb->toCssString(), $rgb->toCssString(), 'OkLab to RGB');
$rgb = Coordinates\RGB::__constructFromArray([250, 100, 10])->toLinear();
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
$rgb = Coordinates\RGB::__constructFromArray([0, 0, 0])->toLinear();
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
$cie_lab = Color::okLabToLab($oklab);
print "CieLab: " . DgS::printAr($cie_lab) . "<br>";
print display($cie_lab->toCssString(), $cie_lab->toCssString(), 'OkLab to Cie Lab');
print "<hr>";
print "</body></html>";

View File

@@ -354,15 +354,21 @@ class Color
{
$saturation = $hsl->S / 100;
$lightness = $hsl->L / 100;
$value = $lightness + $saturation * min($lightness, 1 - $lightness);
// if lightness is 0, then we cannot return convert to hsb
$value = $lightness + $saturation * min($lightness, 1 - $lightness);
// print "Orig: " . print_r($hsl, true) . "\n";
// print "SAT: " . $saturation . ", Lightness: " . $lightness . ", Value: " . $value . "\n";
// var_dump($value);
// check for black and white
$saturation = ($value === 0) ?
$saturation = $value == 0 ?
0 :
200 * (1 - $lightness / $value);
$value *= 100;
return HSB::__constructFromArray([
$hsl->H,
$saturation,
$value * 100,
$value,
]);
}
@@ -377,11 +383,11 @@ class Color
// hsv/toHsl
$hue = $hsb->H;
$saturation = $hsb->S / 100;
$value = $hsb->V / 100;
$value = $hsb->B / 100;
$lightness = $value * (1 - $saturation / 2);
// check for B/W
$saturation = in_array($lightness, [0, 1], true) ?
$saturation = in_array($lightness, [0, 1]) ?
0 :
100 * ($value - $lightness) / min($lightness, 1 - $lightness)
;
@@ -456,6 +462,7 @@ class Color
$blackness = $hwb->B / 100;
$sum = $whiteness + $blackness;
// print "S: B/W: " . $sum . " /W: " . $whiteness . " /B: " . $blackness . "\n";
// for black and white
if ($sum >= 1) {
$saturation = 0;
@@ -475,8 +482,6 @@ class Color
// MARK: LAB <-> LCH
// toLch
/**
* CIE Lab to LCH
*

View File

@@ -62,10 +62,10 @@ class HSB
}
switch ($name) {
case 'H':
if ($value == 360) {
if ((int)$value == 360) {
$value = 0;
}
if ($value < 0 || $value > 359) {
if ((int)$value < 0 || (int)$value > 359) {
throw new \LengthException(
'Argument value ' . $value . ' for hue is not in the range of 0 to 359',
1
@@ -73,7 +73,7 @@ class HSB
}
break;
case 'S':
if ($value < 0 || $value > 100) {
if ((int)$value < 0 || (int)$value > 100) {
throw new \LengthException(
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
2
@@ -81,7 +81,7 @@ class HSB
}
break;
case 'B':
if ($value < 0 || $value > 100) {
if ((int)$value < 0 || (int)$value > 100) {
throw new \LengthException(
'Argument value ' . $value . ' for brightness is not in the range of 0 to 100',
3

View File

@@ -63,10 +63,10 @@ class HSL
}
switch ($name) {
case 'H':
if ($value == 360) {
if ((int)$value == 360) {
$value = 0;
}
if ($value < 0 || $value > 359) {
if ((int)$value < 0 || (int)$value > 359) {
throw new \LengthException(
'Argument value ' . $value . ' for hue is not in the range of 0 to 359',
1
@@ -74,7 +74,7 @@ class HSL
}
break;
case 'S':
if ($value < 0 || $value > 100) {
if ((int)$value < 0 || (int)$value > 100) {
throw new \LengthException(
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
2
@@ -82,7 +82,7 @@ class HSL
}
break;
case 'L':
if ($value < 0 || $value > 100) {
if ((int)$value < 0 || (int)$value > 100) {
throw new \LengthException(
'Argument value ' . $value . ' for luminance is not in the range of 0 to 100',
3

View File

@@ -63,10 +63,10 @@ class HWB
}
switch ($name) {
case 'H':
if ($value == 360) {
if ((int)$value == 360) {
$value = 0;
}
if ($value < 0 || $value > 360) {
if ((int)$value < 0 || (int)$value > 360) {
throw new \LengthException(
'Argument value ' . $value . ' for hue is not in the range of 0 to 360',
1
@@ -74,7 +74,7 @@ class HWB
}
break;
case 'W':
if ($value < 0 || $value > 100) {
if ((int)$value < 0 || (int)$value > 100) {
throw new \LengthException(
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
2
@@ -82,7 +82,7 @@ class HWB
}
break;
case 'B':
if ($value < 0 || $value > 100) {
if ((int)$value < 0 || (int)$value > 100) {
throw new \LengthException(
'Argument value ' . $value . ' for luminance is not in the range of 0 to 100',
3

View File

@@ -85,11 +85,10 @@ class RGB
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
}
// if not linear
if (!$this->linear && ($value < 0 || $value > 255)) {
if (!$this->linear && ((int)$value < 0 || (int)$value > 255)) {
throw new \LengthException('Argument value ' . $value . ' for color ' . $name
. ' is not in the range of 0 to 255', 1);
} elseif ($this->linear && ($value < -10E10 || $value > 1)) {
// not allow very very small negative numbers
} elseif ($this->linear && ((int)$value < 0 || (int)$value > 1)) {
throw new \LengthException('Argument value ' . $value . ' for color ' . $name
. ' is not in the range of 0 to 1 for linear rgb', 1);
}