1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-21 05:01:20 +02:00

Implement ColorInterface::create(mixed ...$input)

This commit is contained in:
Oliver Vogel
2025-06-24 16:56:23 +02:00
parent 576b066718
commit ad0dc4bb76
10 changed files with 76 additions and 5 deletions

View File

@@ -1,6 +1,12 @@
## New Features
- ImageInterface::version()
- ColorInterface::create() now accepts functional string color formats as well as single channel values
- ImageManagerInterface::readPath()
- ImageManagerInterface::readBinary()
- ImageManagerInterface::readBase64()
- ImageManagerInterface::readSplFileInfo()
- ImageManagerInterface::readDataUri()
## API Changes

View File

@@ -10,6 +10,7 @@ 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\Exceptions\DecoderException;
use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
@@ -38,8 +39,20 @@ class Color extends AbstractColor
*
* @see ColorInterface::create()
*/
public static function create(mixed $input): ColorInterface
public static function create(mixed ...$input): ColorInterface
{
$input = match (count($input)) {
1 => $input[0],
4 => $input,
default => throw new DecoderException(
'Too few arguments to create color, ' . count($input) . ' passed and 1 or 4 expected.',
),
};
if (is_array($input)) {
return new self(...$input);
}
return InputHandler::withDecoders([
Decoders\StringColorDecoder::class,
])->handle($input);

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Hsl\Channels\Hue;
use Intervention\Image\Colors\Hsl\Channels\Luminance;
use Intervention\Image\Colors\Hsl\Channels\Saturation;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
@@ -46,8 +47,20 @@ class Color extends AbstractColor
*
* @see ColorInterface::create()
*/
public static function create(mixed $input): ColorInterface
public static function create(mixed ...$input): ColorInterface
{
$input = match (count($input)) {
1 => $input[0],
3 => $input,
default => throw new DecoderException(
'Too few arguments to create color, ' . count($input) . ' passed and 1 or 3 expected.',
),
};
if (is_array($input)) {
return new self(...$input);
}
return InputHandler::withDecoders([
Decoders\StringColorDecoder::class,
])->handle($input);

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Hsv\Channels\Hue;
use Intervention\Image\Colors\Hsv\Channels\Saturation;
use Intervention\Image\Colors\Hsv\Channels\Value;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
@@ -46,8 +47,20 @@ class Color extends AbstractColor
*
* @see ColorInterface::create()
*/
public static function create(mixed $input): ColorInterface
public static function create(mixed ...$input): ColorInterface
{
$input = match (count($input)) {
1 => $input[0],
3 => $input,
default => throw new DecoderException(
'Too few arguments to create color, ' . count($input) . ' passed and 1 or 3 expected.',
),
};
if (is_array($input)) {
return new self(...$input);
}
return InputHandler::withDecoders([
Decoders\StringColorDecoder::class,
])->handle($input);

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Colors\Rgb\Channels\Alpha;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Intervention\Image\Interfaces\ColorInterface;
@@ -47,8 +48,20 @@ class Color extends AbstractColor
*
* @see ColorInterface::create()
*/
public static function create(mixed $input): ColorInterface
public static function create(mixed ...$input): ColorInterface
{
$input = match (count($input)) {
1 => $input[0],
3, 4 => $input,
default => throw new DecoderException(
'Too few arguments to create color, ' . count($input) . ' passed and 1, 3, or 4 expected.',
),
};
if (is_array($input)) {
return new self(...$input);
}
return InputHandler::withDecoders([
Decoders\HexColorDecoder::class,
Decoders\StringColorDecoder::class,

View File

@@ -15,7 +15,7 @@ interface ColorInterface
*
* @throws RuntimeException
*/
public static function create(mixed $input): self;
public static function create(mixed ...$input): self;
/**
* Return colorspace of current color

View File

@@ -27,6 +27,9 @@ final class ColorTest extends BaseTestCase
{
$color = Color::create('cmyk(10, 20, 30, 40)');
$this->assertInstanceOf(Color::class, $color);
$color = Color::create(10, 20, 30, 40);
$this->assertInstanceOf(Color::class, $color);
}
public function testColorspace(): void

View File

@@ -26,6 +26,9 @@ final class ColorTest extends BaseTestCase
{
$color = Color::create('hsl(10, 20, 30)');
$this->assertInstanceOf(Color::class, $color);
$color = Color::create(10, 20, 30);
$this->assertInstanceOf(Color::class, $color);
}
public function testColorspace(): void

View File

@@ -26,6 +26,9 @@ final class ColorTest extends BaseTestCase
{
$color = Color::create('hsv(10, 20, 30)');
$this->assertInstanceOf(Color::class, $color);
$color = Color::create(10, 20, 30);
$this->assertInstanceOf(Color::class, $color);
}
public function testColorspace(): void

View File

@@ -36,6 +36,10 @@ final class ColorTest extends BaseTestCase
$color = Color::create('rgba(10, 20, 30, .2)');
$this->assertInstanceOf(Color::class, $color);
$this->assertEquals([10, 20, 30, 51], $color->toArray());
$color = Color::create(10, 20, 30, 51);
$this->assertInstanceOf(Color::class, $color);
$this->assertEquals([10, 20, 30, 51], $color->toArray());
}
public function testColorspace(): void