From ad0dc4bb769b3fe261ea16c0626ebbfb8afb45c5 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Tue, 24 Jun 2025 16:56:23 +0200 Subject: [PATCH] Implement ColorInterface::create(mixed ...$input) --- changelog.md | 6 ++++++ src/Colors/Cmyk/Color.php | 15 ++++++++++++++- src/Colors/Hsl/Color.php | 15 ++++++++++++++- src/Colors/Hsv/Color.php | 15 ++++++++++++++- src/Colors/Rgb/Color.php | 15 ++++++++++++++- src/Interfaces/ColorInterface.php | 2 +- tests/Unit/Colors/Cmyk/ColorTest.php | 3 +++ tests/Unit/Colors/Hsl/ColorTest.php | 3 +++ tests/Unit/Colors/Hsv/ColorTest.php | 3 +++ tests/Unit/Colors/Rgb/ColorTest.php | 4 ++++ 10 files changed, 76 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index 6f91805c..dd72d58c 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/Colors/Cmyk/Color.php b/src/Colors/Cmyk/Color.php index 978f80e5..902e62fe 100644 --- a/src/Colors/Cmyk/Color.php +++ b/src/Colors/Cmyk/Color.php @@ -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); diff --git a/src/Colors/Hsl/Color.php b/src/Colors/Hsl/Color.php index e116dca7..59f57a3b 100644 --- a/src/Colors/Hsl/Color.php +++ b/src/Colors/Hsl/Color.php @@ -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); diff --git a/src/Colors/Hsv/Color.php b/src/Colors/Hsv/Color.php index 9507657d..14b69607 100644 --- a/src/Colors/Hsv/Color.php +++ b/src/Colors/Hsv/Color.php @@ -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); diff --git a/src/Colors/Rgb/Color.php b/src/Colors/Rgb/Color.php index 70d84c2a..7818a8af 100644 --- a/src/Colors/Rgb/Color.php +++ b/src/Colors/Rgb/Color.php @@ -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, diff --git a/src/Interfaces/ColorInterface.php b/src/Interfaces/ColorInterface.php index 2770b154..82ab7eff 100644 --- a/src/Interfaces/ColorInterface.php +++ b/src/Interfaces/ColorInterface.php @@ -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 diff --git a/tests/Unit/Colors/Cmyk/ColorTest.php b/tests/Unit/Colors/Cmyk/ColorTest.php index 08a028a2..4e641b55 100644 --- a/tests/Unit/Colors/Cmyk/ColorTest.php +++ b/tests/Unit/Colors/Cmyk/ColorTest.php @@ -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 diff --git a/tests/Unit/Colors/Hsl/ColorTest.php b/tests/Unit/Colors/Hsl/ColorTest.php index c0d9e423..589b7547 100644 --- a/tests/Unit/Colors/Hsl/ColorTest.php +++ b/tests/Unit/Colors/Hsl/ColorTest.php @@ -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 diff --git a/tests/Unit/Colors/Hsv/ColorTest.php b/tests/Unit/Colors/Hsv/ColorTest.php index 1d900e28..5ba6db9b 100644 --- a/tests/Unit/Colors/Hsv/ColorTest.php +++ b/tests/Unit/Colors/Hsv/ColorTest.php @@ -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 diff --git a/tests/Unit/Colors/Rgb/ColorTest.php b/tests/Unit/Colors/Rgb/ColorTest.php index 28579ffe..a734ce83 100644 --- a/tests/Unit/Colors/Rgb/ColorTest.php +++ b/tests/Unit/Colors/Rgb/ColorTest.php @@ -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