diff --git a/src/Colors/AbstractColor.php b/src/Colors/AbstractColor.php new file mode 100644 index 00000000..49d1dd2a --- /dev/null +++ b/src/Colors/AbstractColor.php @@ -0,0 +1,78 @@ +channels; + } + + public function channel(string $classname): ColorChannelInterface + { + $channels = array_filter($this->channels(), function (ColorChannelInterface $channel) use ($classname) { + return get_class($channel) == $classname; + }); + + if (count($channels) == 0) { + throw new ColorException('Channel ' . $classname . ' could not be found.'); + } + + return reset($channels); + } + + public function normalize(): array + { + return array_map(function (ColorChannelInterface $channel) { + return $channel->normalize(); + }, $this->channels()); + } + + /** + * {@inheritdoc} + * + * @see ColorInterface::toArray() + */ + public function toArray(): array + { + return array_map(function (ColorChannelInterface $channel) { + return $channel->value(); + }, $this->channels()); + } + + /** + * {@inheritdoc} + * + * @see ColorInterface::convertTo() + */ + public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface + { + $colorspace = match (true) { + is_object($colorspace) => $colorspace, + default => new $colorspace(), + }; + + return $colorspace->importColor($this); + } + + /** + * {@inheritdoc} + * + * @see ColorInterface::__toString() + */ + public function __toString(): string + { + return $this->toString(); + } +} diff --git a/src/Colors/Cmyk/Color.php b/src/Colors/Cmyk/Color.php index 5f46d13d..517e0bb6 100644 --- a/src/Colors/Cmyk/Color.php +++ b/src/Colors/Cmyk/Color.php @@ -2,23 +2,19 @@ namespace Intervention\Image\Colors\Cmyk; +use Intervention\Image\Colors\AbstractColor; use Intervention\Image\Colors\Cmyk\Channels\Cyan; use Intervention\Image\Colors\Cmyk\Channels\Magenta; use Intervention\Image\Colors\Cmyk\Channels\Yellow; use Intervention\Image\Colors\Cmyk\Channels\Key; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; -use Intervention\Image\Colors\Traits\CanHandleChannels; use Intervention\Image\Drivers\AbstractInputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; -class Color implements ColorInterface +class Color extends AbstractColor { - use CanHandleChannels; - - protected array $channels; - public function __construct(int $c, int $m, int $y, int $k) { $this->channels = [ @@ -48,11 +44,6 @@ class Color implements ColorInterface return $this->convertTo(RgbColorspace::class)->toHex($prefix); } - public function channels(): array - { - return $this->channels; - } - public function cyan(): ColorChannelInterface { return $this->channel(Cyan::class); @@ -73,26 +64,6 @@ class Color implements ColorInterface return $this->channel(Key::class); } - public function toArray(): array - { - return [ - $this->cyan()->value(), - $this->magenta()->value(), - $this->yellow()->value(), - $this->key()->value(), - ]; - } - - public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface - { - $colorspace = match (true) { - is_object($colorspace) => $colorspace, - default => new $colorspace(), - }; - - return $colorspace->importColor($this); - } - public function toString(): string { return sprintf( @@ -112,9 +83,4 @@ class Color implements ColorInterface $this->yellow()->value(), ]); } - - public function __toString(): string - { - return $this->toString(); - } } diff --git a/src/Colors/Hsl/Color.php b/src/Colors/Hsl/Color.php index 57858db5..674980f4 100644 --- a/src/Colors/Hsl/Color.php +++ b/src/Colors/Hsl/Color.php @@ -2,25 +2,18 @@ namespace Intervention\Image\Colors\Hsl; +use Intervention\Image\Colors\AbstractColor; use Intervention\Image\Colors\Hsl\Channels\Hue; use Intervention\Image\Colors\Hsl\Channels\Luminance; use Intervention\Image\Colors\Hsl\Channels\Saturation; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; -use Intervention\Image\Colors\Traits\CanHandleChannels; use Intervention\Image\Drivers\AbstractInputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; -class Color implements ColorInterface +class Color extends AbstractColor { - use CanHandleChannels; - - /** - * Color channels - */ - protected array $channels; - public function __construct(int $h, int $s, int $l) { $this->channels = [ @@ -79,33 +72,6 @@ class Color implements ColorInterface return $this->channel(Luminance::class); } - /** - * {@inheritdoc} - * - * @see ColorInterface::toArray() - */ - public function toArray(): array - { - return array_map(function (ColorChannelInterface $channel) { - return $channel->value(); - }, $this->channels()); - } - - /** - * {@inheritdoc} - * - * @see ColorInterface::convertTo() - */ - public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface - { - $colorspace = match (true) { - is_object($colorspace) => $colorspace, - default => new $colorspace(), - }; - - return $colorspace->importColor($this); - } - public function toHex(string $prefix = ''): string { return $this->convertTo(RgbColorspace::class)->toHex($prefix); @@ -135,14 +101,4 @@ class Color implements ColorInterface { return $this->saturation()->value() == 0; } - - /** - * {@inheritdoc} - * - * @see ColorInterface::__toString() - */ - public function __toString(): string - { - return $this->toString(); - } } diff --git a/src/Colors/Hsv/Color.php b/src/Colors/Hsv/Color.php index 29c51da0..251b7570 100644 --- a/src/Colors/Hsv/Color.php +++ b/src/Colors/Hsv/Color.php @@ -2,25 +2,18 @@ namespace Intervention\Image\Colors\Hsv; +use Intervention\Image\Colors\AbstractColor; use Intervention\Image\Colors\Hsv\Channels\Hue; use Intervention\Image\Colors\Hsv\Channels\Saturation; use Intervention\Image\Colors\Hsv\Channels\Value; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; -use Intervention\Image\Colors\Traits\CanHandleChannels; use Intervention\Image\Drivers\AbstractInputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; -class Color implements ColorInterface +class Color extends AbstractColor { - use CanHandleChannels; - - /** - * Color channels - */ - protected array $channels; - public function __construct(int $h, int $s, int $v) { $this->channels = [ @@ -79,33 +72,6 @@ class Color implements ColorInterface return $this->channel(Value::class); } - /** - * {@inheritdoc} - * - * @see ColorInterface::toArray() - */ - public function toArray(): array - { - return array_map(function (ColorChannelInterface $channel) { - return $channel->value(); - }, $this->channels()); - } - - /** - * {@inheritdoc} - * - * @see ColorInterface::convertTo() - */ - public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface - { - $colorspace = match (true) { - is_object($colorspace) => $colorspace, - default => new $colorspace(), - }; - - return $colorspace->importColor($this); - } - public function toHex(string $prefix = ''): string { return $this->convertTo(RgbColorspace::class)->toHex($prefix); @@ -135,14 +101,4 @@ class Color implements ColorInterface { return $this->saturation()->value() == 0; } - - /** - * {@inheritdoc} - * - * @see ColorInterface::__toString() - */ - public function __toString(): string - { - return $this->toString(); - } } diff --git a/src/Colors/Rgb/Color.php b/src/Colors/Rgb/Color.php index b479be6d..7de9e7b9 100644 --- a/src/Colors/Rgb/Color.php +++ b/src/Colors/Rgb/Color.php @@ -2,25 +2,18 @@ namespace Intervention\Image\Colors\Rgb; +use Intervention\Image\Colors\AbstractColor; use Intervention\Image\Colors\Rgb\Channels\Blue; use Intervention\Image\Colors\Rgb\Channels\Green; use Intervention\Image\Colors\Rgb\Channels\Red; use Intervention\Image\Colors\Rgb\Channels\Alpha; -use Intervention\Image\Colors\Traits\CanHandleChannels; use Intervention\Image\Drivers\AbstractInputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; -class Color implements ColorInterface +class Color extends AbstractColor { - use CanHandleChannels; - - /** - * Color channels - */ - protected array $channels; - /** * Create new instance * @@ -102,18 +95,6 @@ class Color implements ColorInterface return $this->channel(Alpha::class); } - /** - * {@inheritdoc} - * - * @see ColorInterface::toArray() - */ - public function toArray(): array - { - return array_map(function (ColorChannelInterface $channel) { - return $channel->value(); - }, $this->channels()); - } - /** * {@inheritdoc} * @@ -141,21 +122,6 @@ class Color implements ColorInterface ); } - /** - * {@inheritdoc} - * - * @see ColorInterface::convertTo() - */ - public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface - { - $colorspace = match (true) { - is_object($colorspace) => $colorspace, - default => new $colorspace(), - }; - - return $colorspace->importColor($this); - } - public function isFullyOpaque(): bool { return $this->alpha()->value() === 255; @@ -197,14 +163,4 @@ class Color implements ColorInterface return count(array_unique($values, SORT_REGULAR)) === 1; } - - /** - * {@inheritdoc} - * - * @see ColorInterface::__toString() - */ - public function __toString(): string - { - return $this->toString(); - } } diff --git a/src/Colors/Traits/CanHandleChannels.php b/src/Colors/Traits/CanHandleChannels.php deleted file mode 100644 index bc231139..00000000 --- a/src/Colors/Traits/CanHandleChannels.php +++ /dev/null @@ -1,34 +0,0 @@ -channels; - } - - public function channel(string $classname): ColorChannelInterface - { - $channels = array_filter($this->channels(), function (ColorChannelInterface $channel) use ($classname) { - return get_class($channel) == $classname; - }); - - if (count($channels) == 0) { - throw new ColorException('Channel ' . $classname . ' could not be found.'); - } - - return reset($channels); - } - - public function normalize(): array - { - return array_map(function (ColorChannelInterface $channel) { - return $channel->normalize(); - }, $this->channels()); - } -}