1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-27 15:50:09 +02:00

Replace driver color classes

This commit is contained in:
Oliver Vogel
2023-10-15 10:39:15 +02:00
parent a5cc7e2d41
commit dbbcded7a4
23 changed files with 545 additions and 638 deletions

View File

@@ -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:

193
src/Colors/Rgb/Parser.php Normal file
View File

@@ -0,0 +1,193 @@
<?php
namespace Intervention\Image\Colors\Rgb;
use Intervention\Image\Exceptions\ColorException;
class Parser
{
public static function fromHex(string $input): Color
{
$pattern = '/^#?(?P<hex>[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<r>[0-9]{1,3}), ?(?P<g>[0-9]{1,3}), ?(?P<b>[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)]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Intervention\Image\Colors\Rgba;
use Intervention\Image\Colors\Rgb\Parser as RgbParser;
use Intervention\Image\Exceptions\ColorException;
class Parser extends RgbParser
{
public static function fromHex(string $input): Color
{
try {
return parent::fromHex($input)->toRgba();
} catch (ColorException $e) {
// move on
}
$pattern = '/^#?(?P<hex>[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<r>[0-9]{1,3}), *(?P<g>[0-9]{1,3}), *(?P<b>[0-9]{1,3}), *(?P<a>((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))
);
}
}

View File

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

View File

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

View File

@@ -1,38 +0,0 @@
<?php
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanValidateColors;
class RgbArrayColorDecoder extends AbstractDecoder implements DecoderInterface
{
use CanValidateColors;
public function decode($input): ImageInterface|ColorInterface
{
if (! $this->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));
}
}

View File

@@ -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<r>[0-9]{1,3}), ?(?P<g>[0-9]{1,3}), ?(?P<b>[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<r>[0-9]{1,3})), ?((?P<g>[0-9]{1,3})), ?((?P<b>[0-9]{1,3})), ?(?P<a>[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');

View File

@@ -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,

View File

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

View File

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

View File

@@ -1,36 +0,0 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Decoders;
use ImagickPixel;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanValidateColors;
class RgbArrayColorDecoder extends AbstractDecoder implements DecoderInterface
{
use CanValidateColors;
public function decode($input): ImageInterface|ColorInterface
{
if (! $this->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);
}
}

View File

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

View File

@@ -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,
];
}

View File

@@ -1,165 +0,0 @@
<?php
namespace Intervention\Image\Traits;
trait CanReadHtmlColorNames
{
protected $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',
];
/**
* 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];
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Intervention\Image\Tests\Colors\Rgb;
use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Colors\Rgb\Parser;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Colors\Rgb\Parser
*/
class ParserTest extends TestCase
{
public function testFromHex(): void
{
$color = Parser::fromHex('ccc');
$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());
$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());
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Intervention\Image\Tests\Colors\Rgba;
use Intervention\Image\Colors\Rgba\Color;
use Intervention\Image\Colors\Rgba\Parser;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Colors\Rgba\Parser
*/
class ParserTest extends TestCase
{
public function testFromHex(): void
{
$color = Parser::fromHex('ccc');
$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('#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)');
}
}

View File

@@ -1,39 +0,0 @@
<?php
namespace Intervention\Image\Tests\Drivers\Abstract;
use Intervention\Image\Drivers\Abstract\AbstractColor;
use Intervention\Image\Tests\TestCase;
use Mockery;
/**
* @covers \Intervention\Image\Drivers\Abstract\AbstractColor
*/
class AbstractColorTest extends TestCase
{
public function testToHex(): void
{
$color = Mockery::mock(AbstractColor::class)->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());
}
}

View File

@@ -1,88 +0,0 @@
<?php
namespace Intervention\Image\Tests\Drivers\Gd;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Drivers\Gd\Color
*/
class ColorTest extends TestCase
{
protected function getTestColor($r = 0, $g = 0, $b = 0, $a = 0): Color
{
$gd = imagecreatetruecolor(1, 1);
$value = imagecolorallocatealpha($gd, $r, $g, $b, $a);
return new Color($value);
}
public function testConstructor(): void
{
$this->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('#'));
}
}

View File

@@ -1,25 +0,0 @@
<?php
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Drivers\Gd\Decoders\RgbArrayColorDecoder;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension gd
* @covers \Intervention\Image\Drivers\Gd\Decoders\RgbArrayColorDecoder
*/
class RgbArrayColorDecoderTest extends TestCase
{
public function testDecode(): void
{
$decoder = new RgbArrayColorDecoder();
$color = $decoder->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());
}
}

View File

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

View File

@@ -1,112 +0,0 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Color
*/
class ColorTest extends TestCase
{
protected function getTestColor(int $r = 0, int $g = 0, int $b = 0, float $a = 1): Color
{
return new Color(
new ImagickPixel(sprintf('rgba(%s, %s, %s, %s)', $r, $g, $b, $a))
);
}
public function testConstructor(): void
{
$this->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);
}
}

View File

@@ -1,25 +0,0 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Decoders\RgbArrayColorDecoder;
use Intervention\Image\Tests\TestCase;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Decoders\RgbArrayColorDecoder
*/
class RgbArrayColorDecoderTest extends TestCase
{
public function testDecode(): void
{
$decoder = new RgbArrayColorDecoder();
$color = $decoder->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());
}
}

View File

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