From a21a46eb0eee8f9bfde0ce06a9f4eea5a3ffd832 Mon Sep 17 00:00:00 2001 From: macocci7 Date: Sat, 19 Apr 2025 10:50:14 +0900 Subject: [PATCH] Add support for uppercase hex codes --- README.md | 17 +++++++------ playground/converter.php | 30 +++++++++++++++++++++++ src/Converter.php | 23 ++++++++++++++++-- tests/ConverterTest.php | 31 ++++++++++++++++++++++++ tests/{Filter => Filters}/FilterTest.php | 0 5 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 playground/converter.php rename tests/{Filter => Filters}/FilterTest.php (100%) diff --git a/README.md b/README.md index 5555423..e13f4fc 100644 --- a/README.md +++ b/README.md @@ -280,7 +280,7 @@ composer require macocci7/bash-colorizer > *5: Partially effective
-e.g.) on VSCode Terminal +e.g.) on VSCode Terminal (theme:`Dark Modern`) @@ -302,36 +302,36 @@ See more: [Select Graphic Rendition parameters | ANSI escape code | Wikipedia](h - `default` - e.g.) on VSCode Terminal + e.g.) on VSCode Terminal (theme:`Dark Modern`) |Foregound Colors|Background Colors| |---|---| ||| - 256 colors [ 0 - 255 ]: `foreground`/`background`/`underline` - e.g.) foreground colors on VSCode Terminal: + e.g.) foreground colors on VSCode Terminal (theme:`Dark Modern`): - e.g.) background colors on VSCode Terminal: + e.g.) background colors on VSCode Terminal (theme:`Dark Modern`): - e.g.) underline colors on VSCode Terminal: + e.g.) underline colors on VSCode Terminal (theme:`Dark Modern`): - 24bit (16777216) colors: - e.g.) foreground colors on VSCode Terminal: + e.g.) foreground colors on VSCode Terminal (theme:`Dark Modern`): - e.g.) background colors on VSCode Terminal: + e.g.) background colors on VSCode Terminal (theme:`Dark Modern`): - e.g.) underline colors on VSCode Terminal: + e.g.) underline colors on VSCode Terminal (theme:`Dark Modern`): @@ -350,6 +350,7 @@ Example codes are in [playground](playground/) directory. - [underline_256colors.php](playground/underline_256colors.php) - [underline_24bitcolors.php](playground/underline_24bitcolors.php) - [readable.php](playground/readable.php) +- [converter.php](playground/converter.php) ## 8. LICENSE diff --git a/playground/converter.php b/playground/converter.php new file mode 100644 index 0000000..96bb0b4 --- /dev/null +++ b/playground/converter.php @@ -0,0 +1,30 @@ + [%s, %s, %s]\n", + Colorizer::foreground($hex)->encode($hex), + Colorizer::foreground('red')->encode($rgb[0]), + Colorizer::foreground('green')->encode($rgb[1]), + Colorizer::foreground('blue')->encode($rgb[2]), +); + +// decimal to rgb +$rgb = [254, 220, 186]; +$hex = Converter::hex($rgb); + +echo sprintf( + "[%s, %s, %s] => %s\n", + Colorizer::foreground('red')->encode($rgb[0]), + Colorizer::foreground('green')->encode($rgb[1]), + Colorizer::foreground('blue')->encode($rgb[2]), + Colorizer::foreground($hex)->encode($hex), +); diff --git a/src/Converter.php b/src/Converter.php index d843757..e0cf175 100644 --- a/src/Converter.php +++ b/src/Converter.php @@ -2,6 +2,8 @@ namespace Macocci7\BashColorizer; +use Macocci7\BashColorizer\Filters\Filter; + class Converter { /** @@ -11,14 +13,14 @@ public static function decimal(string $rgb): array { $patternS = '/^#([0-9a-f])([0-9a-f])([0-9a-f])$/'; $patternL = '/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/'; - if (preg_match($patternS, $rgb, $matches)) { + if (preg_match($patternS, strtolower($rgb), $matches)) { // short format (#rgb) return [ hexdec($matches[1] . $matches[1]), hexdec($matches[2] . $matches[2]), hexdec($matches[3] . $matches[3]), ]; - } elseif (preg_match($patternL, $rgb, $matches)) { + } elseif (preg_match($patternL, strtolower($rgb), $matches)) { // long format (#rrggbb) return [ hexdec($matches[1]), @@ -28,4 +30,21 @@ public static function decimal(string $rgb): array } return []; } + + /** + * @param int[] $rgb + */ + public static function hex(array $rgb): string|null + { + if (empty($rgb)) { + return null; + } + $rgb = Filter::rgb($rgb); + return sprintf( + "#%02s%02s%02s", + dechex($rgb[0]), + dechex($rgb[1]), + dechex($rgb[2]), + ); + } } diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index 4096330..02a721c 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -16,9 +16,15 @@ public static function provide_decimal_can_return_correct_array(): array '#000' => ['rgb' => '#000', 'expected' => [0, 0, 0]], '#123' => ['rgb' => '#123', 'expected' => [17, 34, 51]], '#fed' => ['rgb' => '#fed', 'expected' => [255, 238, 221]], + '#FED' => ['rgb' => '#FED', 'expected' => [255, 238, 221]], + '#19f' => ['rgb' => '#19f', 'expected' => [17, 153, 255]], + '#19F' => ['rgb' => '#19F', 'expected' => [17, 153, 255]], '#000000' => ['rgb' => '#000000', 'expected' => [0, 0, 0]], '#123456' => ['rgb' => '#123456', 'expected' => [18, 52, 86]], '#ffeedd' => ['rgb' => '#ffeedd', 'expected' => [255, 238, 221]], + '#FFEEDD' => ['rgb' => '#FFEEDD', 'expected' => [255, 238, 221]], + '#123abc' => ['rgb' => '#123abc', 'expected' => [18, 58, 188]], + '#123ABC' => ['rgb' => '#123abc', 'expected' => [18, 58, 188]], 'fff' => ['rgb' => 'fff', 'expected' => []], 'fffffff' => ['rgb' => 'fffffff', 'expected' => []], @@ -26,9 +32,11 @@ public static function provide_decimal_can_return_correct_array(): array '#1' => ['rgb' => '#1', 'expected' => []], '#12' => ['rgb' => '#12', 'expected' => []], '#efg' => ['rgb' => '#efg', 'expected' => []], + '#EFG' => ['rgb' => '#EFG', 'expected' => []], '#1234' => ['rgb' => '#1234', 'expected' => []], '#12345' => ['rgb' => '#12345', 'expected' => []], '#bcdefg' => ['rgb' => '#bcdefg', 'expected' => []], + '#BCDEFG' => ['rgb' => '#BCDEFG', 'expected' => []], '#1234567' => ['rgb' => '#1234567', 'expected' => []], '#abcdefg' => ['rgb' => '#abcdefg', 'expected' => []], ]; @@ -39,4 +47,27 @@ public function test_decimal_can_return_correct_array(string $rgb, array $expect { $this->assertSame($expected, Converter::decimal($rgb)); } + + public static function provide_hex_can_return_correct_hex_code(): array + { + return [ + 'empty' => ['rgb' => [], 'expected' => null], + '[0]' => ['rgb' => [0], 'expected' => '#000000'], + '[1]' => ['rgb' => [1], 'expected' => '#010000'], + '[1, 2]' => ['rgb' => [1, 2], 'expected' => '#010200'], + '[1, 2, 3]' => ['rgb' => [1, 2, 3], 'expected' => '#010203'], + '[0, 0, 10]' => ['rgb' => [0, 0, 10], 'expected' => '#00000a'], + '[0, 0, 171]' => ['rgb' => [0, 0, 171], 'expected' => '#0000ab'], + '[0, 171, 205]' => ['rgb' => [0, 171, 205], 'expected' => '#00abcd'], + '[171, 205, 239]' => ['rgb' => [171, 205, 239], 'expected' => '#abcdef'], + '[-1, 0, 1]' => ['rgb' => [-1, 0, 1], 'expected' => '#000001'], + '[254, 255, 256]' => ['rgb' => [254, 255, 256], 'expected' => '#feffff'], + ]; + } + + #[DataProvider('provide_hex_can_return_correct_hex_code')] + public function test_hex_can_return_correct_hex_code(array $rgb, string|null $expected): void + { + $this->assertSame($expected, Converter::hex($rgb)); + } } diff --git a/tests/Filter/FilterTest.php b/tests/Filters/FilterTest.php similarity index 100% rename from tests/Filter/FilterTest.php rename to tests/Filters/FilterTest.php