mirror of
https://github.com/Intervention/image.git
synced 2025-08-17 19:26:25 +02:00
55
src/Colors/Cmyk/Channels/Cyan.php
Normal file
55
src/Colors/Cmyk/Channels/Cyan.php
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Cmyk\Channels;
|
||||||
|
|
||||||
|
use Intervention\Image\Exceptions\ColorException;
|
||||||
|
use Intervention\Image\Interfaces\ColorChannelInterface;
|
||||||
|
|
||||||
|
class Cyan implements ColorChannelInterface
|
||||||
|
{
|
||||||
|
protected int $value;
|
||||||
|
|
||||||
|
public function __construct(int $value)
|
||||||
|
{
|
||||||
|
$this->value = $this->validate($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function value(): int
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function normalize($precision = 32): float
|
||||||
|
{
|
||||||
|
return round($this->value() / $this->max(), $precision);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function min(): int
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function max(): int
|
||||||
|
{
|
||||||
|
return 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validate(mixed $value): mixed
|
||||||
|
{
|
||||||
|
if ($value < $this->min() || $value > $this->max()) {
|
||||||
|
throw new ColorException('CMYK color values must be in range 0-100.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return (string) $this->value();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->toString();
|
||||||
|
}
|
||||||
|
}
|
8
src/Colors/Cmyk/Channels/Key.php
Normal file
8
src/Colors/Cmyk/Channels/Key.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Cmyk\Channels;
|
||||||
|
|
||||||
|
class Key extends Cyan
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
8
src/Colors/Cmyk/Channels/Magenta.php
Normal file
8
src/Colors/Cmyk/Channels/Magenta.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Cmyk\Channels;
|
||||||
|
|
||||||
|
class Magenta extends Cyan
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
8
src/Colors/Cmyk/Channels/Yellow.php
Normal file
8
src/Colors/Cmyk/Channels/Yellow.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Cmyk\Channels;
|
||||||
|
|
||||||
|
class Yellow extends Cyan
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
115
src/Colors/Cmyk/Color.php
Normal file
115
src/Colors/Cmyk/Color.php
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Cmyk;
|
||||||
|
|
||||||
|
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\Abstract\AbstractInputHandler;
|
||||||
|
use Intervention\Image\Interfaces\ColorChannelInterface;
|
||||||
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
|
|
||||||
|
class Color implements ColorInterface
|
||||||
|
{
|
||||||
|
use CanHandleChannels;
|
||||||
|
|
||||||
|
protected array $channels;
|
||||||
|
|
||||||
|
public function __construct(int $c, int $m, int $y, int $k)
|
||||||
|
{
|
||||||
|
$this->channels = [
|
||||||
|
new Cyan($c),
|
||||||
|
new Magenta($m),
|
||||||
|
new Yellow($y),
|
||||||
|
new Key($k),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function create(mixed $input): ColorInterface
|
||||||
|
{
|
||||||
|
return (new class ([
|
||||||
|
Decoders\StringColorDecoder::class,
|
||||||
|
]) extends AbstractInputHandler
|
||||||
|
{
|
||||||
|
})->handle($input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toHex(): string
|
||||||
|
{
|
||||||
|
return $this->convertTo(RgbColorspace::class)->toHex();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function channels(): array
|
||||||
|
{
|
||||||
|
return $this->channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cyan(): ColorChannelInterface
|
||||||
|
{
|
||||||
|
return $this->channel(Cyan::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function magenta(): ColorChannelInterface
|
||||||
|
{
|
||||||
|
return $this->channel(Magenta::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function yellow(): ColorChannelInterface
|
||||||
|
{
|
||||||
|
return $this->channel(Yellow::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function key(): ColorChannelInterface
|
||||||
|
{
|
||||||
|
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->convertColor($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf(
|
||||||
|
'cmyk(%d, %d, %d, %d)',
|
||||||
|
$this->cyan()->value(),
|
||||||
|
$this->magenta()->value(),
|
||||||
|
$this->yellow()->value(),
|
||||||
|
$this->key()->value()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isGreyscale(): bool
|
||||||
|
{
|
||||||
|
return 0 === array_sum([
|
||||||
|
$this->cyan()->value(),
|
||||||
|
$this->magenta()->value(),
|
||||||
|
$this->yellow()->value(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->toString();
|
||||||
|
}
|
||||||
|
}
|
38
src/Colors/Cmyk/Colorspace.php
Normal file
38
src/Colors/Cmyk/Colorspace.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Cmyk;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||||
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
|
|
||||||
|
class Colorspace implements ColorspaceInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*
|
||||||
|
* @see ColorspaceInterface::convertColor()
|
||||||
|
*/
|
||||||
|
public function convertColor(ColorInterface $color): ColorInterface
|
||||||
|
{
|
||||||
|
return match (get_class($color)) {
|
||||||
|
RgbColor::class => $this->convertRgbColor($color),
|
||||||
|
default => $color,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function convertRgbColor(RgbColor $color): CmykColor
|
||||||
|
{
|
||||||
|
$c = (255 - $color->red()->value()) / 255.0 * 100;
|
||||||
|
$m = (255 - $color->green()->value()) / 255.0 * 100;
|
||||||
|
$y = (255 - $color->blue()->value()) / 255.0 * 100;
|
||||||
|
$k = intval(round(min([$c, $m, $y])));
|
||||||
|
|
||||||
|
$c = intval(round($c - $k));
|
||||||
|
$m = intval(round($m - $k));
|
||||||
|
$y = intval(round($y - $k));
|
||||||
|
|
||||||
|
return new CmykColor($c, $m, $y, $k);
|
||||||
|
}
|
||||||
|
}
|
37
src/Colors/Cmyk/Decoders/StringColorDecoder.php
Normal file
37
src/Colors/Cmyk/Decoders/StringColorDecoder.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Colors\Cmyk\Decoders;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color;
|
||||||
|
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
|
||||||
|
use Intervention\Image\Exceptions\DecoderException;
|
||||||
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
|
use Intervention\Image\Interfaces\DecoderInterface;
|
||||||
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
|
||||||
|
class StringColorDecoder extends AbstractDecoder implements DecoderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Decode CMYK color strings
|
||||||
|
*
|
||||||
|
* @param mixed $input
|
||||||
|
* @return ImageInterface|ColorInterface
|
||||||
|
*/
|
||||||
|
public function decode($input): ImageInterface|ColorInterface
|
||||||
|
{
|
||||||
|
if (! is_string($input)) {
|
||||||
|
throw new DecoderException('Unable to decode input');
|
||||||
|
}
|
||||||
|
|
||||||
|
$pattern = '/^cmyk\((?P<c>[0-9\.]+%?), ?(?P<m>[0-9\.]+%?), ?(?P<y>[0-9\.]+%?), ?(?P<k>[0-9\.]+%?)\)$/i';
|
||||||
|
if (preg_match($pattern, $input, $matches) != 1) {
|
||||||
|
throw new DecoderException('Unable to decode input');
|
||||||
|
}
|
||||||
|
|
||||||
|
$values = array_map(function ($value) {
|
||||||
|
return intval(round(floatval(trim(str_replace('%', '', $value)))));
|
||||||
|
}, [$matches['c'], $matches['m'], $matches['y'], $matches['k']]);
|
||||||
|
|
||||||
|
return new Color(...$values);
|
||||||
|
}
|
||||||
|
}
|
@@ -151,11 +151,6 @@ class Color implements ColorInterface
|
|||||||
return $colorspace->convertColor($this);
|
return $colorspace->convertColor($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the current color is fully opaque
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isFullyOpaque(): bool
|
public function isFullyOpaque(): bool
|
||||||
{
|
{
|
||||||
return $this->alpha()->value() === 255;
|
return $this->alpha()->value() === 255;
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Colors\Rgb;
|
namespace Intervention\Image\Colors\Rgb;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
@@ -10,7 +11,17 @@ class Colorspace implements ColorspaceInterface
|
|||||||
public function convertColor(ColorInterface $color): ColorInterface
|
public function convertColor(ColorInterface $color): ColorInterface
|
||||||
{
|
{
|
||||||
return match (get_class($color)) {
|
return match (get_class($color)) {
|
||||||
|
CmykColor::class => $this->convertCmykColor($color),
|
||||||
default => $color,
|
default => $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())),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Drivers\Gd;
|
namespace Intervention\Image\Drivers\Gd;
|
||||||
|
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder as RgbHexColorDecoder;
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder as RgbStringColorDecoder;
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\HtmlColornameDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\HtmlColornameDecoder;
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\TransparentColorDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\TransparentColorDecoder;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Decoders\StringColorDecoder as CmykStringColorDecoder;
|
||||||
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
|
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
|
||||||
use Intervention\Image\Drivers\Gd\Decoders\ImageObjectDecoder;
|
use Intervention\Image\Drivers\Gd\Decoders\ImageObjectDecoder;
|
||||||
use Intervention\Image\Drivers\Gd\Decoders\ColorObjectDecoder;
|
use Intervention\Image\Drivers\Gd\Decoders\ColorObjectDecoder;
|
||||||
@@ -20,8 +21,9 @@ class InputHandler extends AbstractInputHandler
|
|||||||
protected array $decoders = [
|
protected array $decoders = [
|
||||||
ImageObjectDecoder::class,
|
ImageObjectDecoder::class,
|
||||||
ColorObjectDecoder::class,
|
ColorObjectDecoder::class,
|
||||||
HexColorDecoder::class,
|
RgbHexColorDecoder::class,
|
||||||
StringColorDecoder::class,
|
RgbStringColorDecoder::class,
|
||||||
|
CmykStringColorDecoder::class,
|
||||||
TransparentColorDecoder::class,
|
TransparentColorDecoder::class,
|
||||||
HtmlColornameDecoder::class,
|
HtmlColornameDecoder::class,
|
||||||
FilePointerImageDecoder::class,
|
FilePointerImageDecoder::class,
|
||||||
|
@@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Drivers\Imagick;
|
namespace Intervention\Image\Drivers\Imagick;
|
||||||
|
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder as RgbHexColorDecoder;
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder as RgbStringColorDecoder;
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\HtmlColornameDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\HtmlColornameDecoder;
|
||||||
use Intervention\Image\Colors\Rgb\Decoders\TransparentColorDecoder;
|
use Intervention\Image\Colors\Rgb\Decoders\TransparentColorDecoder;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Decoders\StringColorDecoder as CmykStringColorDecoder;
|
||||||
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
|
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
|
||||||
use Intervention\Image\Drivers\Imagick\Decoders\ImageObjectDecoder;
|
use Intervention\Image\Drivers\Imagick\Decoders\ImageObjectDecoder;
|
||||||
use Intervention\Image\Drivers\Imagick\Decoders\ColorObjectDecoder;
|
use Intervention\Image\Drivers\Imagick\Decoders\ColorObjectDecoder;
|
||||||
@@ -20,8 +21,9 @@ class InputHandler extends AbstractInputHandler
|
|||||||
protected array $decoders = [
|
protected array $decoders = [
|
||||||
ImageObjectDecoder::class,
|
ImageObjectDecoder::class,
|
||||||
ColorObjectDecoder::class,
|
ColorObjectDecoder::class,
|
||||||
HexColorDecoder::class,
|
RgbHexColorDecoder::class,
|
||||||
StringColorDecoder::class,
|
RgbStringColorDecoder::class,
|
||||||
|
CmykStringColorDecoder::class,
|
||||||
TransparentColorDecoder::class,
|
TransparentColorDecoder::class,
|
||||||
HtmlColornameDecoder::class,
|
HtmlColornameDecoder::class,
|
||||||
FilePointerImageDecoder::class,
|
FilePointerImageDecoder::class,
|
||||||
|
48
tests/Colors/Cmyk/ChannelTest.php
Normal file
48
tests/Colors/Cmyk/ChannelTest.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Cmyk;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Channels\Cyan as Channel;
|
||||||
|
use Intervention\Image\Exceptions\ColorException;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension gd
|
||||||
|
* @covers \Intervention\Image\Colors\Cmyk\Channels\Cyan
|
||||||
|
* @covers \Intervention\Image\Colors\Cmyk\Channels\Magenta
|
||||||
|
* @covers \Intervention\Image\Colors\Cmyk\Channels\Yellow
|
||||||
|
* @covers \Intervention\Image\Colors\Cmyk\Channels\Key
|
||||||
|
*/
|
||||||
|
class ChannelTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstructor(): void
|
||||||
|
{
|
||||||
|
$channel = new Channel(0);
|
||||||
|
$this->assertInstanceOf(Channel::class, $channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValue(): void
|
||||||
|
{
|
||||||
|
$channel = new Channel(10);
|
||||||
|
$this->assertEquals(10, $channel->value());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNormalize(): void
|
||||||
|
{
|
||||||
|
$channel = new Channel(100);
|
||||||
|
$this->assertEquals(1, $channel->normalize());
|
||||||
|
$channel = new Channel(0);
|
||||||
|
$this->assertEquals(0, $channel->normalize());
|
||||||
|
$channel = new Channel(20);
|
||||||
|
$this->assertEquals(.2, $channel->normalize());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidate(): void
|
||||||
|
{
|
||||||
|
$this->expectException(ColorException::class);
|
||||||
|
new Channel(101);
|
||||||
|
|
||||||
|
$this->expectException(ColorException::class);
|
||||||
|
new Channel(-1);
|
||||||
|
}
|
||||||
|
}
|
69
tests/Colors/Cmyk/ColorTest.php
Normal file
69
tests/Colors/Cmyk/ColorTest.php
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Cmyk;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
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)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
35
tests/Colors/Cmyk/Decoders/StringColorDecoderTest.php
Normal file
35
tests/Colors/Cmyk/Decoders/StringColorDecoderTest.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Tests\Colors\Cmyk\Decoders;
|
||||||
|
|
||||||
|
use Intervention\Image\Colors\Cmyk\Color;
|
||||||
|
use Intervention\Image\Colors\Cmyk\Decoders\StringColorDecoder;
|
||||||
|
use Intervention\Image\Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires extension gd
|
||||||
|
* @covers \Intervention\Image\Colors\Cmyk\Decoders\StringColorDecoder
|
||||||
|
*/
|
||||||
|
class StringColorDecoderTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testDecode(): void
|
||||||
|
{
|
||||||
|
$decoder = new StringColorDecoder();
|
||||||
|
$result = $decoder->decode('cmyk(0,0,0,0)');
|
||||||
|
$this->assertInstanceOf(Color::class, $result);
|
||||||
|
$this->assertEquals([0, 0, 0, 0], $result->toArray());
|
||||||
|
|
||||||
|
$result = $decoder->decode('cmyk(0, 100, 100, 0)');
|
||||||
|
$this->assertInstanceOf(Color::class, $result);
|
||||||
|
$this->assertEquals([0, 100, 100, 0], $result->toArray());
|
||||||
|
|
||||||
|
$result = $decoder->decode('cmyk(0, 100, 100, 0)');
|
||||||
|
$this->assertInstanceOf(Color::class, $result);
|
||||||
|
$this->assertEquals([0, 100, 100, 0], $result->toArray());
|
||||||
|
|
||||||
|
|
||||||
|
$result = $decoder->decode('cmyk(0%, 100%, 100%, 0%)');
|
||||||
|
$this->assertInstanceOf(Color::class, $result);
|
||||||
|
$this->assertEquals([0, 100, 100, 0], $result->toArray());
|
||||||
|
}
|
||||||
|
}
|
@@ -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\Cmyk\Colorspace as CmykColorspace;
|
||||||
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;
|
||||||
@@ -84,4 +86,37 @@ class ColorTest extends TestCase
|
|||||||
$color = new Color(181, 55, 23);
|
$color = new Color(181, 55, 23);
|
||||||
$this->assertEquals('rgb(181, 55, 23)', (string) $color);
|
$this->assertEquals('rgb(181, 55, 23)', (string) $color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testConvertTo(): void
|
||||||
|
{
|
||||||
|
$color = new Color(0, 0, 0);
|
||||||
|
$converted = $color->convertTo(CmykColorspace::class);
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 0, 100], $converted->toArray());
|
||||||
|
|
||||||
|
$color = new Color(255, 255, 255);
|
||||||
|
$converted = $color->convertTo(CmykColorspace::class);
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 0, 0], $converted->toArray());
|
||||||
|
|
||||||
|
$color = new Color(255, 0, 0);
|
||||||
|
$converted = $color->convertTo(CmykColorspace::class);
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 100, 100, 0], $converted->toArray());
|
||||||
|
|
||||||
|
$color = new Color(255, 0, 255);
|
||||||
|
$converted = $color->convertTo(CmykColorspace::class);
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 100, 0, 0], $converted->toArray());
|
||||||
|
|
||||||
|
$color = new Color(255, 255, 0);
|
||||||
|
$converted = $color->convertTo(CmykColorspace::class);
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 0, 100, 0], $converted->toArray());
|
||||||
|
|
||||||
|
$color = new Color(255, 204, 204);
|
||||||
|
$converted = $color->convertTo(CmykColorspace::class);
|
||||||
|
$this->assertInstanceOf(CmykColor::class, $converted);
|
||||||
|
$this->assertEquals([0, 20, 20, 0], $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)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Tests\Drivers\Gd;
|
namespace Intervention\Image\Tests\Drivers\Gd;
|
||||||
|
|
||||||
use Intervention\Image\Colors\Rgb\Color;
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
use Intervention\Image\Drivers\Gd\Image;
|
use Intervention\Image\Drivers\Gd\Image;
|
||||||
use Intervention\Image\Drivers\Gd\InputHandler;
|
use Intervention\Image\Drivers\Gd\InputHandler;
|
||||||
use Intervention\Image\Exceptions\DecoderException;
|
use Intervention\Image\Exceptions\DecoderException;
|
||||||
@@ -58,37 +58,37 @@ class GdInputHandlerTest extends TestCase
|
|||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = 'ccff33';
|
$input = 'ccff33';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = 'cf3';
|
$input = 'cf3';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#123456';
|
$input = '#123456';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([18, 52, 86, 255], $result->toArray());
|
$this->assertEquals([18, 52, 86, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#333';
|
$input = '#333';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([51, 51, 51, 255], $result->toArray());
|
$this->assertEquals([51, 51, 51, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#3333';
|
$input = '#3333';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#33333333';
|
$input = '#33333333';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,12 +96,12 @@ class GdInputHandlerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$result = $handler->handle('rgb(10, 20, 30)');
|
$result = $handler->handle('rgb(10, 20, 30)');
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$result = $handler->handle('rgba(10, 20, 30, 1.0)');
|
$result = $handler->handle('rgba(10, 20, 30, 1.0)');
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +110,7 @@ class GdInputHandlerTest extends TestCase
|
|||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = 'transparent';
|
$input = 'transparent';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([0, 0, 0, 0], $result->toArray());
|
$this->assertEquals([0, 0, 0, 0], $result->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Tests\Drivers\Imagick;
|
namespace Intervention\Image\Tests\Drivers\Imagick;
|
||||||
|
|
||||||
use Intervention\Image\Colors\Rgb\Color;
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
use Intervention\Image\Drivers\Imagick\Image;
|
use Intervention\Image\Drivers\Imagick\Image;
|
||||||
use Intervention\Image\Drivers\Imagick\InputHandler;
|
use Intervention\Image\Drivers\Imagick\InputHandler;
|
||||||
use Intervention\Image\Exceptions\DecoderException;
|
use Intervention\Image\Exceptions\DecoderException;
|
||||||
@@ -58,37 +58,37 @@ class InputHandlerTest extends TestCase
|
|||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = 'ccff33';
|
$input = 'ccff33';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = 'cf3';
|
$input = 'cf3';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
$this->assertEquals([204, 255, 51, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#123456';
|
$input = '#123456';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([18, 52, 86, 255], $result->toArray());
|
$this->assertEquals([18, 52, 86, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#333';
|
$input = '#333';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([51, 51, 51, 255], $result->toArray());
|
$this->assertEquals([51, 51, 51, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#3333';
|
$input = '#3333';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = '#33333333';
|
$input = '#33333333';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,12 +96,12 @@ class InputHandlerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$result = $handler->handle('rgb(10, 20, 30)');
|
$result = $handler->handle('rgb(10, 20, 30)');
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
||||||
|
|
||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$result = $handler->handle('rgba(10, 20, 30, 1.0)');
|
$result = $handler->handle('rgba(10, 20, 30, 1.0)');
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +110,7 @@ class InputHandlerTest extends TestCase
|
|||||||
$handler = new InputHandler();
|
$handler = new InputHandler();
|
||||||
$input = 'transparent';
|
$input = 'transparent';
|
||||||
$result = $handler->handle($input);
|
$result = $handler->handle($input);
|
||||||
$this->assertInstanceOf(Color::class, $result);
|
$this->assertInstanceOf(RgbColor::class, $result);
|
||||||
$this->assertEquals([0, 0, 0, 0], $result->toArray());
|
$this->assertEquals([0, 0, 0, 0], $result->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Tests;
|
namespace Intervention\Image\Tests;
|
||||||
|
|
||||||
use Intervention\Image\Colors\Rgb\Color;
|
use Intervention\Image\Colors\Rgb\Channels\Alpha;
|
||||||
use Intervention\Image\Colors\Rgb\Colorspace;
|
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
||||||
|
|
||||||
@@ -26,7 +26,8 @@ abstract class TestCase extends MockeryTestCase
|
|||||||
|
|
||||||
protected function assertTransparency(ColorInterface $color)
|
protected function assertTransparency(ColorInterface $color)
|
||||||
{
|
{
|
||||||
$this->assertInstanceOf(Color::class, $color);
|
$this->assertInstanceOf(RgbColor::class, $color);
|
||||||
$this->assertEquals(0, $color->convertTo(Colorspace::class)->alpha()->value());
|
$channel = $color->channel(Alpha::class);
|
||||||
|
$this->assertEquals(0, $channel->value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user