From dbbcded7a46049233e68831ecf4a6c469f143b5b Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 15 Oct 2023 10:39:15 +0200 Subject: [PATCH] Replace driver color classes --- docker-compose.yml | 2 +- src/Colors/Rgb/Parser.php | 193 ++++++++++++++++++ src/Colors/Rgba/Parser.php | 55 +++++ src/Drivers/Gd/Decoders/HexColorDecoder.php | 27 ++- .../Gd/Decoders/HtmlColorNameDecoder.php | 18 +- .../Gd/Decoders/RgbArrayColorDecoder.php | 38 ---- .../Gd/Decoders/RgbStringColorDecoder.php | 26 +-- src/Drivers/Gd/InputHandler.php | 5 +- .../Imagick/Decoders/HexColorDecoder.php | 27 ++- .../Imagick/Decoders/HtmlColorNameDecoder.php | 15 +- .../Imagick/Decoders/RgbArrayColorDecoder.php | 36 ---- .../Decoders/RgbStringColorDecoder.php | 20 +- src/Drivers/Imagick/InputHandler.php | 7 +- src/Traits/CanReadHtmlColorNames.php | 165 --------------- tests/Colors/Rgb/ParserTest.php | 60 ++++++ tests/Colors/Rgba/ParserTest.php | 68 ++++++ tests/Drivers/Abstract/AbstractColorTest.php | 39 ---- tests/Drivers/Gd/ColorTest.php | 88 -------- .../Gd/Decoders/RgbArrayColorDecoderTest.php | 25 --- tests/Drivers/Gd/InputHandlerTest.php | 77 +++---- tests/Drivers/Imagick/ColorTest.php | 112 ---------- .../Decoders/RgbArrayColorDecoderTest.php | 25 --- tests/Drivers/Imagick/InputHandlerTest.php | 55 ++++- 23 files changed, 545 insertions(+), 638 deletions(-) create mode 100644 src/Colors/Rgb/Parser.php create mode 100644 src/Colors/Rgba/Parser.php delete mode 100644 src/Drivers/Gd/Decoders/RgbArrayColorDecoder.php delete mode 100644 src/Drivers/Imagick/Decoders/RgbArrayColorDecoder.php delete mode 100644 src/Traits/CanReadHtmlColorNames.php create mode 100644 tests/Colors/Rgb/ParserTest.php create mode 100644 tests/Colors/Rgba/ParserTest.php delete mode 100644 tests/Drivers/Abstract/AbstractColorTest.php delete mode 100644 tests/Drivers/Gd/ColorTest.php delete mode 100644 tests/Drivers/Gd/Decoders/RgbArrayColorDecoderTest.php delete mode 100644 tests/Drivers/Imagick/ColorTest.php delete mode 100644 tests/Drivers/Imagick/Decoders/RgbArrayColorDecoderTest.php diff --git a/docker-compose.yml b/docker-compose.yml index 8d90fbff..e32993d7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: tests: build: ./ working_dir: /project - command: bash -c "composer install && ./vendor/bin/phpunit -vvv" + command: bash -c "composer install && ./vendor/bin/phpunit -vvv --filter=InputHandlerTest" volumes: - ./:/project analysis: diff --git a/src/Colors/Rgb/Parser.php b/src/Colors/Rgb/Parser.php new file mode 100644 index 00000000..be787dab --- /dev/null +++ b/src/Colors/Rgb/Parser.php @@ -0,0 +1,193 @@ +[0-9a-f]{3}|[0-9a-f]{6})$/i'; + $result = preg_match($pattern, $input, $matches); + + if ($result !== 1) { + throw new ColorException('Unable to parse color'); + } + + $matches = match (strlen($matches['hex'])) { + 3 => str_split($matches['hex']), + 6 => str_split($matches['hex'], 2), + default => throw new ColorException('Unable to parse color'), + }; + + return new Color( + strlen($matches[0]) == '1' ? hexdec($matches[0] . $matches[0]) : hexdec($matches[0]), + strlen($matches[1]) == '1' ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]), + strlen($matches[2]) == '1' ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2]), + ); + } + + public static function fromString(string $input): Color + { + $pattern = '/^rgb\((?P[0-9]{1,3}), ?(?P[0-9]{1,3}), ?(?P[0-9]{1,3})\)$/'; + $result = preg_match($pattern, $input, $matches); + + if ($result !== 1) { + throw new ColorException('Unable to parse color'); + } + + return new Color($matches['r'], $matches['g'], $matches['b']); + } + + public static function fromName(string $input): Color + { + $names = [ + 'lightsalmon' => '#FFA07A', + 'salmon' => '#FA8072', + 'darksalmon' => '#E9967A', + 'lightcoral' => '#F08080', + 'indianred' => '#CD5C5C', + 'crimson' => '#DC143C', + 'firebrick' => '#B22222', + 'red' => '#FF0000', + 'darkred' => '#8B0000', + 'coral' => '#FF7F50', + 'tomato' => '#FF6347', + 'orangered' => '#FF4500', + 'gold' => '#FFD700', + 'orange' => '#FFA500', + 'darkorange' => '#FF8C00', + 'lightyellow' => '#FFFFE0', + 'lemonchiffon' => '#FFFACD', + 'lightgoldenrodyellow' => '#FAFAD2', + 'papayawhip' => '#FFEFD5', + 'moccasin' => '#FFE4B5', + 'peachpuff' => '#FFDAB9', + 'palegoldenrod' => '#EEE8AA', + 'khaki' => '#F0E68C', + 'darkkhaki' => '#BDB76B', + 'yellow' => '#FFFF00', + 'lawngreen' => '#7CFC00', + 'chartreuse' => '#7FFF00', + 'limegreen' => '#32CD32', + 'lime' => '#00FF00', + 'forestgreen' => '#228B22', + 'green' => '#008000', + 'darkgreen' => '#006400', + 'greenyellow' => '#ADFF2F', + 'yellowgreen' => '#9ACD32', + 'springgreen' => '#00FF7F', + 'mediumspringgreen' => '#00FA9A', + 'lightgreen' => '#90EE90', + 'palegreen' => '#98FB98', + 'darkseagreen' => '#8FBC8F', + 'mediumseagre' => 'en #3CB371', + 'seagreen' => '#2E8B57', + 'olive' => '#808000', + 'darkolivegreen' => '#556B2F', + 'olivedrab' => '#6B8E23', + 'lightcyan' => '#E0FFFF', + 'cyan' => '#00FFFF', + 'aqua' => '#00FFFF', + 'aquamarine' => '#7FFFD4', + 'mediumaquamarine' => '#66CDAA', + 'paleturquoise' => '#AFEEEE', + 'turquoise' => '#40E0D0', + 'mediumturquoise' => '#48D1CC', + 'darkturquoise' => '#00CED1', + 'lightseagreen' => '#20B2AA', + 'cadetblue' => '#5F9EA0', + 'darkcyan' => '#008B8B', + 'teal' => '#008080', + 'powderblue' => '#B0E0E6', + 'lightblue' => '#ADD8E6', + 'lightskyblue' => '#87CEFA', + 'skyblue' => '#87CEEB', + 'deepskyblue' => '#00BFFF', + 'lightsteelblue' => '#B0C4DE', + 'dodgerblue' => '#1E90FF', + 'cornflowerblue' => '#6495ED', + 'steelblue' => '#4682B4', + 'royalblue' => '#4169E1', + 'blue' => '#0000FF', + 'mediumblue' => '#0000CD', + 'darkblue' => '#00008B', + 'navy' => '#000080', + 'midnightblue' => '#191970', + 'mediumslateblue' => '#7B68EE', + 'slateblue' => '#6A5ACD', + 'darkslateblue' => '#483D8B', + 'lavender' => '#E6E6FA', + 'thistle' => '#D8BFD8', + 'plum' => '#DDA0DD', + 'violet' => '#EE82EE', + 'orchid' => '#DA70D6', + 'fuchsia' => '#FF00FF', + 'magenta' => '#FF00FF', + 'mediumorchid' => '#BA55D3', + 'mediumpurple' => '#9370DB', + 'blueviolet' => '#8A2BE2', + 'darkviolet' => '#9400D3', + 'darkorchid' => '#9932CC', + 'darkmagenta' => '#8B008B', + 'purple' => '#800080', + 'indigo' => '#4B0082', + 'pink' => '#FFC0CB', + 'lightpink' => '#FFB6C1', + 'hotpink' => '#FF69B4', + 'deeppink' => '#FF1493', + 'palevioletred' => '#DB7093', + 'mediumvioletred' => '#C71585', + 'white' => '#FFFFFF', + 'snow' => '#FFFAFA', + 'honeydew' => '#F0FFF0', + 'mintcream' => '#F5FFFA', + 'azure' => '#F0FFFF', + 'aliceblue' => '#F0F8FF', + 'ghostwhite' => '#F8F8FF', + 'whitesmoke' => '#F5F5F5', + 'seashell' => '#FFF5EE', + 'beige' => '#F5F5DC', + 'oldlace' => '#FDF5E6', + 'floralwhite' => '#FFFAF0', + 'ivory' => '#FFFFF0', + 'antiquewhite' => '#FAEBD7', + 'linen' => '#FAF0E6', + 'lavenderblush' => '#FFF0F5', + 'mistyrose' => '#FFE4E1', + 'gainsboro' => '#DCDCDC', + 'lightgray' => '#D3D3D3', + 'silver' => '#C0C0C0', + 'darkgray' => '#A9A9A9', + 'gray' => '#808080', + 'dimgray' => '#696969', + 'lightslategray' => '#778899', + 'slategray' => '#708090', + 'darkslategray' => '#2F4F4F', + 'black' => '#000000', + 'cornsilk' => '#FFF8DC', + 'blanchedalmond' => '#FFEBCD', + 'bisque' => '#FFE4C4', + 'navajowhite' => '#FFDEAD', + 'wheat' => '#F5DEB3', + 'burlywood' => '#DEB887', + 'tan' => '#D2B48C', + 'rosybrown' => '#BC8F8F', + 'sandybrown' => '#F4A460', + 'goldenrod' => '#DAA520', + 'peru' => '#CD853F', + 'chocolate' => '#D2691E', + 'saddlebrown' => '#8B4513', + 'sienna' => '#A0522D', + 'brown' => '#A52A2A', + 'maroon' => '#800000', + ]; + + if (!array_key_exists(strtolower($input), $names)) { + throw new ColorException('Unable to parse color'); + } + + return static::fromHex($names[strtolower($input)]); + } +} diff --git a/src/Colors/Rgba/Parser.php b/src/Colors/Rgba/Parser.php new file mode 100644 index 00000000..a567f70e --- /dev/null +++ b/src/Colors/Rgba/Parser.php @@ -0,0 +1,55 @@ +toRgba(); + } catch (ColorException $e) { + // move on + } + + $pattern = '/^#?(?P[0-9a-f]{4}|[0-9a-f]{8})$/i'; + $result = preg_match($pattern, $input, $matches); + + if ($result !== 1) { + throw new ColorException('Unable to parse color'); + } + + $matches = match (strlen($matches['hex'])) { + 4 => str_split($matches['hex']), + 8 => str_split($matches['hex'], 2), + default => throw new ColorException('Unable to parse color'), + }; + + return new Color( + strlen($matches[0]) == '1' ? hexdec($matches[0] . $matches[0]) : hexdec($matches[0]), + strlen($matches[1]) == '1' ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]), + strlen($matches[2]) == '1' ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2]), + strlen($matches[3]) == '1' ? hexdec($matches[3] . $matches[3]) : hexdec($matches[3]), + ); + } + + public static function fromString(string $input): Color + { + $pattern = '/^rgba\((?P[0-9]{1,3}), *(?P[0-9]{1,3}), *(?P[0-9]{1,3}), *(?P((1|0))?(\.[0-9]+)?)\)$/'; + $result = preg_match($pattern, $input, $matches); + + if ($result !== 1) { + throw new ColorException('Unable to parse color'); + } + + return new Color( + $matches['r'], + $matches['g'], + $matches['b'], + intval(round(floatval($matches['a']) * 255)) + ); + } +} diff --git a/src/Drivers/Gd/Decoders/HexColorDecoder.php b/src/Drivers/Gd/Decoders/HexColorDecoder.php index 8cb6b27b..3371659a 100644 --- a/src/Drivers/Gd/Decoders/HexColorDecoder.php +++ b/src/Drivers/Gd/Decoders/HexColorDecoder.php @@ -2,12 +2,16 @@ namespace Intervention\Image\Drivers\Gd\Decoders; +use Intervention\Image\Colors\Rgb\Parser as RgbColorParser; +use Intervention\Image\Colors\Rgba\Parser as RgbaColorParser; +use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\ImageInterface; -class HexColorDecoder extends RgbArrayColorDecoder implements DecoderInterface +class HexColorDecoder extends AbstractDecoder implements DecoderInterface { public function decode($input): ImageInterface|ColorInterface { @@ -15,17 +19,18 @@ class HexColorDecoder extends RgbArrayColorDecoder implements DecoderInterface throw new DecoderException('Unable to decode input'); } - $pattern = '/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i'; - $result = preg_match($pattern, $input, $matches); - - if ($result !== 1) { - throw new DecoderException('Unable to decode input'); + try { + return RgbColorParser::fromHex($input); + } catch (ColorException $e) { + # code ... } - return parent::decode([ - strlen($matches[1]) == '1' ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]), - strlen($matches[2]) == '1' ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2]), - strlen($matches[3]) == '1' ? hexdec($matches[3] . $matches[3]) : hexdec($matches[3]), - ]); + try { + return RgbaColorParser::fromHex($input); + } catch (ColorException $e) { + # code ... + } + + throw new DecoderException('Unable to decode input'); } } diff --git a/src/Drivers/Gd/Decoders/HtmlColorNameDecoder.php b/src/Drivers/Gd/Decoders/HtmlColorNameDecoder.php index 59ed6fd2..fd1d9dce 100644 --- a/src/Drivers/Gd/Decoders/HtmlColorNameDecoder.php +++ b/src/Drivers/Gd/Decoders/HtmlColorNameDecoder.php @@ -2,27 +2,27 @@ namespace Intervention\Image\Drivers\Gd\Decoders; +use Intervention\Image\Colors\Rgb\Parser; +use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\Traits\CanReadHtmlColorNames; -class HtmlColorNameDecoder extends HexColorDecoder +class HtmlColorNameDecoder extends AbstractDecoder { - use CanReadHtmlColorNames; - public function decode($input): ImageInterface|ColorInterface { if (!is_string($input)) { throw new DecoderException('Unable to decode input'); } - $hexcolor = $this->hexColorFromColorName($input); - - if (empty($hexcolor)) { - throw new DecoderException('Unable to decode input'); + try { + return Parser::fromName($input); + } catch (ColorException $e) { + # code ... } - return parent::decode($hexcolor); + throw new DecoderException('Unable to decode input'); } } diff --git a/src/Drivers/Gd/Decoders/RgbArrayColorDecoder.php b/src/Drivers/Gd/Decoders/RgbArrayColorDecoder.php deleted file mode 100644 index 8b25f691..00000000 --- a/src/Drivers/Gd/Decoders/RgbArrayColorDecoder.php +++ /dev/null @@ -1,38 +0,0 @@ -isValidColorArray($input)) { - throw new DecoderException('Unable to decode input'); - } - - if (count($input) === 3) { - $input[] = 1; - } - - list($r, $g, $b, $a) = $input; - - return new Color( - ($this->opacityToGdAlpha($a) << 24) + ($r << 16) + ($g << 8) + $b - ); - } - - protected function opacityToGdAlpha(float $opacity): int - { - return intval(round($opacity * 127 * -1 + 127)); - } -} diff --git a/src/Drivers/Gd/Decoders/RgbStringColorDecoder.php b/src/Drivers/Gd/Decoders/RgbStringColorDecoder.php index 03c46b96..8707375e 100644 --- a/src/Drivers/Gd/Decoders/RgbStringColorDecoder.php +++ b/src/Drivers/Gd/Decoders/RgbStringColorDecoder.php @@ -2,12 +2,16 @@ namespace Intervention\Image\Drivers\Gd\Decoders; +use Intervention\Image\Colors\Rgb\Parser as RgbColorParser; +use Intervention\Image\Colors\Rgba\Parser as RgbaColorParser; +use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\ImageInterface; -class RgbStringColorDecoder extends RgbArrayColorDecoder implements DecoderInterface +class RgbStringColorDecoder extends AbstractDecoder implements DecoderInterface { public function decode($input): ImageInterface|ColorInterface { @@ -15,20 +19,16 @@ class RgbStringColorDecoder extends RgbArrayColorDecoder implements DecoderInter throw new DecoderException('Unable to decode input'); } - if (substr($input, 0, 3) !== 'rgb') { - throw new DecoderException('Unable to decode input'); + try { + return RgbColorParser::fromString($input); + } catch (ColorException $e) { + # code ... } - // rgb string like rgb(102, 200, 0) - $pattern = "/^rgb ?\((?P[0-9]{1,3}), ?(?P[0-9]{1,3}), ?(?P[0-9]{1,3})\)$/i"; - if ((bool) preg_match($pattern, $input, $matches)) { - return parent::decode([$matches['r'], $matches['g'], $matches['b']]); - } - - // rgba string like "rgba(200, 10, 30, 0.5)" - $pattern = "/^rgba ?\(((?P[0-9]{1,3})), ?((?P[0-9]{1,3})), ?((?P[0-9]{1,3})), ?(?P[0-9.]{1,4})\)$/i"; - if ((bool) preg_match($pattern, $input, $matches)) { - return parent::decode([$matches['r'], $matches['g'], $matches['b'], $matches['a']]); + try { + return RgbaColorParser::fromString($input); + } catch (ColorException $e) { + # code ... } throw new DecoderException('Unable to decode input'); diff --git a/src/Drivers/Gd/InputHandler.php b/src/Drivers/Gd/InputHandler.php index 9fec013b..42785e59 100644 --- a/src/Drivers/Gd/InputHandler.php +++ b/src/Drivers/Gd/InputHandler.php @@ -9,11 +9,10 @@ class InputHandler extends AbstractInputHandler protected $decoders = [ Decoders\ImageObjectDecoder::class, Decoders\FilePointerImageDecoder::class, - Decoders\RgbArrayColorDecoder::class, Decoders\HtmlColorNameDecoder::class, - Decoders\RgbStringColorDecoder::class, Decoders\HexColorDecoder::class, - Decoders\TransparentColorDecoder::class, + Decoders\RgbStringColorDecoder::class, + // Decoders\TransparentColorDecoder::class, Decoders\FilePathImageDecoder::class, Decoders\BinaryImageDecoder::class, Decoders\DataUriImageDecoder::class, diff --git a/src/Drivers/Imagick/Decoders/HexColorDecoder.php b/src/Drivers/Imagick/Decoders/HexColorDecoder.php index 37498320..e1e687b3 100644 --- a/src/Drivers/Imagick/Decoders/HexColorDecoder.php +++ b/src/Drivers/Imagick/Decoders/HexColorDecoder.php @@ -2,12 +2,16 @@ namespace Intervention\Image\Drivers\Imagick\Decoders; +use Intervention\Image\Colors\Rgb\Parser as RgbColorParser; +use Intervention\Image\Colors\Rgba\Parser as RgbaColorParser; +use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\ImageInterface; -class HexColorDecoder extends RgbArrayColorDecoder implements DecoderInterface +class HexColorDecoder extends AbstractDecoder implements DecoderInterface { public function decode($input): ImageInterface|ColorInterface { @@ -15,17 +19,18 @@ class HexColorDecoder extends RgbArrayColorDecoder implements DecoderInterface throw new DecoderException('Unable to decode input'); } - $pattern = '/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i'; - $result = preg_match($pattern, $input, $matches); - - if ($result !== 1) { - throw new DecoderException('Unable to decode input'); + try { + return RgbColorParser::fromHex($input); + } catch (ColorException $e) { + # code ... } - return parent::decode([ - strlen($matches[1]) == '1' ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]), - strlen($matches[2]) == '1' ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2]), - strlen($matches[3]) == '1' ? hexdec($matches[3] . $matches[3]) : hexdec($matches[3]), - ]); + try { + return RgbaColorParser::fromHex($input); + } catch (ColorException $e) { + # code ... + } + + throw new DecoderException('Unable to decode input'); } } diff --git a/src/Drivers/Imagick/Decoders/HtmlColorNameDecoder.php b/src/Drivers/Imagick/Decoders/HtmlColorNameDecoder.php index fba912ea..e8cb70a6 100644 --- a/src/Drivers/Imagick/Decoders/HtmlColorNameDecoder.php +++ b/src/Drivers/Imagick/Decoders/HtmlColorNameDecoder.php @@ -2,27 +2,26 @@ namespace Intervention\Image\Drivers\Imagick\Decoders; +use Intervention\Image\Colors\Rgb\Parser; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\Traits\CanReadHtmlColorNames; class HtmlColorNameDecoder extends HexColorDecoder { - use CanReadHtmlColorNames; - public function decode($input): ImageInterface|ColorInterface { if (!is_string($input)) { throw new DecoderException('Unable to decode input'); } - $hexcolor = $this->hexColorFromColorName($input); - - if (empty($hexcolor)) { - throw new DecoderException('Unable to decode input'); + try { + return Parser::fromName($input); + } catch (ColorException $e) { + # code ... } - return parent::decode($hexcolor); + throw new DecoderException('Unable to decode input'); } } diff --git a/src/Drivers/Imagick/Decoders/RgbArrayColorDecoder.php b/src/Drivers/Imagick/Decoders/RgbArrayColorDecoder.php deleted file mode 100644 index 2e924c87..00000000 --- a/src/Drivers/Imagick/Decoders/RgbArrayColorDecoder.php +++ /dev/null @@ -1,36 +0,0 @@ -isValidColorArray($input)) { - throw new DecoderException('Unable to decode input'); - } - - if (count($input) === 3) { - $input[] = 1; - } - - list($r, $g, $b, $a) = $input; - - $pixel = new ImagickPixel( - sprintf('rgba(%d, %d, %d, %.2F)', $r, $g, $b, $a) - ); - - return new Color($pixel); - } -} diff --git a/src/Drivers/Imagick/Decoders/RgbStringColorDecoder.php b/src/Drivers/Imagick/Decoders/RgbStringColorDecoder.php index 7b036033..cd254960 100644 --- a/src/Drivers/Imagick/Decoders/RgbStringColorDecoder.php +++ b/src/Drivers/Imagick/Decoders/RgbStringColorDecoder.php @@ -2,10 +2,10 @@ namespace Intervention\Image\Drivers\Imagick\Decoders; -use ImagickPixel; -use ImagickPixelException; +use Intervention\Image\Colors\Rgb\Parser as RgbColorParser; +use Intervention\Image\Colors\Rgba\Parser as RgbaColorParser; use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder; -use Intervention\Image\Drivers\Imagick\Color; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; @@ -19,16 +19,18 @@ class RgbStringColorDecoder extends AbstractDecoder implements DecoderInterface throw new DecoderException('Unable to decode input'); } - if (substr($input, 0, 3) !== 'rgb') { - throw new DecoderException('Unable to decode input'); + try { + return RgbColorParser::fromString($input); + } catch (ColorException $e) { + # code ... } try { - $pixel = new ImagickPixel($input); - } catch (ImagickPixelException $e) { - throw new DecoderException('Unable to decode input'); + return RgbaColorParser::fromString($input); + } catch (ColorException $e) { + # code ... } - return new Color($pixel); + throw new DecoderException('Unable to decode input'); } } diff --git a/src/Drivers/Imagick/InputHandler.php b/src/Drivers/Imagick/InputHandler.php index 8b00c35e..0fc798ff 100644 --- a/src/Drivers/Imagick/InputHandler.php +++ b/src/Drivers/Imagick/InputHandler.php @@ -9,14 +9,13 @@ class InputHandler extends AbstractInputHandler protected $decoders = [ Decoders\ImageObjectDecoder::class, Decoders\FilePointerImageDecoder::class, - Decoders\RgbArrayColorDecoder::class, - Decoders\HexColorDecoder::class, Decoders\HtmlColorNameDecoder::class, + Decoders\HexColorDecoder::class, Decoders\RgbStringColorDecoder::class, - Decoders\TransparentColorDecoder::class, + // Decoders\TransparentColorDecoder::class, Decoders\FilePathImageDecoder::class, Decoders\BinaryImageDecoder::class, Decoders\DataUriImageDecoder::class, - Decoders\Base64ImageDecoder::class + Decoders\Base64ImageDecoder::class, ]; } diff --git a/src/Traits/CanReadHtmlColorNames.php b/src/Traits/CanReadHtmlColorNames.php deleted file mode 100644 index c02b4367..00000000 --- a/src/Traits/CanReadHtmlColorNames.php +++ /dev/null @@ -1,165 +0,0 @@ - '#FFA07A', - 'salmon' => '#FA8072', - 'darksalmon' => '#E9967A', - 'lightcoral' => '#F08080', - 'indianred' => '#CD5C5C', - 'crimson' => '#DC143C', - 'firebrick' => '#B22222', - 'red' => '#FF0000', - 'darkred' => '#8B0000', - 'coral' => '#FF7F50', - 'tomato' => '#FF6347', - 'orangered' => '#FF4500', - 'gold' => '#FFD700', - 'orange' => '#FFA500', - 'darkorange' => '#FF8C00', - 'lightyellow' => '#FFFFE0', - 'lemonchiffon' => '#FFFACD', - 'lightgoldenrodyellow' => '#FAFAD2', - 'papayawhip' => '#FFEFD5', - 'moccasin' => '#FFE4B5', - 'peachpuff' => '#FFDAB9', - 'palegoldenrod' => '#EEE8AA', - 'khaki' => '#F0E68C', - 'darkkhaki' => '#BDB76B', - 'yellow' => '#FFFF00', - 'lawngreen' => '#7CFC00', - 'chartreuse' => '#7FFF00', - 'limegreen' => '#32CD32', - 'lime' => '#00FF00', - 'forestgreen' => '#228B22', - 'green' => '#008000', - 'darkgreen' => '#006400', - 'greenyellow' => '#ADFF2F', - 'yellowgreen' => '#9ACD32', - 'springgreen' => '#00FF7F', - 'mediumspringgreen' => '#00FA9A', - 'lightgreen' => '#90EE90', - 'palegreen' => '#98FB98', - 'darkseagreen' => '#8FBC8F', - 'mediumseagre' => 'en #3CB371', - 'seagreen' => '#2E8B57', - 'olive' => '#808000', - 'darkolivegreen' => '#556B2F', - 'olivedrab' => '#6B8E23', - 'lightcyan' => '#E0FFFF', - 'cyan' => '#00FFFF', - 'aqua' => '#00FFFF', - 'aquamarine' => '#7FFFD4', - 'mediumaquamarine' => '#66CDAA', - 'paleturquoise' => '#AFEEEE', - 'turquoise' => '#40E0D0', - 'mediumturquoise' => '#48D1CC', - 'darkturquoise' => '#00CED1', - 'lightseagreen' => '#20B2AA', - 'cadetblue' => '#5F9EA0', - 'darkcyan' => '#008B8B', - 'teal' => '#008080', - 'powderblue' => '#B0E0E6', - 'lightblue' => '#ADD8E6', - 'lightskyblue' => '#87CEFA', - 'skyblue' => '#87CEEB', - 'deepskyblue' => '#00BFFF', - 'lightsteelblue' => '#B0C4DE', - 'dodgerblue' => '#1E90FF', - 'cornflowerblue' => '#6495ED', - 'steelblue' => '#4682B4', - 'royalblue' => '#4169E1', - 'blue' => '#0000FF', - 'mediumblue' => '#0000CD', - 'darkblue' => '#00008B', - 'navy' => '#000080', - 'midnightblue' => '#191970', - 'mediumslateblue' => '#7B68EE', - 'slateblue' => '#6A5ACD', - 'darkslateblue' => '#483D8B', - 'lavender' => '#E6E6FA', - 'thistle' => '#D8BFD8', - 'plum' => '#DDA0DD', - 'violet' => '#EE82EE', - 'orchid' => '#DA70D6', - 'fuchsia' => '#FF00FF', - 'magenta' => '#FF00FF', - 'mediumorchid' => '#BA55D3', - 'mediumpurple' => '#9370DB', - 'blueviolet' => '#8A2BE2', - 'darkviolet' => '#9400D3', - 'darkorchid' => '#9932CC', - 'darkmagenta' => '#8B008B', - 'purple' => '#800080', - 'indigo' => '#4B0082', - 'pink' => '#FFC0CB', - 'lightpink' => '#FFB6C1', - 'hotpink' => '#FF69B4', - 'deeppink' => '#FF1493', - 'palevioletred' => '#DB7093', - 'mediumvioletred' => '#C71585', - 'white' => '#FFFFFF', - 'snow' => '#FFFAFA', - 'honeydew' => '#F0FFF0', - 'mintcream' => '#F5FFFA', - 'azure' => '#F0FFFF', - 'aliceblue' => '#F0F8FF', - 'ghostwhite' => '#F8F8FF', - 'whitesmoke' => '#F5F5F5', - 'seashell' => '#FFF5EE', - 'beige' => '#F5F5DC', - 'oldlace' => '#FDF5E6', - 'floralwhite' => '#FFFAF0', - 'ivory' => '#FFFFF0', - 'antiquewhite' => '#FAEBD7', - 'linen' => '#FAF0E6', - 'lavenderblush' => '#FFF0F5', - 'mistyrose' => '#FFE4E1', - 'gainsboro' => '#DCDCDC', - 'lightgray' => '#D3D3D3', - 'silver' => '#C0C0C0', - 'darkgray' => '#A9A9A9', - 'gray' => '#808080', - 'dimgray' => '#696969', - 'lightslategray' => '#778899', - 'slategray' => '#708090', - 'darkslategray' => '#2F4F4F', - 'black' => '#000000', - 'cornsilk' => '#FFF8DC', - 'blanchedalmond' => '#FFEBCD', - 'bisque' => '#FFE4C4', - 'navajowhite' => '#FFDEAD', - 'wheat' => '#F5DEB3', - 'burlywood' => '#DEB887', - 'tan' => '#D2B48C', - 'rosybrown' => '#BC8F8F', - 'sandybrown' => '#F4A460', - 'goldenrod' => '#DAA520', - 'peru' => '#CD853F', - 'chocolate' => '#D2691E', - 'saddlebrown' => '#8B4513', - 'sienna' => '#A0522D', - 'brown' => '#A52A2A', - 'maroon' => '#800000', - ]; - - /** - * Transform given html color name to hex color - * or return null, if color name doesn't exist. - * - * @param string $name - * @return null|string - */ - public function hexColorFromColorName(string $name): ?string - { - $name = strtolower($name); - if (!array_key_exists($name, $this->color_names)) { - return null; - } - - return $this->color_names[$name]; - } -} diff --git a/tests/Colors/Rgb/ParserTest.php b/tests/Colors/Rgb/ParserTest.php new file mode 100644 index 00000000..08389cbf --- /dev/null +++ b/tests/Colors/Rgb/ParserTest.php @@ -0,0 +1,60 @@ +assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204], $color->toArray()); + + $color = Parser::fromHex('cccccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204], $color->toArray()); + + $color = Parser::fromHex('#cccccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204], $color->toArray()); + + $this->expectException(ColorException::class); + (new Parser())->fromHex('cccccccc'); + } + + public function testFromString(): void + { + $color = Parser::fromString('rgb(204, 204, 204)'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204], $color->toArray()); + + $color = Parser::fromString('rgb(204,204,204)'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204], $color->toArray()); + + $this->expectException(ColorException::class); + (new Parser())->fromString('rgb(204,204,204,1)'); + + $this->expectException(ColorException::class); + (new Parser())->fromString('rgb(120)'); + + $this->expectException(ColorException::class); + (new Parser())->fromString('rgba(204,204,204,1)'); + } + + public function testFromName(): void + { + $color = Parser::fromName('salmon'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals('fa8072', $color->toHex()); + } +} diff --git a/tests/Colors/Rgba/ParserTest.php b/tests/Colors/Rgba/ParserTest.php new file mode 100644 index 00000000..0986c797 --- /dev/null +++ b/tests/Colors/Rgba/ParserTest.php @@ -0,0 +1,68 @@ +assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 255], $color->toArray()); + + $color = Parser::fromHex('cccccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 255], $color->toArray()); + + $color = Parser::fromHex('#cccccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 255], $color->toArray()); + + $color = Parser::fromHex('#cccccccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 204], $color->toArray()); + + $color = Parser::fromHex('#cccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 204], $color->toArray()); + + $color = Parser::fromHex('cccccccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 204], $color->toArray()); + + $color = Parser::fromHex('cccc'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 204], $color->toArray()); + } + + public function testFromString(): void + { + $color = Parser::fromString('rgba(204, 204, 204, 1)'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 255], $color->toArray()); + + $color = Parser::fromString('rgba(204,204,204,1.0)'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 255], $color->toArray()); + + $color = Parser::fromString('rgba(204,204,204,0.2)'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 51], $color->toArray()); + + $color = Parser::fromString('rgba(204,204, 204, .2)'); + $this->assertInstanceOf(Color::class, $color); + $this->assertEquals([204, 204, 204, 51], $color->toArray()); + + $this->expectException(ColorException::class); + $color = Parser::fromString('rgba(204, 204, 204, 1.2)'); + } +} diff --git a/tests/Drivers/Abstract/AbstractColorTest.php b/tests/Drivers/Abstract/AbstractColorTest.php deleted file mode 100644 index d419c157..00000000 --- a/tests/Drivers/Abstract/AbstractColorTest.php +++ /dev/null @@ -1,39 +0,0 @@ -makePartial(); - $color->shouldReceive('red')->andReturn(255); - $color->shouldReceive('green')->andReturn(0); - $color->shouldReceive('blue')->andReturn(0); - - $this->assertEquals('ff0000', $color->toHex()); - $this->assertEquals('#ff0000', $color->toHex('#')); - } - - public function testIsGreyscale(): void - { - $color = Mockery::mock(AbstractColor::class)->makePartial(); - $color->shouldReceive('red')->andReturn(255); - $color->shouldReceive('green')->andReturn(0); - $color->shouldReceive('blue')->andReturn(0); - $this->assertFalse($color->isGreyscale()); - - $color = Mockery::mock(AbstractColor::class)->makePartial(); - $color->shouldReceive('red')->andReturn(100); - $color->shouldReceive('green')->andReturn(100); - $color->shouldReceive('blue')->andReturn(100); - $this->assertTrue($color->isGreyscale()); - } -} diff --git a/tests/Drivers/Gd/ColorTest.php b/tests/Drivers/Gd/ColorTest.php deleted file mode 100644 index 3c9aa9dc..00000000 --- a/tests/Drivers/Gd/ColorTest.php +++ /dev/null @@ -1,88 +0,0 @@ -assertInstanceOf(Color::class, $this->getTestColor()); - } - - public function testRed(): void - { - $color = $this->getTestColor(255, 0, 0); - $this->assertEquals(255, $color->red()); - } - - public function testGreen(): void - { - $color = $this->getTestColor(0, 150, 0); - $this->assertEquals(150, $color->green()); - } - - public function testBlue(): void - { - $color = $this->getTestColor(0, 0, 120); - $this->assertEquals(120, $color->blue()); - } - - public function testAlpha(): void - { - $color = $this->getTestColor(0, 0, 120, 0); - $this->assertEquals(1, $color->alpha()); - - $color = $this->getTestColor(0, 0, 120, 127); - $this->assertEquals(0, $color->alpha()); - - $color = $this->getTestColor(0, 0, 120, 64); - $this->assertEquals(.5, $color->alpha()); - } - - public function testToArray(): void - { - $color = $this->getTestColor(0, 0, 120, 0); - $this->assertEquals([0, 0, 120, 1], $color->toArray()); - - $color = $this->getTestColor(0, 0, 120, 127); - $this->assertEquals([0, 0, 120, 0], $color->toArray()); - - $color = $this->getTestColor(0, 0, 120, 64); - $this->assertEquals([0, 0, 120, .5], $color->toArray()); - } - - public function testToInt(): void - { - $color = $this->getTestColor(0, 0, 0, 0); - $this->assertEquals(0, $color->toInt()); - - $color = $this->getTestColor(255, 255, 255, 0); - $this->assertEquals(16777215, $color->toInt()); - } - - public function testToHex(): void - { - $color = $this->getTestColor(181, 55, 23); - $this->assertEquals('b53717', $color->toHex()); - $this->assertEquals('#b53717', $color->toHex('#')); - - $color = $this->getTestColor(181, 55, 23, 127); - $this->assertEquals('b53717', $color->toHex()); - $this->assertEquals('#b53717', $color->toHex('#')); - } -} diff --git a/tests/Drivers/Gd/Decoders/RgbArrayColorDecoderTest.php b/tests/Drivers/Gd/Decoders/RgbArrayColorDecoderTest.php deleted file mode 100644 index 524b2f63..00000000 --- a/tests/Drivers/Gd/Decoders/RgbArrayColorDecoderTest.php +++ /dev/null @@ -1,25 +0,0 @@ -decode([181, 55, 23, .5]); - $this->assertInstanceOf(Color::class, $color); - $this->assertEquals(181, $color->red()); - $this->assertEquals(55, $color->green()); - $this->assertEquals(23, $color->blue()); - $this->assertEquals(.5, $color->alpha()); - } -} diff --git a/tests/Drivers/Gd/InputHandlerTest.php b/tests/Drivers/Gd/InputHandlerTest.php index 829fdca1..8157f01a 100644 --- a/tests/Drivers/Gd/InputHandlerTest.php +++ b/tests/Drivers/Gd/InputHandlerTest.php @@ -2,7 +2,8 @@ namespace Intervention\Image\Tests\Drivers\Gd; -use Intervention\Image\Drivers\Gd\Color; +use Intervention\Image\Colors\Rgb\Color as RgbColor; +use Intervention\Image\Colors\Rgba\Color as RgbaColor; use Intervention\Image\Drivers\Gd\Image; use Intervention\Image\Drivers\Gd\InputHandler; use Intervention\Image\Exceptions\DecoderException; @@ -12,13 +13,13 @@ use Intervention\Image\Tests\TestCase; * @requires extension gd * @covers \Intervention\Image\Drivers\Gd\InputHandler */ -class InputHandlerTest extends TestCase +class GdInputHandlerTest extends TestCase { public function testHandleEmptyString(): void { $handler = new InputHandler(); $this->expectException(DecoderException::class); - $result = $handler->handle(''); + $handler->handle(''); } public function testHandleBinaryImage(): void @@ -53,59 +54,63 @@ class InputHandlerTest extends TestCase $this->assertInstanceOf(Image::class, $result); } - public function testHandleArrayColor(): void - { - $handler = new InputHandler(); - $input = [181, 55, 23, .5]; - $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); - - $handler = new InputHandler(); - $input = [181, 55, 23]; - $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); - } - public function testHandleHexColor(): void { $handler = new InputHandler(); $input = 'ccff33'; $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals(204, $result->red()); - $this->assertEquals(255, $result->green()); - $this->assertEquals(51, $result->blue()); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([204, 255, 51], $result->toArray()); $handler = new InputHandler(); $input = 'cf3'; $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals(204, $result->red()); - $this->assertEquals(255, $result->green()); - $this->assertEquals(51, $result->blue()); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([204, 255, 51], $result->toArray()); $handler = new InputHandler(); $input = '#123456'; $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals(18, $result->red()); - $this->assertEquals(52, $result->green()); - $this->assertEquals(86, $result->blue()); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([18, 52, 86], $result->toArray()); $handler = new InputHandler(); $input = '#333'; $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); - $this->assertEquals(51, $result->red()); - $this->assertEquals(51, $result->green()); - $this->assertEquals(51, $result->blue()); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([51, 51, 51], $result->toArray()); + + $handler = new InputHandler(); + $input = '#3333'; + $result = $handler->handle($input); + $this->assertInstanceOf(RgbaColor::class, $result); + $this->assertEquals([51, 51, 51, 51], $result->toArray()); + + $handler = new InputHandler(); + $input = '#33333333'; + $result = $handler->handle($input); + $this->assertInstanceOf(RgbaColor::class, $result); + $this->assertEquals([51, 51, 51, 51], $result->toArray()); } - public function testHandleTransparent(): void + public function testHandleRgbString(): void { $handler = new InputHandler(); - $input = 'transparent'; - $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); + $result = $handler->handle('rgb(10, 20, 30)'); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([10, 20, 30], $result->toArray()); + + $handler = new InputHandler(); + $result = $handler->handle('rgba(10, 20, 30, 1.0)'); + $this->assertInstanceOf(RgbaColor::class, $result); + $this->assertEquals([10, 20, 30, 255], $result->toArray()); } + + // public function testHandleTransparent(): void + // { + // $handler = new InputHandler(); + // $input = 'transparent'; + // $result = $handler->handle($input); + // $this->assertInstanceOf(Color::class, $result); + // } } diff --git a/tests/Drivers/Imagick/ColorTest.php b/tests/Drivers/Imagick/ColorTest.php deleted file mode 100644 index 2840e98f..00000000 --- a/tests/Drivers/Imagick/ColorTest.php +++ /dev/null @@ -1,112 +0,0 @@ -assertInstanceOf(Color::class, $this->getTestColor()); - } - - public function testRed(): void - { - $color = $this->getTestColor(255, 0, 0); - $this->assertEquals(255, $color->red()); - } - - public function testGreen(): void - { - $color = $this->getTestColor(0, 150, 0); - $this->assertEquals(150, $color->green()); - } - - public function testBlue(): void - { - $color = $this->getTestColor(0, 0, 120); - $this->assertEquals(120, $color->blue()); - } - - public function testAlpha(): void - { - $color = $this->getTestColor(0, 0, 120, 1); - $this->assertEquals(1, $color->alpha()); - - $color = $this->getTestColor(0, 0, 120, 0); - $this->assertEquals(0, $color->alpha()); - - $color = $this->getTestColor(0, 0, 120, .5); - $this->assertEquals(.5, $color->alpha()); - - $color = $this->getTestColor(0, 0, 120, .57); - $this->assertEquals(.57, $color->alpha()); - - $color = $this->getTestColor(0, 0, 120, .578); - $this->assertEquals(.58, $color->alpha()); - } - - public function testToArray(): void - { - $color = $this->getTestColor(0, 0, 120, 1); - $this->assertEquals([0, 0, 120, 1], $color->toArray()); - - $color = $this->getTestColor(0, 0, 120, 0); - $this->assertEquals([0, 0, 120, 0], $color->toArray()); - - $color = $this->getTestColor(0, 0, 120, .5); - $this->assertEquals([0, 0, 120, .5], $color->toArray()); - - $color = $this->getTestColor(0, 0, 120, .57); - $this->assertEquals([0, 0, 120, .57], $color->toArray()); - - $color = $this->getTestColor(0, 0, 120, .578); - $this->assertEquals([0, 0, 120, .58], $color->toArray()); - } - - public function testToHex(): void - { - $color = $this->getTestColor(181, 55, 23); - $this->assertEquals('b53717', $color->toHex()); - $this->assertEquals('#b53717', $color->toHex('#')); - - $color = $this->getTestColor(181, 55, 23, 127); - $this->assertEquals('b53717', $color->toHex()); - $this->assertEquals('#b53717', $color->toHex('#')); - } - - public function testToInt(): void - { - $color = $this->getTestColor(255, 255, 255); - $this->assertEquals($color->toInt(), 4294967295); - - $color = $this->getTestColor(255, 255, 255, 1); - $this->assertEquals($color->toInt(), 4294967295); - - $color = $this->getTestColor(181, 55, 23, 0.2); - $this->assertEquals($color->toInt(), 867514135); - - $color = $this->getTestColor(255, 255, 255, 0.5); - $this->assertEquals($color->toInt(), 2164260863); - - $color = $this->getTestColor(181, 55, 23, 1); - $this->assertEquals($color->toInt(), 4290066199); - - $color = $this->getTestColor(0, 0, 0, 0); - $this->assertEquals($color->toInt(), 0); - } -} diff --git a/tests/Drivers/Imagick/Decoders/RgbArrayColorDecoderTest.php b/tests/Drivers/Imagick/Decoders/RgbArrayColorDecoderTest.php deleted file mode 100644 index fbb5b4e0..00000000 --- a/tests/Drivers/Imagick/Decoders/RgbArrayColorDecoderTest.php +++ /dev/null @@ -1,25 +0,0 @@ -decode([181, 55, 23, .5]); - $this->assertInstanceOf(Color::class, $color); - $this->assertEquals(181, $color->red()); - $this->assertEquals(55, $color->green()); - $this->assertEquals(23, $color->blue()); - $this->assertEquals(.5, $color->alpha()); - } -} diff --git a/tests/Drivers/Imagick/InputHandlerTest.php b/tests/Drivers/Imagick/InputHandlerTest.php index 627529ef..f125f686 100644 --- a/tests/Drivers/Imagick/InputHandlerTest.php +++ b/tests/Drivers/Imagick/InputHandlerTest.php @@ -2,7 +2,8 @@ namespace Intervention\Image\Tests\Drivers\Imagick; -use Intervention\Image\Drivers\Imagick\Color; +use Intervention\Image\Colors\Rgb\Color as RgbColor; +use Intervention\Image\Colors\Rgba\Color as RgbaColor; use Intervention\Image\Drivers\Imagick\Image; use Intervention\Image\Drivers\Imagick\InputHandler; use Intervention\Image\Exceptions\DecoderException; @@ -18,7 +19,7 @@ class InputHandlerTest extends TestCase { $handler = new InputHandler(); $this->expectException(DecoderException::class); - $result = $handler->handle(''); + $handler->handle(''); } public function testHandleBinaryImage(): void @@ -53,11 +54,55 @@ class InputHandlerTest extends TestCase $this->assertInstanceOf(Image::class, $result); } - public function testHandleArrayColor(): void + public function testHandleHexColor(): void { $handler = new InputHandler(); - $input = [181, 55, 23, .5]; + $input = 'ccff33'; $result = $handler->handle($input); - $this->assertInstanceOf(Color::class, $result); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([204, 255, 51], $result->toArray()); + + $handler = new InputHandler(); + $input = 'cf3'; + $result = $handler->handle($input); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([204, 255, 51], $result->toArray()); + + $handler = new InputHandler(); + $input = '#123456'; + $result = $handler->handle($input); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([18, 52, 86], $result->toArray()); + + $handler = new InputHandler(); + $input = '#333'; + $result = $handler->handle($input); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([51, 51, 51], $result->toArray()); + + $handler = new InputHandler(); + $input = '#3333'; + $result = $handler->handle($input); + $this->assertInstanceOf(RgbaColor::class, $result); + $this->assertEquals([51, 51, 51, 51], $result->toArray()); + + $handler = new InputHandler(); + $input = '#33333333'; + $result = $handler->handle($input); + $this->assertInstanceOf(RgbaColor::class, $result); + $this->assertEquals([51, 51, 51, 51], $result->toArray()); + } + + public function testHandleRgbString(): void + { + $handler = new InputHandler(); + $result = $handler->handle('rgb(10, 20, 30)'); + $this->assertInstanceOf(RgbColor::class, $result); + $this->assertEquals([10, 20, 30], $result->toArray()); + + $handler = new InputHandler(); + $result = $handler->handle('rgba(10, 20, 30, 1.0)'); + $this->assertInstanceOf(RgbaColor::class, $result); + $this->assertEquals([10, 20, 30, 255], $result->toArray()); } }