mirror of
https://github.com/Intervention/image.git
synced 2025-01-17 20:28:21 +01:00
Refactor colors & add setChannel method
This commit is contained in:
parent
8d9d3a0e12
commit
05943221b3
78
src/Colors/AbstractColor.php
Normal file
78
src/Colors/AbstractColor.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Colors;
|
||||
|
||||
use Intervention\Image\Exceptions\ColorException;
|
||||
use Intervention\Image\Interfaces\ColorChannelInterface;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||
|
||||
abstract class AbstractColor implements ColorInterface
|
||||
{
|
||||
/**
|
||||
* Color channels
|
||||
*/
|
||||
protected array $channels;
|
||||
|
||||
public function channels(): array
|
||||
{
|
||||
return $this->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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Colors\Traits;
|
||||
|
||||
use Intervention\Image\Exceptions\ColorException;
|
||||
use Intervention\Image\Interfaces\ColorChannelInterface;
|
||||
|
||||
trait CanHandleChannels
|
||||
{
|
||||
public function channels(): array
|
||||
{
|
||||
return $this->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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user