mirror of
https://github.com/Intervention/image.git
synced 2025-08-21 13:11:18 +02:00
Add color tests
This commit is contained in:
@@ -9,24 +9,26 @@ use Intervention\Image\Colors\Cmyk\Channels\Key;
|
|||||||
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
|
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
|
||||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
|
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
|
||||||
|
use Intervention\Image\Colors\Rgba\Colorspace as RgbaColorspace;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
|
use Intervention\Image\Colors\Traits\CanHandleChannels;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
|
|
||||||
class Color implements ColorInterface
|
class Color implements ColorInterface
|
||||||
{
|
{
|
||||||
protected array $channels;
|
use CanHandleChannels;
|
||||||
|
|
||||||
protected Cyan $cyan;
|
protected array $channels;
|
||||||
protected Magenta $magenta;
|
|
||||||
protected Yellow $yellow;
|
|
||||||
protected Key $key;
|
|
||||||
|
|
||||||
public function __construct(int $c, int $m, int $y, int $k)
|
public function __construct(int $c, int $m, int $y, int $k)
|
||||||
{
|
{
|
||||||
$this->cyan = new Cyan($c);
|
$this->channels = [
|
||||||
$this->magenta = new Magenta($m);
|
new Cyan($c),
|
||||||
$this->yellow = new Yellow($y);
|
new Magenta($m),
|
||||||
$this->key = new Key($k);
|
new Yellow($y),
|
||||||
|
new Key($k),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function channels(): array
|
public function channels(): array
|
||||||
@@ -36,22 +38,22 @@ class Color implements ColorInterface
|
|||||||
|
|
||||||
public function cyan(): Cyan
|
public function cyan(): Cyan
|
||||||
{
|
{
|
||||||
return $this->cyan;
|
return $this->channel(Cyan::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function magenta(): Magenta
|
public function magenta(): Magenta
|
||||||
{
|
{
|
||||||
return $this->magenta;
|
return $this->channel(Magenta::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function yellow(): Yellow
|
public function yellow(): Yellow
|
||||||
{
|
{
|
||||||
return $this->yellow;
|
return $this->channel(Yellow::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function key(): Key
|
public function key(): Key
|
||||||
{
|
{
|
||||||
return $this->key;
|
return $this->channel(Key::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
@@ -64,24 +66,29 @@ class Color implements ColorInterface
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function transformTo(string|ColorspaceInterface $colorspace): ColorInterface
|
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
|
||||||
{
|
{
|
||||||
$colorspace = match (true) {
|
$colorspace = match (true) {
|
||||||
is_object($colorspace) => $colorspace,
|
is_object($colorspace) => $colorspace,
|
||||||
default => new $colorspace(),
|
default => new $colorspace(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return $colorspace->transformColor($this);
|
return $colorspace->convertColor($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toRgb(): RgbColor
|
public function toRgb(): RgbColor
|
||||||
{
|
{
|
||||||
return $this->transformTo(RgbColorspace::class);
|
return $this->convertTo(RgbColorspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toRgba(): RgbaColor
|
||||||
|
{
|
||||||
|
return $this->convertTo(RgbaColorspace::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toCmyk(): self
|
public function toCmyk(): self
|
||||||
{
|
{
|
||||||
return $this->transformTo(CmykColorspace::class);
|
return $this->convertTo(CmykColorspace::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
|
@@ -3,16 +3,22 @@
|
|||||||
namespace Intervention\Image\Colors\Cmyk;
|
namespace Intervention\Image\Colors\Cmyk;
|
||||||
|
|
||||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
|
|
||||||
class Colorspace implements ColorspaceInterface
|
class Colorspace implements ColorspaceInterface
|
||||||
{
|
{
|
||||||
public function transformColor(ColorInterface $color): ColorInterface
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*
|
||||||
|
* @see ColorspaceInterface::convertColor()
|
||||||
|
*/
|
||||||
|
public function convertColor(ColorInterface $color): ColorInterface
|
||||||
{
|
{
|
||||||
return match (get_class($color)) {
|
return match (get_class($color)) {
|
||||||
RgbColor::class => $this->convertRgbColor($color),
|
RgbColor::class, RgbaColor::class => $this->convertRgbColor($color),
|
||||||
default => $color,
|
default => $color,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -4,15 +4,20 @@ namespace Intervention\Image\Colors\Rgb;
|
|||||||
|
|
||||||
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
|
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
|
||||||
|
use Intervention\Image\Colors\Rgba\Colorspace as RgbaColorspace;
|
||||||
use Intervention\Image\Colors\Rgb\Channels\Blue;
|
use Intervention\Image\Colors\Rgb\Channels\Blue;
|
||||||
use Intervention\Image\Colors\Rgb\Channels\Green;
|
use Intervention\Image\Colors\Rgb\Channels\Green;
|
||||||
use Intervention\Image\Colors\Rgb\Channels\Red;
|
use Intervention\Image\Colors\Rgb\Channels\Red;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
|
use Intervention\Image\Colors\Traits\CanHandleChannels;
|
||||||
use Intervention\Image\Interfaces\ColorChannelInterface;
|
use Intervention\Image\Interfaces\ColorChannelInterface;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
|
|
||||||
class Color implements ColorInterface
|
class Color implements ColorInterface
|
||||||
{
|
{
|
||||||
|
use CanHandleChannels;
|
||||||
|
|
||||||
protected array $channels;
|
protected array $channels;
|
||||||
|
|
||||||
public function __construct(int $r, int $g, int $b)
|
public function __construct(int $r, int $g, int $b)
|
||||||
@@ -24,20 +29,6 @@ class Color implements ColorInterface
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function channels(): array
|
|
||||||
{
|
|
||||||
return $this->channels;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function channel(string $classname): ColorChannelInterface
|
|
||||||
{
|
|
||||||
$channels = array_filter($this->channels(), function (ColorChannelInterface $channel) use ($classname) {
|
|
||||||
return is_a($channel, $classname);
|
|
||||||
});
|
|
||||||
|
|
||||||
return reset($channels);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function red(): Red
|
public function red(): Red
|
||||||
{
|
{
|
||||||
return $this->channel(Red::class);
|
return $this->channel(Red::class);
|
||||||
@@ -60,13 +51,6 @@ class Color implements ColorInterface
|
|||||||
}, $this->channels());
|
}, $this->channels());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function normalize(): array
|
|
||||||
{
|
|
||||||
return array_map(function (ColorChannelInterface $channel) {
|
|
||||||
return $channel->normalize();
|
|
||||||
}, $this->channels());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function toHex(string $prefix = ''): string
|
public function toHex(string $prefix = ''): string
|
||||||
{
|
{
|
||||||
return sprintf(
|
return sprintf(
|
||||||
@@ -78,29 +62,29 @@ class Color implements ColorInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toRgb(): Color
|
public function toRgb(): self
|
||||||
{
|
{
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toInt(): int
|
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
|
||||||
{
|
|
||||||
return $this->red()->value() * 256 * 256 + $this->green()->value() * 256 + $this->blue()->value();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function transformTo(string|ColorspaceInterface $colorspace): ColorInterface
|
|
||||||
{
|
{
|
||||||
$colorspace = match (true) {
|
$colorspace = match (true) {
|
||||||
is_object($colorspace) => $colorspace,
|
is_object($colorspace) => $colorspace,
|
||||||
default => new $colorspace(),
|
default => new $colorspace(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return $colorspace->transformColor($this);
|
return $colorspace->convertColor($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toCmyk(): CmykColor
|
public function toCmyk(): CmykColor
|
||||||
{
|
{
|
||||||
return $this->transformTo(CmykColorspace::class);
|
return $this->convertTo(CmykColorspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toRgba(): RgbaColor
|
||||||
|
{
|
||||||
|
return $this->convertTo(RgbaColorspace::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
|
@@ -3,15 +3,17 @@
|
|||||||
namespace Intervention\Image\Colors\Rgb;
|
namespace Intervention\Image\Colors\Rgb;
|
||||||
|
|
||||||
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
|
|
||||||
class Colorspace implements ColorspaceInterface
|
class Colorspace implements ColorspaceInterface
|
||||||
{
|
{
|
||||||
public function transformColor(ColorInterface $color): ColorInterface
|
public function convertColor(ColorInterface $color): ColorInterface
|
||||||
{
|
{
|
||||||
return match ($color) {
|
return match (get_class($color)) {
|
||||||
CmykColor::class => $this->convertCmykColor($color),
|
CmykColor::class => $this->convertCmykColor($color),
|
||||||
|
RgbaColor::class => $this->convertRgbaColor($color),
|
||||||
default => $color,
|
default => $color,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -19,9 +21,18 @@ class Colorspace implements ColorspaceInterface
|
|||||||
protected function convertCmykColor(CmykColor $color): Color
|
protected function convertCmykColor(CmykColor $color): Color
|
||||||
{
|
{
|
||||||
return new Color(
|
return new Color(
|
||||||
(int) (255 * (1 - $color->cyan()->value()) * (1 - $color->key()->value())),
|
(int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())),
|
||||||
(int) (255 * (1 - $color->magenta()->value()) * (1 - $color->key()->value())),
|
(int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())),
|
||||||
(int) (255 * (1 - $color->yellow()->value()) * (1 - $color->key()->value())),
|
(int) (255 * (1 - $color->yellow()->normalize()) * (1 - $color->key()->normalize())),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function convertRgbaColor(RgbaColor $color): Color
|
||||||
|
{
|
||||||
|
return new Color(
|
||||||
|
$color->red()->value(),
|
||||||
|
$color->green()->value(),
|
||||||
|
$color->blue()->value()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
src/Colors/Rgba/Channels/Alpha.php
Normal file
8
src/Colors/Rgba/Channels/Alpha.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||||
|
|
||||||
|
class Alpha extends Red
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
10
src/Colors/Rgba/Channels/Blue.php
Normal file
10
src/Colors/Rgba/Channels/Blue.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Rgb\Channels\Blue as RgbBlue;
|
||||||
|
|
||||||
|
class Blue extends RgbBlue
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
10
src/Colors/Rgba/Channels/Green.php
Normal file
10
src/Colors/Rgba/Channels/Green.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Rgb\Channels\Green as RgbGreen;
|
||||||
|
|
||||||
|
class Green extends RgbGreen
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
10
src/Colors/Rgba/Channels/Red.php
Normal file
10
src/Colors/Rgba/Channels/Red.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Rgb\Channels\Red as RgbRed;
|
||||||
|
|
||||||
|
class Red extends RgbRed
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
94
src/Colors/Rgba/Color.php
Normal file
94
src/Colors/Rgba/Color.php
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Rgba;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace;
|
||||||
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Blue;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Green;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Red;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Alpha;
|
||||||
|
|
||||||
|
class Color extends RgbColor
|
||||||
|
{
|
||||||
|
protected array $channels;
|
||||||
|
|
||||||
|
public function __construct(int $r, int $g, int $b, int $a)
|
||||||
|
{
|
||||||
|
$this->channels = [
|
||||||
|
new Red($r),
|
||||||
|
new Green($g),
|
||||||
|
new Blue($b),
|
||||||
|
new Alpha($a),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function red(): Red
|
||||||
|
{
|
||||||
|
return $this->channel(Red::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function green(): Green
|
||||||
|
{
|
||||||
|
return $this->channel(Green::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function blue(): Blue
|
||||||
|
{
|
||||||
|
return $this->channel(Blue::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function alpha(): Alpha
|
||||||
|
{
|
||||||
|
return $this->channel(Alpha::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isFullyOpaque(): bool
|
||||||
|
{
|
||||||
|
return $this->alpha()->value() === 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toHex(string $prefix = ''): string
|
||||||
|
{
|
||||||
|
if ($this->isFullyOpaque()) {
|
||||||
|
return parent::toHex($prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sprintf(
|
||||||
|
'%s%02x%02x%02x%02x',
|
||||||
|
$prefix,
|
||||||
|
$this->red()->value(),
|
||||||
|
$this->green()->value(),
|
||||||
|
$this->blue()->value(),
|
||||||
|
$this->alpha()->value()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toRgb(): RgbColor
|
||||||
|
{
|
||||||
|
return $this->convertTo(RgbColorspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toRgba(): self
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toCmyk(): CmykColor
|
||||||
|
{
|
||||||
|
return $this->convertTo(CmykColorspace::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
'rgba(%d, %d, %d, %.1F)',
|
||||||
|
$this->red()->value(),
|
||||||
|
$this->green()->value(),
|
||||||
|
$this->blue()->value(),
|
||||||
|
$this->alpha()->normalize(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
55
src/Colors/Rgba/Colorspace.php
Normal file
55
src/Colors/Rgba/Colorspace.php
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Rgba;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
|
|
||||||
|
class Colorspace implements ColorspaceInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function convertColor(ColorInterface $color): ColorInterface
|
||||||
|
{
|
||||||
|
return match (get_class($color)) {
|
||||||
|
CmykColor::class => $this->convertCmykColor($color),
|
||||||
|
RgbColor::class => $this->convertRgbColor($color),
|
||||||
|
default => $color,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert given color to the RGBA colorspace
|
||||||
|
*
|
||||||
|
* @param RgbColor $color
|
||||||
|
* @return Color
|
||||||
|
*/
|
||||||
|
protected function convertRgbColor(RgbColor $color): Color
|
||||||
|
{
|
||||||
|
return new Color(
|
||||||
|
$color->red()->value(),
|
||||||
|
$color->green()->value(),
|
||||||
|
$color->blue()->value(),
|
||||||
|
255
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert given color to the RGBA colorspace
|
||||||
|
*
|
||||||
|
* @param CmykColor $color
|
||||||
|
* @return Color
|
||||||
|
*/
|
||||||
|
protected function convertCmykColor(CmykColor $color): Color
|
||||||
|
{
|
||||||
|
return new Color(
|
||||||
|
(int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())),
|
||||||
|
(int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())),
|
||||||
|
(int) (255 * (1 - $color->yellow()->normalize()) * (1 - $color->key()->normalize())),
|
||||||
|
255
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
34
src/Colors/Traits/CanHandleChannels.php
Normal file
34
src/Colors/Traits/CanHandleChannels.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?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());
|
||||||
|
}
|
||||||
|
}
|
@@ -2,18 +2,20 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Interfaces;
|
namespace Intervention\Image\Interfaces;
|
||||||
|
|
||||||
use Intervention\Image\Colors\CmykColor;
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
use Intervention\Image\Colors\RgbColor;
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
|
|
||||||
interface ColorInterface
|
interface ColorInterface
|
||||||
{
|
{
|
||||||
|
// public function toRgb(): RgbColor;
|
||||||
|
// public function toRgba(): RgbaColor;
|
||||||
|
// public function toCmyk(): CmykColor;
|
||||||
|
|
||||||
// public function channels(): array;
|
// public function channels(): array;
|
||||||
// public function channel(string $classname): ColorChannelInterface;
|
// public function channel(string $classname): ColorChannelInterface;
|
||||||
// public function colorspace(): ColorspaceInterface;
|
// public function colorspace(): ColorspaceInterface;
|
||||||
// public function toRgb(): RgbColor;
|
|
||||||
// public function toRgba(): RgbColor;
|
|
||||||
// public function toCmyk(): CmykColor;
|
|
||||||
// public function toArray(): array;
|
// public function toArray(): array;
|
||||||
// public function transformTo(string|ColorspaceInterface $colorspace): ColorInterface;
|
// public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface;
|
||||||
// public function __toString(): string;
|
// public function __toString(): string;
|
||||||
}
|
}
|
||||||
|
@@ -4,5 +4,11 @@ namespace Intervention\Image\Interfaces;
|
|||||||
|
|
||||||
interface ColorspaceInterface
|
interface ColorspaceInterface
|
||||||
{
|
{
|
||||||
public function transformColor(ColorInterface $color): ColorInterface;
|
/**
|
||||||
|
* Convert given color to the format of the current colorspace
|
||||||
|
*
|
||||||
|
* @param ColorInterface $color
|
||||||
|
* @return ColorInterface
|
||||||
|
*/
|
||||||
|
public function convertColor(ColorInterface $color): ColorInterface;
|
||||||
}
|
}
|
||||||
|
94
tests/Colors/Cmyk/ColorTest.php
Normal file
94
tests/Colors/Cmyk/ColorTest.php
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Cmyk;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Channels\Cyan;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Channels\Key;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Channels\Magenta;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Channels\Yellow;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as Color;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension gd
|
||||||
|
* @covers \Intervention\Image\Colors\Cmyk\Color
|
||||||
|
*/
|
||||||
|
class ColorTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstructor(): void
|
||||||
|
{
|
||||||
|
$color = new Color(0, 0, 0, 0);
|
||||||
|
$this->assertInstanceOf(Color::class, $color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testChannels(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 40);
|
||||||
|
$this->assertIsArray($color->channels());
|
||||||
|
$this->assertCount(4, $color->channels());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testChannel(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 40);
|
||||||
|
$channel = $color->channel(Cyan::class);
|
||||||
|
$this->assertInstanceOf(Cyan::class, $channel);
|
||||||
|
$this->assertEquals(10, $channel->value());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCyanMagentaYellowKey(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 40);
|
||||||
|
$this->assertInstanceOf(Cyan::class, $color->cyan());
|
||||||
|
$this->assertInstanceOf(Magenta::class, $color->magenta());
|
||||||
|
$this->assertInstanceOf(Yellow::class, $color->yellow());
|
||||||
|
$this->assertInstanceOf(Key::class, $color->key());
|
||||||
|
$this->assertEquals(10, $color->cyan()->value());
|
||||||
|
$this->assertEquals(20, $color->magenta()->value());
|
||||||
|
$this->assertEquals(30, $color->yellow()->value());
|
||||||
|
$this->assertEquals(40, $color->key()->value());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToArray(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 40);
|
||||||
|
$this->assertEquals([10, 20, 30, 40], $color->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNormalize(): void
|
||||||
|
{
|
||||||
|
$color = new Color(100, 50, 20, 0);
|
||||||
|
$this->assertEquals([1.0, 0.5, 0.2, 0.0], $color->normalize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToString(): void
|
||||||
|
{
|
||||||
|
$color = new Color(100, 50, 20, 0);
|
||||||
|
$this->assertEquals('cmyk(100, 50, 20, 0)', (string) $color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToCmyk(): void
|
||||||
|
{
|
||||||
|
$color = new Color(0, 0, 0, 0);
|
||||||
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(Color::class, $converted);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToRgb(): void
|
||||||
|
{
|
||||||
|
$color = new Color(0, 20, 20, 0);
|
||||||
|
$converted = $color->toRgb();
|
||||||
|
$this->assertInstanceOf(RgbColor::class, $converted);
|
||||||
|
$this->assertEquals([255, 204, 204], $converted->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToRgba(): void
|
||||||
|
{
|
||||||
|
$color = new Color(0, 20, 20, 0);
|
||||||
|
$converted = $color->toRgba();
|
||||||
|
$this->assertInstanceOf(RgbaColor::class, $converted);
|
||||||
|
$this->assertEquals([255, 204, 204, 255], $converted->toArray());
|
||||||
|
}
|
||||||
|
}
|
26
tests/Colors/Cmyk/ColorspaceTest.php
Normal file
26
tests/Colors/Cmyk/ColorspaceTest.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Cmyk;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Colorspace;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension gd
|
||||||
|
* @covers \Intervention\Image\Colors\Cmyk\Colorspace
|
||||||
|
*/
|
||||||
|
class ColorspaceTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConvertColor(): void
|
||||||
|
{
|
||||||
|
$colorspace = new Colorspace();
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
CmykColor::class,
|
||||||
|
$colorspace->convertColor(
|
||||||
|
new RgbColor(0, 0, 0)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Tests\Colors\Rgb;
|
namespace Intervention\Image\Tests\Colors\Rgb;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
use Intervention\Image\Colors\Rgb\Channels\Red;
|
use Intervention\Image\Colors\Rgb\Channels\Red;
|
||||||
use Intervention\Image\Colors\Rgb\Channels\Green;
|
use Intervention\Image\Colors\Rgb\Channels\Green;
|
||||||
use Intervention\Image\Colors\Rgb\Channels\Blue;
|
use Intervention\Image\Colors\Rgb\Channels\Blue;
|
||||||
@@ -65,18 +67,6 @@ class ColorTest extends TestCase
|
|||||||
$this->assertEquals([1.0, 0.0, 0.2], $color->normalize());
|
$this->assertEquals([1.0, 0.0, 0.2], $color->normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testToInt(): void
|
|
||||||
{
|
|
||||||
$color = new Color(0, 0, 0);
|
|
||||||
$this->assertEquals(0, $color->toInt());
|
|
||||||
|
|
||||||
$color = new Color(255, 255, 255);
|
|
||||||
$this->assertEquals(16777215, $color->toInt());
|
|
||||||
|
|
||||||
$color = new Color(181, 55, 23);
|
|
||||||
$this->assertEquals(11876119, $color->toInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testToString(): void
|
public function testToString(): void
|
||||||
{
|
{
|
||||||
$color = new Color(181, 55, 23);
|
$color = new Color(181, 55, 23);
|
||||||
@@ -86,21 +76,47 @@ class ColorTest extends TestCase
|
|||||||
public function testToCmyk(): void
|
public function testToCmyk(): void
|
||||||
{
|
{
|
||||||
$color = new Color(0, 0, 0);
|
$color = new Color(0, 0, 0);
|
||||||
$this->assertEquals([0, 0, 0, 100], $color->toCmyk()->toArray());
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 0, 100], $converted->toArray());
|
||||||
|
|
||||||
$color = new Color(255, 255, 255);
|
$color = new Color(255, 255, 255);
|
||||||
$this->assertEquals([0, 0, 0, 0], $color->toCmyk()->toArray());
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 0, 0], $converted->toArray());
|
||||||
|
|
||||||
$color = new Color(255, 0, 0);
|
$color = new Color(255, 0, 0);
|
||||||
$this->assertEquals([0, 100, 100, 0], $color->toCmyk()->toArray());
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 100, 100, 0], $converted->toArray());
|
||||||
|
|
||||||
$color = new Color(255, 0, 255);
|
$color = new Color(255, 0, 255);
|
||||||
$this->assertEquals([0, 100, 0, 0], $color->toCmyk()->toArray());
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 100, 0, 0], $converted->toArray());
|
||||||
|
|
||||||
$color = new Color(255, 255, 0);
|
$color = new Color(255, 255, 0);
|
||||||
$this->assertEquals([0, 0, 100, 0], $color->toCmyk()->toArray());
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 100, 0], $converted->toArray());
|
||||||
|
|
||||||
$color = new Color(255, 204, 204);
|
$color = new Color(255, 204, 204);
|
||||||
$this->assertEquals([0, 20, 20, 0], $color->toCmyk()->toArray());
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 20, 20, 0], $converted->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToRgb(): void
|
||||||
|
{
|
||||||
|
$color = new Color(181, 55, 23);
|
||||||
|
$this->assertInstanceOf(Color::class, $color->toRgb());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToRgba(): void
|
||||||
|
{
|
||||||
|
$color = new Color(181, 55, 23);
|
||||||
|
$converted = $color->toRgba();
|
||||||
|
$this->assertInstanceOf(RgbaColor::class, $converted);
|
||||||
|
$this->assertEquals([181, 55, 23, 255], $converted->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
tests/Colors/Rgb/ColorspaceTest.php
Normal file
26
tests/Colors/Rgb/ColorspaceTest.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Rgb;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Rgb\Colorspace;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension gd
|
||||||
|
* @covers \Intervention\Image\Colors\Rgb\Colorspace
|
||||||
|
*/
|
||||||
|
class ColorspaceTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConvertColor(): void
|
||||||
|
{
|
||||||
|
$colorspace = new Colorspace();
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
RgbColor::class,
|
||||||
|
$colorspace->convertColor(
|
||||||
|
new CmykColor(0, 0, 0, 0)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
119
tests/Colors/Rgba/ColorTest.php
Normal file
119
tests/Colors/Rgba/ColorTest.php
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Rgba;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Red;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Green;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Blue;
|
||||||
|
use Intervention\Image\Colors\Rgba\Channels\Alpha;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \Intervention\Image\Colors\Rgba\Color
|
||||||
|
*/
|
||||||
|
class ColorTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstructor(): void
|
||||||
|
{
|
||||||
|
$color = new Color(0, 0, 0, 0);
|
||||||
|
$this->assertInstanceOf(Color::class, $color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testChannels(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 255);
|
||||||
|
$this->assertIsArray($color->channels());
|
||||||
|
$this->assertCount(4, $color->channels());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testChannel(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 255);
|
||||||
|
$channel = $color->channel(Red::class);
|
||||||
|
$this->assertInstanceOf(Red::class, $channel);
|
||||||
|
$this->assertEquals(10, $channel->value());
|
||||||
|
$channel = $color->channel(Alpha::class);
|
||||||
|
$this->assertInstanceOf(Alpha::class, $channel);
|
||||||
|
$this->assertEquals(255, $channel->value());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRedGreenBlueAlpha(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 255);
|
||||||
|
$this->assertInstanceOf(Red::class, $color->red());
|
||||||
|
$this->assertInstanceOf(Green::class, $color->green());
|
||||||
|
$this->assertInstanceOf(Blue::class, $color->blue());
|
||||||
|
$this->assertInstanceOf(Alpha::class, $color->alpha());
|
||||||
|
$this->assertEquals(10, $color->red()->value());
|
||||||
|
$this->assertEquals(20, $color->green()->value());
|
||||||
|
$this->assertEquals(30, $color->blue()->value());
|
||||||
|
$this->assertEquals(255, $color->alpha()->value());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToArray(): void
|
||||||
|
{
|
||||||
|
$color = new Color(10, 20, 30, 255);
|
||||||
|
$this->assertEquals([10, 20, 30, 255], $color->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToHex(): void
|
||||||
|
{
|
||||||
|
$color = new Color(181, 55, 23, 0);
|
||||||
|
$this->assertEquals('b5371700', $color->toHex());
|
||||||
|
$this->assertEquals('#b5371700', $color->toHex('#'));
|
||||||
|
|
||||||
|
$color = new Color(181, 55, 23, 255);
|
||||||
|
$this->assertEquals('b53717', $color->toHex());
|
||||||
|
|
||||||
|
$color = new Color(181, 55, 23, 204);
|
||||||
|
$this->assertEquals('b53717cc', $color->toHex());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNormalize(): void
|
||||||
|
{
|
||||||
|
$color = new Color(255, 0, 51, 255);
|
||||||
|
$this->assertEquals([1.0, 0.0, 0.2, 1.0], $color->normalize());
|
||||||
|
$color = new Color(255, 0, 51, 51);
|
||||||
|
$this->assertEquals([1.0, 0.0, 0.2, 0.2], $color->normalize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToString(): void
|
||||||
|
{
|
||||||
|
$color = new Color(255, 255, 255, 255);
|
||||||
|
$this->assertEquals('rgba(255, 255, 255, 1.0)', (string) $color);
|
||||||
|
|
||||||
|
$color = new Color(10, 20, 30, 85);
|
||||||
|
$this->assertEquals('rgba(10, 20, 30, 0.3)', (string) $color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToRgba(): void
|
||||||
|
{
|
||||||
|
$color = new Color(181, 55, 23, 120);
|
||||||
|
$converted = $color->toRgba();
|
||||||
|
$this->assertInstanceOf(Color::class, $converted);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToRgb(): void
|
||||||
|
{
|
||||||
|
$color = new Color(181, 55, 23, 120);
|
||||||
|
$converted = $color->toRgb();
|
||||||
|
$this->assertInstanceOf(RgbColor::class, $converted);
|
||||||
|
$this->assertEquals([181, 55, 23], $converted->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToCmyk(): void
|
||||||
|
{
|
||||||
|
$color = new Color(0, 0, 0, 255);
|
||||||
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 0, 100], $converted->toArray());
|
||||||
|
|
||||||
|
$color = new Color(0, 0, 0, 0);
|
||||||
|
$converted = $color->toCmyk();
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 0, 100], $converted->toArray());
|
||||||
|
}
|
||||||
|
}
|
26
tests/Colors/Rgba/ColorspaceTest.php
Normal file
26
tests/Colors/Rgba/ColorspaceTest.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Rgba;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||||
|
use Intervention\Image\Colors\Rgba\Colorspace;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension gd
|
||||||
|
* @covers \Intervention\Image\Colors\Rgba\Colorspace
|
||||||
|
*/
|
||||||
|
class ColorspaceTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConvertColor(): void
|
||||||
|
{
|
||||||
|
$colorspace = new Colorspace();
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
RgbaColor::class,
|
||||||
|
$colorspace->convertColor(
|
||||||
|
new CmykColor(0, 0, 0, 0)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user