mirror of
https://github.com/Intervention/image.git
synced 2025-01-17 12:18:14 +01:00
Remove Rgba color space
RGB colors with alpha channel are now handles by RGB.
This commit is contained in:
parent
56ab8b1ea7
commit
d95120153f
@ -9,8 +9,6 @@ use Intervention\Image\Colors\Cmyk\Channels\Key;
|
||||
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\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\ColorspaceInterface;
|
||||
@ -81,11 +79,6 @@ class Color implements ColorInterface
|
||||
return $this->convertTo(RgbColorspace::class);
|
||||
}
|
||||
|
||||
public function toRgba(): RgbaColor
|
||||
{
|
||||
return $this->convertTo(RgbaColorspace::class);
|
||||
}
|
||||
|
||||
public function toCmyk(): self
|
||||
{
|
||||
return $this->convertTo(CmykColorspace::class);
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace Intervention\Image\Colors\Cmyk;
|
||||
|
||||
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\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||
@ -18,7 +17,7 @@ class Colorspace implements ColorspaceInterface
|
||||
public function convertColor(ColorInterface $color): ColorInterface
|
||||
{
|
||||
return match (get_class($color)) {
|
||||
RgbColor::class, RgbaColor::class => $this->convertRgbColor($color),
|
||||
RgbColor::class => $this->convertRgbColor($color),
|
||||
default => $color,
|
||||
};
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ class Parser
|
||||
{
|
||||
protected static $parsers = [
|
||||
Rgb\Parser::class,
|
||||
Rgba\Parser::class,
|
||||
Cmyk\Parser::class,
|
||||
];
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||
namespace Intervention\Image\Colors\Rgb\Channels;
|
||||
|
||||
class Alpha extends Red
|
||||
{
|
@ -4,11 +4,10 @@ namespace Intervention\Image\Colors\Rgb;
|
||||
|
||||
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||
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\Green;
|
||||
use Intervention\Image\Colors\Rgb\Channels\Red;
|
||||
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||
use Intervention\Image\Colors\Rgb\Channels\Alpha;
|
||||
use Intervention\Image\Colors\Traits\CanHandleChannels;
|
||||
use Intervention\Image\Interfaces\ColorChannelInterface;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
@ -20,12 +19,13 @@ class Color implements ColorInterface
|
||||
|
||||
protected array $channels;
|
||||
|
||||
public function __construct(int $r, int $g, int $b)
|
||||
public function __construct(int $r, int $g, int $b, int $a = 255)
|
||||
{
|
||||
$this->channels = [
|
||||
new Red($r),
|
||||
new Green($g),
|
||||
new Blue($b),
|
||||
new Alpha($a),
|
||||
];
|
||||
}
|
||||
|
||||
@ -44,6 +44,11 @@ class Color implements ColorInterface
|
||||
return $this->channel(Blue::class);
|
||||
}
|
||||
|
||||
public function alpha(): Alpha
|
||||
{
|
||||
return $this->channel(Alpha::class);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return array_map(function (ColorChannelInterface $channel) {
|
||||
@ -53,12 +58,23 @@ class Color implements ColorInterface
|
||||
|
||||
public function toHex(string $prefix = ''): string
|
||||
{
|
||||
if ($this->isFullyOpaque()) {
|
||||
return sprintf(
|
||||
'%s%02x%02x%02x',
|
||||
$prefix,
|
||||
$this->red()->value(),
|
||||
$this->green()->value(),
|
||||
$this->blue()->value()
|
||||
);
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'%s%02x%02x%02x',
|
||||
'%s%02x%02x%02x%02x',
|
||||
$prefix,
|
||||
$this->red()->value(),
|
||||
$this->green()->value(),
|
||||
$this->blue()->value()
|
||||
$this->blue()->value(),
|
||||
$this->alpha()->value()
|
||||
);
|
||||
}
|
||||
|
||||
@ -82,18 +98,28 @@ class Color implements ColorInterface
|
||||
return $this->convertTo(CmykColorspace::class);
|
||||
}
|
||||
|
||||
public function toRgba(): RgbaColor
|
||||
public function isFullyOpaque(): bool
|
||||
{
|
||||
return $this->convertTo(RgbaColorspace::class);
|
||||
return $this->alpha()->value() === 255;
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
if ($this->isFullyOpaque()) {
|
||||
return sprintf(
|
||||
'rgb(%d, %d, %d)',
|
||||
$this->red()->value(),
|
||||
$this->green()->value(),
|
||||
$this->blue()->value()
|
||||
);
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'rgb(%d, %d, %d)',
|
||||
'rgba(%d, %d, %d, %.1F)',
|
||||
$this->red()->value(),
|
||||
$this->green()->value(),
|
||||
$this->blue()->value()
|
||||
$this->blue()->value(),
|
||||
$this->alpha()->normalize(),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace Intervention\Image\Colors\Rgb;
|
||||
|
||||
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\ColorspaceInterface;
|
||||
|
||||
@ -13,7 +12,6 @@ class Colorspace implements ColorspaceInterface
|
||||
{
|
||||
return match (get_class($color)) {
|
||||
CmykColor::class => $this->convertCmykColor($color),
|
||||
RgbaColor::class => $this->convertRgbaColor($color),
|
||||
default => $color,
|
||||
};
|
||||
}
|
||||
@ -26,13 +24,4 @@ class Colorspace implements ColorspaceInterface
|
||||
(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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ class Parser
|
||||
|
||||
public static function fromHex(string $input): Color
|
||||
{
|
||||
// Hexadecimal colors
|
||||
$pattern = '/^#?(?P<hex>[0-9a-f]{3}|[0-9a-f]{6})$/i';
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
|
||||
@ -48,10 +49,35 @@ class Parser
|
||||
default => throw new ColorException('Unable to parse color'),
|
||||
};
|
||||
|
||||
try {
|
||||
return new Color(
|
||||
strlen($matches[0]) == '1' ? hexdec($matches[0] . $matches[0]) : hexdec($matches[0]),
|
||||
strlen($matches[1]) == '1' ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]),
|
||||
strlen($matches[2]) == '1' ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2])
|
||||
);
|
||||
} catch (ColorException $e) {
|
||||
# move on
|
||||
}
|
||||
|
||||
// Hexadecimal colors with transparency
|
||||
$pattern = '/^#?(?P<hex>[0-9a-f]{4}|[0-9a-f]{8})$/i';
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
|
||||
if ($result !== 1) {
|
||||
throw new ColorException('Unable to parse color');
|
||||
}
|
||||
|
||||
$matches = match (strlen($matches['hex'])) {
|
||||
4 => str_split($matches['hex']),
|
||||
8 => str_split($matches['hex'], 2),
|
||||
default => throw new ColorException('Unable to parse color'),
|
||||
};
|
||||
|
||||
return new Color(
|
||||
strlen($matches[0]) == '1' ? hexdec($matches[0] . $matches[0]) : hexdec($matches[0]),
|
||||
strlen($matches[1]) == '1' ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]),
|
||||
strlen($matches[2]) == '1' ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2]),
|
||||
strlen($matches[3]) == '1' ? hexdec($matches[3] . $matches[3]) : hexdec($matches[3]),
|
||||
);
|
||||
}
|
||||
|
||||
@ -79,6 +105,31 @@ class Parser
|
||||
);
|
||||
}
|
||||
|
||||
// rgba(255, 255, 255, 1.0)
|
||||
$pattern = '/^s?rgba\((?P<r>[0-9]{1,3}), *(?P<g>[0-9]{1,3}), *(?P<b>[0-9]{1,3}), *(?P<a>((1|0))?(\.[0-9]+)?)\)$/';
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
|
||||
if ($result === 1) {
|
||||
return new Color(
|
||||
$matches['r'],
|
||||
$matches['g'],
|
||||
$matches['b'],
|
||||
intval(round(floatval($matches['a']) * 255))
|
||||
);
|
||||
}
|
||||
|
||||
// rgba(100%, 100%, 100%, 100%)
|
||||
$pattern = '/s?rgba\((?P<r>[0-9\.]+)%, ?(?P<g>[0-9\.]+)%, ?(?P<b>[0-9\.]+)%, ?(?P<a>[0-9\.]+)%\)/';
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
if ($result === 1) {
|
||||
return new Color(
|
||||
intval(round(floatval($matches['r']) / 100 * 255)),
|
||||
intval(round(floatval($matches['g']) / 100 * 255)),
|
||||
intval(round(floatval($matches['b']) / 100 * 255)),
|
||||
intval(round(floatval($matches['a']) / 100 * 255))
|
||||
);
|
||||
}
|
||||
|
||||
throw new ColorException('Unable to parse color');
|
||||
}
|
||||
|
||||
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||
|
||||
use Intervention\Image\Colors\Rgb\Channels\Blue as RgbBlue;
|
||||
|
||||
class Blue extends RgbBlue
|
||||
{
|
||||
//
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||
|
||||
use Intervention\Image\Colors\Rgb\Channels\Green as RgbGreen;
|
||||
|
||||
class Green extends RgbGreen
|
||||
{
|
||||
//
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Colors\Rgba\Channels;
|
||||
|
||||
use Intervention\Image\Colors\Rgb\Channels\Red as RgbRed;
|
||||
|
||||
class Red extends RgbRed
|
||||
{
|
||||
//
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
<?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(),
|
||||
);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
<?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
|
||||
);
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Colors\Rgba;
|
||||
|
||||
use Intervention\Image\Colors\Rgb\Parser as RgbParser;
|
||||
use Intervention\Image\Exceptions\ColorException;
|
||||
|
||||
class Parser extends RgbParser
|
||||
{
|
||||
public static function fromHex(string $input): Color
|
||||
{
|
||||
try {
|
||||
return parent::fromHex($input)->toRgba();
|
||||
} catch (ColorException $e) {
|
||||
// move on
|
||||
}
|
||||
|
||||
$pattern = '/^#?(?P<hex>[0-9a-f]{4}|[0-9a-f]{8})$/i';
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
|
||||
if ($result !== 1) {
|
||||
throw new ColorException('Unable to parse color');
|
||||
}
|
||||
|
||||
$matches = match (strlen($matches['hex'])) {
|
||||
4 => str_split($matches['hex']),
|
||||
8 => str_split($matches['hex'], 2),
|
||||
default => throw new ColorException('Unable to parse color'),
|
||||
};
|
||||
|
||||
return new Color(
|
||||
strlen($matches[0]) == '1' ? hexdec($matches[0] . $matches[0]) : hexdec($matches[0]),
|
||||
strlen($matches[1]) == '1' ? hexdec($matches[1] . $matches[1]) : hexdec($matches[1]),
|
||||
strlen($matches[2]) == '1' ? hexdec($matches[2] . $matches[2]) : hexdec($matches[2]),
|
||||
strlen($matches[3]) == '1' ? hexdec($matches[3] . $matches[3]) : hexdec($matches[3]),
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromString(string $input): Color
|
||||
{
|
||||
// rgba(255, 255, 255, 1.0)
|
||||
$pattern = '/^s?rgba\((?P<r>[0-9]{1,3}), *(?P<g>[0-9]{1,3}), *(?P<b>[0-9]{1,3}), *(?P<a>((1|0))?(\.[0-9]+)?)\)$/';
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
|
||||
if ($result === 1) {
|
||||
return new Color(
|
||||
$matches['r'],
|
||||
$matches['g'],
|
||||
$matches['b'],
|
||||
intval(round(floatval($matches['a']) * 255))
|
||||
);
|
||||
}
|
||||
|
||||
// rgba(100%, 100%, 100%, 100%)
|
||||
$pattern = '/s?rgba\((?P<r>[0-9\.]+)%, ?(?P<g>[0-9\.]+)%, ?(?P<b>[0-9\.]+)%, ?(?P<a>[0-9\.]+)%\)/';
|
||||
$result = preg_match($pattern, $input, $matches);
|
||||
if ($result === 1) {
|
||||
return new Color(
|
||||
intval(round(floatval($matches['r']) / 100 * 255)),
|
||||
intval(round(floatval($matches['g']) / 100 * 255)),
|
||||
intval(round(floatval($matches['b']) / 100 * 255)),
|
||||
intval(round(floatval($matches['a']) / 100 * 255))
|
||||
);
|
||||
}
|
||||
|
||||
throw new ColorException('Unable to parse color');
|
||||
}
|
||||
}
|
@ -3,12 +3,12 @@
|
||||
namespace Intervention\Image\Drivers\Gd;
|
||||
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Colors\Rgba\Color;
|
||||
use Intervention\Image\Colors\Rgb\Color;
|
||||
|
||||
class ColorTransformer
|
||||
{
|
||||
/**
|
||||
* Transforms GD Library integer color value to RGBA color object
|
||||
* Transforms GD Library integer color value to RGB color object
|
||||
*
|
||||
* @param int $value
|
||||
* @return Color
|
||||
@ -35,7 +35,7 @@ class ColorTransformer
|
||||
*/
|
||||
public static function colorToInteger(ColorInterface $color): int
|
||||
{
|
||||
$color = $color->toRgba();
|
||||
$color = $color->toRgb();
|
||||
|
||||
$r = $color->red()->value();
|
||||
$g = $color->green()->value();
|
||||
|
@ -4,12 +4,10 @@ namespace Intervention\Image\Interfaces;
|
||||
|
||||
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||
|
||||
interface ColorInterface
|
||||
{
|
||||
public function toRgb(): RgbColor;
|
||||
public function toRgba(): RgbaColor;
|
||||
public function toCmyk(): CmykColor;
|
||||
public function toArray(): array;
|
||||
public function toString(): string;
|
||||
|
@ -3,7 +3,6 @@
|
||||
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;
|
||||
@ -83,12 +82,4 @@ class ColorTest extends TestCase
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ namespace Intervention\Image\Tests\Colors;
|
||||
|
||||
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
|
||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||
use Intervention\Image\Colors\Parser;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
|
||||
@ -25,7 +24,7 @@ class ParserTest extends TestCase
|
||||
$this->assertEquals([204, 204, 204], $color->toArray());
|
||||
|
||||
$color = Parser::parse('rgba(204, 204, 204, 1)');
|
||||
$this->assertInstanceOf(RgbaColor::class, $color);
|
||||
$this->assertInstanceOf(RgbColor::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::parse('cccc');
|
||||
|
@ -3,7 +3,6 @@
|
||||
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\Green;
|
||||
use Intervention\Image\Colors\Rgb\Channels\Blue;
|
||||
@ -111,12 +110,4 @@ class ColorTest extends TestCase
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
@ -69,9 +69,6 @@ class ParserTest extends TestCase
|
||||
|
||||
$this->expectException(ColorException::class);
|
||||
(new Parser())->fromString('rgb(120)');
|
||||
|
||||
$this->expectException(ColorException::class);
|
||||
(new Parser())->fromString('rgba(204,204,204,1)');
|
||||
}
|
||||
|
||||
public function testFromName(): void
|
||||
|
@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Tests\Colors\Rgba;
|
||||
|
||||
use Intervention\Image\Colors\Rgba\Channels\Red as Channel;
|
||||
use Intervention\Image\Colors\Rgba\Channels\Alpha as Alpha;
|
||||
use Intervention\Image\Exceptions\ColorException;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @requires extension gd
|
||||
* @covers \Intervention\Image\Colors\Rgba\Channels\Red
|
||||
* @covers \Intervention\Image\Colors\Rgba\Channels\Green
|
||||
* @covers \Intervention\Image\Colors\Rgba\Channels\Blue
|
||||
* @covers \Intervention\Image\Colors\Rgba\Channels\Alpha
|
||||
*/
|
||||
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(255);
|
||||
$this->assertEquals(1, $channel->normalize());
|
||||
$channel = new Channel(0);
|
||||
$this->assertEquals(0, $channel->normalize());
|
||||
$channel = new Channel(51);
|
||||
$this->assertEquals(.2, $channel->normalize());
|
||||
}
|
||||
|
||||
public function testValidate(): void
|
||||
{
|
||||
$this->expectException(ColorException::class);
|
||||
new Channel(256);
|
||||
|
||||
$this->expectException(ColorException::class);
|
||||
new Channel(-1);
|
||||
}
|
||||
|
||||
public function testToString(): void
|
||||
{
|
||||
$channel = new Channel(255);
|
||||
$this->assertEquals("255", $channel->toString());
|
||||
|
||||
$channel = new Alpha(0);
|
||||
$this->assertEquals("0", $channel->toString());
|
||||
|
||||
$channel = new Alpha(51);
|
||||
$this->assertEquals("0.2", $channel->toString());
|
||||
|
||||
$channel = new Alpha(255);
|
||||
$this->assertEquals("1", $channel->toString());
|
||||
|
||||
$channel = new Alpha(170);
|
||||
$this->assertEquals("0.666667", $channel->toString());
|
||||
}
|
||||
}
|
@ -1,119 +0,0 @@
|
||||
<?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());
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
<?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)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Tests\Colors\Rgba;
|
||||
|
||||
use Intervention\Image\Colors\Rgba\Color;
|
||||
use Intervention\Image\Colors\Rgba\Parser;
|
||||
use Intervention\Image\Exceptions\ColorException;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @requires extension gd
|
||||
* @covers \Intervention\Image\Colors\Rgba\Parser
|
||||
*/
|
||||
class ParserTest extends TestCase
|
||||
{
|
||||
public function testParse(): void
|
||||
{
|
||||
$color = Parser::parse('ccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::parse('rgba(204, 204, 204, 1)');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::parse('salmon');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals('fa8072', $color->toHex());
|
||||
}
|
||||
|
||||
public function testFromHex(): void
|
||||
{
|
||||
$color = Parser::fromHex('ccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::fromHex('cccccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::fromHex('#cccccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::fromHex('#cccccccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 204], $color->toArray());
|
||||
|
||||
$color = Parser::fromHex('#cccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 204], $color->toArray());
|
||||
|
||||
$color = Parser::fromHex('cccccccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 204], $color->toArray());
|
||||
|
||||
$color = Parser::fromHex('cccc');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 204], $color->toArray());
|
||||
}
|
||||
|
||||
public function testFromString(): void
|
||||
{
|
||||
$color = Parser::fromString('rgba(204, 204, 204, 1)');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::fromString('rgba(204,204,204,1.0)');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 255], $color->toArray());
|
||||
|
||||
$color = Parser::fromString('rgba(204,204,204,0.2)');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 51], $color->toArray());
|
||||
|
||||
$color = Parser::fromString('rgba(204,204, 204, .2)');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([204, 204, 204, 51], $color->toArray());
|
||||
|
||||
$color = Parser::fromString('rgba(100%,20%,25%,100%)');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([255, 51, 64, 255], $color->toArray());
|
||||
|
||||
$color = Parser::fromString('rgba(100%,74.8064%,25.2497%,100%)');
|
||||
$this->assertInstanceOf(Color::class, $color);
|
||||
$this->assertEquals([255, 191, 64, 255], $color->toArray());
|
||||
|
||||
$this->expectException(ColorException::class);
|
||||
$color = Parser::fromString('rgba(204, 204, 204, 1.2)');
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Intervention\Image\Tests\Drivers\Gd;
|
||||
|
||||
use Intervention\Image\Colors\Rgba\Color;
|
||||
use Intervention\Image\Colors\Rgb\Color;
|
||||
use Intervention\Image\Drivers\Gd\ColorTransformer;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace Intervention\Image\Tests\Drivers\Gd\Decoders;
|
||||
|
||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||
use Intervention\Image\Drivers\Gd\Decoders\RgbStringColorDecoder;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
|
||||
@ -25,7 +24,7 @@ class RgbStringColorDecoderTest extends TestCase
|
||||
{
|
||||
$decoder = new RgbStringColorDecoder();
|
||||
$color = $decoder->decode('rgba(181, 55, 23, 0.5)');
|
||||
$this->assertInstanceOf(RgbaColor::class, $color);
|
||||
$this->assertInstanceOf(RgbColor::class, $color);
|
||||
$this->assertEquals([181, 55, 23, 51], $color->toArray());
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace Intervention\Image\Tests\Drivers\Gd;
|
||||
|
||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||
use Intervention\Image\Drivers\Gd\Image;
|
||||
use Intervention\Image\Drivers\Gd\InputHandler;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
@ -83,13 +82,13 @@ class GdInputHandlerTest extends TestCase
|
||||
$handler = new InputHandler();
|
||||
$input = '#3333';
|
||||
$result = $handler->handle($input);
|
||||
$this->assertInstanceOf(RgbaColor::class, $result);
|
||||
$this->assertInstanceOf(RgbColor::class, $result);
|
||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||
|
||||
$handler = new InputHandler();
|
||||
$input = '#33333333';
|
||||
$result = $handler->handle($input);
|
||||
$this->assertInstanceOf(RgbaColor::class, $result);
|
||||
$this->assertInstanceOf(RgbColor::class, $result);
|
||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||
}
|
||||
|
||||
@ -102,7 +101,7 @@ class GdInputHandlerTest extends TestCase
|
||||
|
||||
$handler = new InputHandler();
|
||||
$result = $handler->handle('rgba(10, 20, 30, 1.0)');
|
||||
$this->assertInstanceOf(RgbaColor::class, $result);
|
||||
$this->assertInstanceOf(RgbColor::class, $result);
|
||||
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
namespace Intervention\Image\Tests\Drivers\Imagick;
|
||||
|
||||
use ImagickPixel;
|
||||
use Intervention\Image\Colors\Rgba\Color;
|
||||
use Intervention\Image\Colors\Rgb\Color;
|
||||
use Intervention\Image\Drivers\Imagick\ColorTransformer;
|
||||
use Intervention\Image\Tests\TestCase;
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace Intervention\Image\Tests\Drivers\Imagick;
|
||||
|
||||
use Intervention\Image\Colors\Rgb\Color as RgbColor;
|
||||
use Intervention\Image\Colors\Rgba\Color as RgbaColor;
|
||||
use Intervention\Image\Drivers\Imagick\Image;
|
||||
use Intervention\Image\Drivers\Imagick\InputHandler;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
@ -83,13 +82,13 @@ class InputHandlerTest extends TestCase
|
||||
$handler = new InputHandler();
|
||||
$input = '#3333';
|
||||
$result = $handler->handle($input);
|
||||
$this->assertInstanceOf(RgbaColor::class, $result);
|
||||
$this->assertInstanceOf(RgbColor::class, $result);
|
||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||
|
||||
$handler = new InputHandler();
|
||||
$input = '#33333333';
|
||||
$result = $handler->handle($input);
|
||||
$this->assertInstanceOf(RgbaColor::class, $result);
|
||||
$this->assertInstanceOf(RgbColor::class, $result);
|
||||
$this->assertEquals([51, 51, 51, 51], $result->toArray());
|
||||
}
|
||||
|
||||
@ -102,7 +101,7 @@ class InputHandlerTest extends TestCase
|
||||
|
||||
$handler = new InputHandler();
|
||||
$result = $handler->handle('rgba(10, 20, 30, 1.0)');
|
||||
$this->assertInstanceOf(RgbaColor::class, $result);
|
||||
$this->assertInstanceOf(RgbColor::class, $result);
|
||||
$this->assertEquals([10, 20, 30, 255], $result->toArray());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user