diff --git a/4dev/tests/CoreLibsCheckColorsTest.php b/4dev/tests/CoreLibsCheckColorsTest.php new file mode 100644 index 00000000..7386bf76 --- /dev/null +++ b/4dev/tests/CoreLibsCheckColorsTest.php @@ -0,0 +1,329 @@ + [ + '#ab12cd', + null, + true, + ], + 'valid hex rgb, flag ALL' => [ + '#ab12cd', + \CoreLibs\Check\Colors::ALL, + true, + ], + 'valid hex rgb, flag HEX_RGB' => [ + '#ab12cd', + \CoreLibs\Check\Colors::HEX_RGB, + true, + ], + 'valid hex rgb, wrong flag' => [ + '#ab12cd', + \CoreLibs\Check\Colors::RGB, + false, + ], + // error + 'invalid hex rgb A' => [ + '#ab12zz', + null, + false, + ], + 'invalid hex rgb B' => [ + '#ZyQfo', + null, + false, + ], + // other valid hex checks + 'valid hex rgb, alt A' => [ + '#AB12cd', + null, + true, + ], + // * hax alpha + 'valid hex rgb alpha, flag ALL (default)' => [ + '#ab12cd12', + null, + true, + ], + 'valid hex rgb alpha, flag ALL' => [ + '#ab12cd12', + \CoreLibs\Check\Colors::ALL, + true, + ], + 'valid hex rgb alpha, flag HEX_RGBA' => [ + '#ab12cd12', + \CoreLibs\Check\Colors::HEX_RGBA, + true, + ], + 'valid hex rgb alpha, wrong flag' => [ + '#ab12cd12', + \CoreLibs\Check\Colors::RGB, + false, + ], + // error + 'invalid hex rgb alpha A' => [ + '#ab12dd1', + null, + false, + ], + 'invalid hex rgb alpha B' => [ + '#ab12ddzz', + null, + false, + ], + 'valid hex rgb alpha, alt A' => [ + '#ab12cdEE', + null, + true, + ], + // * rgb + 'valid rgb, flag ALL (default)' => [ + 'rgb(255, 10, 20)', + null, + true, + ], + 'valid rgb, flag ALL' => [ + 'rgb(255, 10, 20)', + \CoreLibs\Check\Colors::ALL, + true, + ], + 'valid rgb, flag RGB' => [ + 'rgb(255, 10, 20)', + \CoreLibs\Check\Colors::RGB, + true, + ], + 'valid rgb, wrong flag' => [ + 'rgb(255, 10, 20)', + \CoreLibs\Check\Colors::HEX_RGB, + false, + ], + // error + 'invalid rgb A' => [ + 'rgb(356, 10, 20)', + null, + false, + ], + // other valid rgb conbinations + 'valid rgb, alt A (percent)' => [ + 'rgb(100%, 10%, 20%)', + null, + true, + ], + // TODO check all % and non percent combinations + 'valid rgb, alt B (percent, mix)' => [ + 'rgb(100%, 10, 40)', + null, + true, + ], + // * rgb alpha + 'valid rgba, flag ALL (default)' => [ + 'rgba(255, 10, 20, 0.5)', + null, + true, + ], + 'valid rgba, flag ALL' => [ + 'rgba(255, 10, 20, 0.5)', + \CoreLibs\Check\Colors::ALL, + true, + ], + 'valid rgba, flag RGB' => [ + 'rgba(255, 10, 20, 0.5)', + \CoreLibs\Check\Colors::RGBA, + true, + ], + 'valid rgba, wrong flag' => [ + 'rgba(255, 10, 20, 0.5)', + \CoreLibs\Check\Colors::HEX_RGB, + false, + ], + // error + 'invalid rgba A' => [ + 'rgba(356, 10, 20, 0.5)', + null, + false, + ], + // other valid rgba combinations + 'valid rgba, alt A (percent)' => [ + 'rgba(100%, 10%, 20%, 0.5)', + null, + true, + ], + // TODO check all % and non percent combinations + 'valid rgba, alt B (percent, mix)' => [ + 'rgba(100%, 10, 40, 0.5)', + null, + true, + ], + // TODO check all % and non percent combinations with percent transparent + 'valid rgba, alt C (percent transparent)' => [ + 'rgba(100%, 10%, 20%, 50%)', + null, + true, + ], + /* + // hsl + 'hsl(100, 50%, 60%)', + 'hsl(100, 50.5%, 60.5%)', + 'hsla(100, 50%, 60%)', + 'hsla(100, 50.5%, 60.5%)', + 'hsla(100, 50%, 60%, 0.5)', + 'hsla(100, 50.5%, 60.5%, 0.5)', + 'hsla(100, 50%, 60%, 50%)', + 'hsla(100, 50.5%, 60.5%, 50%)', + */ + // * hsl + 'valid hsl, flag ALL (default)' => [ + 'hsl(100, 50%, 60%)', + null, + true, + ], + 'valid hsl, flag ALL' => [ + 'hsl(100, 50%, 60%)', + \CoreLibs\Check\Colors::ALL, + true, + ], + 'valid hsl, flag RGB' => [ + 'hsl(100, 50%, 60%)', + \CoreLibs\Check\Colors::HSL, + true, + ], + 'valid hsl, wrong flag' => [ + 'hsl(100, 50%, 60%)', + \CoreLibs\Check\Colors::HEX_RGB, + false, + ], + 'invalid hsl A' => [ + 'hsl(500, 50%, 60%)', + null, + false, + ], + 'valid hsl, alt A' => [ + 'hsl(100, 50.5%, 60.5%)', + null, + true, + ], + // * hsl alpha + 'valid hsla, flag ALL (default)' => [ + 'hsla(100, 50%, 60%, 0.5)', + null, + true, + ], + 'valid hsla, flag ALL' => [ + 'hsla(100, 50%, 60%, 0.5)', + \CoreLibs\Check\Colors::ALL, + true, + ], + 'valid hsla, flag RGB' => [ + 'hsla(100, 50%, 60%, 0.5)', + \CoreLibs\Check\Colors::HSLA, + true, + ], + 'valid hsla, wrong flag' => [ + 'hsla(100, 50%, 60%, 0.5)', + \CoreLibs\Check\Colors::HEX_RGB, + false, + ], + 'invalid hsla A' => [ + 'hsla(500, 50%, 60%, 0.5)', + null, + false, + ], + 'valid hsla, alt A (percent alpha' => [ + 'hsla(100, 50%, 60%, 50%)', + null, + true, + ], + 'valid hsla, alt A (percent alpha' => [ + 'hsla(100, 50.5%, 60.5%, 50%)', + null, + true, + ], + // * combined flag checks + 'valid rgb, flag RGB|RGBA' => [ + 'rgb(100%, 10%, 20%)', + \CoreLibs\Check\Colors::RGB | \CoreLibs\Check\Colors::RGBA, + true, + ], + // TODO other combined flag checks all combinations + // * invalid string + 'invalid string A' => [ + 'invalid string', + null, + false, + ], + 'invalid string B' => [ + '(hsla(100, 100, 100))', + null, + false, + ], + 'invalid string C' => [ + 'hsla(100, 100, 100', + null, + false, + ], + ]; + } + + /** + * Undocumented function + * + * @covers ::validateColor + * @dataProvider validateColorProvider + * @testdox validateColor $input with flags $flags be $expected [$_dataName] + * + * @param string $input + * @param int|null $flags + * @param bool $expected + * @return void + */ + public function testValidateColor(string $input, ?int $flags, bool $expected) + { + if ($flags === null) { + $result = \CoreLibs\Check\Colors::validateColor($input); + } else { + $result = \CoreLibs\Check\Colors::validateColor($input, $flags); + } + $this->assertEquals( + $expected, + $result + ); + } + + /** + * Undocumented function + * + * @covers ::validateColor + * @testWith [99] + * @testdox Check Exception throw for $flag + * + * @param int $flag + * @return void + */ + public function testValidateColorException(int $flag): void + { + $this->expectException(\Exception::class); + \CoreLibs\Check\Colors::validateColor('#ffffff', $flag); + } +} + +// __END__ diff --git a/4dev/tests/CoreLibsConvertColorsTest.php b/4dev/tests/CoreLibsConvertColorsTest.php index d58ce903..7b288f0c 100644 --- a/4dev/tests/CoreLibsConvertColorsTest.php +++ b/4dev/tests/CoreLibsConvertColorsTest.php @@ -122,6 +122,8 @@ final class CoreLibsConvertColorsTest extends TestCase */ public function rgb2hslAndhsbList(): array { + // if hsb_from or hsl_from is set, this will be used in hsb/hsl convert + // hsb_rgb is used for adjusted rgb valus due to round error to in return [ 'valid gray' => [ 'rgb' => [12, 12, 12], @@ -137,6 +139,16 @@ final class CoreLibsConvertColorsTest extends TestCase 'hsl' => [211.6, 90.5, 41.2], 'valid' => true, ], + // hsg/hsl with 360 which is seen as 0 + 'valid color hue 360' => [ + 'rgb' => [200, 10, 10], + 'hsb' => [0, 95, 78.0], + 'hsb_from' => [360, 95, 78.0], + 'hsb_rgb' => [199, 10, 10], // should be rgb, but rounding error + 'hsl' => [0.0, 90.5, 41.2], + 'hsl_from' => [360.0, 90.5, 41.2], + 'valid' => true, + ], // invalid values 'invalid color' => [ 'rgb' => [-12, 300, 12], @@ -176,9 +188,9 @@ final class CoreLibsConvertColorsTest extends TestCase $list = []; foreach ($this->rgb2hslAndhsbList() as $name => $values) { $list[$name . ', hsb to rgb'] = [ - 0 => $values['hsb'][0], - 1 => $values['hsb'][1], - 2 => $values['hsb'][2], + 0 => $values['hsb_from'][0] ?? $values['hsb'][0], + 1 => $values['hsb_from'][1] ?? $values['hsb'][1], + 2 => $values['hsb_from'][2] ?? $values['hsb'][2], 3 => $values['valid'] ? $values['hsb_rgb'] : false ]; } @@ -214,9 +226,9 @@ final class CoreLibsConvertColorsTest extends TestCase $list = []; foreach ($this->rgb2hslAndhsbList() as $name => $values) { $list[$name . ', hsl to rgb'] = [ - 0 => $values['hsl'][0], - 1 => $values['hsl'][1], - 2 => $values['hsl'][2], + 0 => $values['hsl_from'][0] ?? $values['hsl'][0], + 1 => $values['hsl_from'][1] ?? $values['hsl'][1], + 2 => $values['hsl_from'][2] ?? $values['hsl'][2], 3 => $values['valid'] ? $values['rgb'] : false ]; } @@ -382,6 +394,27 @@ final class CoreLibsConvertColorsTest extends TestCase \CoreLibs\Convert\Colors::hsl2rgb($input_h, $input_s, $input_l) ); } + + /** + * edge case check hsl/hsb and hue 360 (= 0) + * + * @covers ::hsl2rgb + * @covers ::hsb2rgb + * @testdox hsl2rgb/hsb2rgb hue 360 valid check + * + * @return void + */ + public function testHslHsb360hue(): void + { + $this->assertNotFalse( + \CoreLibs\Convert\Colors::hsl2rgb(360.0, 90.5, 41.2), + 'HSL to RGB with 360 hue' + ); + $this->assertNotFalse( + \CoreLibs\Convert\Colors::hsb2rgb(360, 95, 78.0), + 'HSB to RGB with 360 hue' + ); + } } // __END__ diff --git a/www/admin/class_test.check.colors.php b/www/admin/class_test.check.colors.php new file mode 100644 index 00000000..9ca898ad --- /dev/null +++ b/www/admin/class_test.check.colors.php @@ -0,0 +1,123 @@ + BASE . LOG, + 'file_id' => $LOG_FILE_ID, + // add file date + 'print_file_date' => true, + // set debug and print flags + 'debug_all' => $DEBUG_ALL ?? false, + 'echo_all' => $ECHO_ALL ?? false, + 'print_all' => $PRINT_ALL ?? false, +]); + +$PAGE_NAME = 'TEST CLASS: CHECK COLORS'; +print ""; +print "