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

@@ -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);
}