Skip to content

Commit 6a72305

Browse files
committed
Add ability to import named colors
1 parent b97e8f2 commit 6a72305

File tree

13 files changed

+106
-12
lines changed

13 files changed

+106
-12
lines changed

src/Colors/Cmyk/Colorspace.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Intervention\Image\Colors\Oklch\Color as OklchColor;
1313
use Intervention\Image\Colors\Rgb\Color as RgbColor;
1414
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
15+
use Intervention\Image\Colors\Rgb\NamedColor;
1516
use Intervention\Image\Exceptions\ColorDecoderException;
1617
use Intervention\Image\Exceptions\InvalidArgumentException;
1718
use Intervention\Image\Exceptions\NotSupportedException;
@@ -77,6 +78,7 @@ public function importColor(ColorInterface $color): CmykColor
7778
OklchColor::class,
7879
OklabColor::class,
7980
HsvColor::class,
81+
NamedColor::class,
8082
HslColor::class => $this->importViaRgbColor($color),
8183
RgbColor::class => $this->importRgbColor($color),
8284
CmykColor::class => $color,
@@ -108,7 +110,7 @@ private function importRgbColor(RgbColor $color): CmykColor
108110
*
109111
* @throws ColorDecoderException
110112
*/
111-
private function importViaRgbColor(OklabColor|OklchColor|HslColor|HsvColor $color): CmykColor
113+
private function importViaRgbColor(NamedColor|OklabColor|OklchColor|HslColor|HsvColor $color): CmykColor
112114
{
113115
try {
114116
$color = $color->toColorspace(RgbColorspace::class);

src/Colors/Hsl/Colorspace.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Intervention\Image\Colors\Oklch\Color as OklchColor;
1313
use Intervention\Image\Colors\Rgb\Color as RgbColor;
1414
use Intervention\Image\Colors\Rgb\Colorspace as Rgb;
15+
use Intervention\Image\Colors\Rgb\NamedColor;
1516
use Intervention\Image\Exceptions\ColorDecoderException;
1617
use Intervention\Image\Exceptions\InvalidArgumentException;
1718
use Intervention\Image\Exceptions\NotSupportedException;
@@ -78,6 +79,7 @@ public function importColor(ColorInterface $color): HslColor
7879
return match ($color::class) {
7980
OklchColor::class,
8081
OklabColor::class,
82+
NamedColor::class,
8183
CmykColor::class => $this->importViaRgbColor($color),
8284
RgbColor::class => $this->importRgbColor($color),
8385
HsvColor::class => $this->importHsvColor($color),
@@ -165,7 +167,7 @@ private function importHsvColor(HsvColor $color): HslColor
165167
*
166168
* @throws ColorDecoderException
167169
*/
168-
private function importViaRgbColor(CmykColor|OklabColor|OklchColor $color): HslColor
170+
private function importViaRgbColor(NamedColor|CmykColor|OklabColor|OklchColor $color): HslColor
169171
{
170172
try {
171173
$color = $color->toColorspace(Rgb::class);

src/Colors/Hsv/Colorspace.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Intervention\Image\Colors\Oklch\Color as OklchColor;
1313
use Intervention\Image\Colors\Rgb\Color as RgbColor;
1414
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
15+
use Intervention\Image\Colors\Rgb\NamedColor;
1516
use Intervention\Image\Exceptions\ColorDecoderException;
1617
use Intervention\Image\Exceptions\InvalidArgumentException;
1718
use Intervention\Image\Exceptions\NotSupportedException;
@@ -79,6 +80,7 @@ public function importColor(ColorInterface $color): HsvColor
7980
return match ($color::class) {
8081
CmykColor::class,
8182
OklchColor::class,
83+
NamedColor::class,
8284
OklabColor::class => $this->importViaRgbColor($color),
8385
RgbColor::class => $this->importRgbColor($color),
8486
HslColor::class => $this->importHslColor($color),
@@ -163,7 +165,7 @@ protected function importHslColor(ColorInterface $color): HsvColor
163165
*
164166
* @throws ColorDecoderException
165167
*/
166-
private function importViaRgbColor(CmykColor|OklchColor|OklabColor $color): HsvColor
168+
private function importViaRgbColor(NamedColor|CmykColor|OklchColor|OklabColor $color): HsvColor
167169
{
168170
try {
169171
$color = $color->toColorspace(RgbColorspace::class);

src/Colors/Oklab/Colorspace.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Intervention\Image\Colors\Oklch\Color as OklchColor;
1313
use Intervention\Image\Colors\Rgb\Color as RgbColor;
1414
use Intervention\Image\Colors\Rgb\Colorspace as Rgb;
15+
use Intervention\Image\Colors\Rgb\NamedColor;
1516
use Intervention\Image\Exceptions\ColorDecoderException;
1617
use Intervention\Image\Exceptions\InvalidArgumentException;
1718
use Intervention\Image\Exceptions\NotSupportedException;
@@ -77,6 +78,7 @@ public function importColor(ColorInterface $color): OklabColor
7778
return match ($color::class) {
7879
CmykColor::class,
7980
HsvColor::class,
81+
NamedColor::class,
8082
HslColor::class => $this->importViaRgbColor($color),
8183
RgbColor::class => $this->importRgbColor($color),
8284
OklchColor::class => $this->importOklchColor($color),
@@ -139,7 +141,7 @@ private function importOklchColor(OklchColor $color): OklabColor
139141
*
140142
* @throws ColorDecoderException
141143
*/
142-
private function importViaRgbColor(CmykColor|HslColor|HsvColor $color): OklabColor
144+
private function importViaRgbColor(NamedColor|CmykColor|HslColor|HsvColor $color): OklabColor
143145
{
144146
try {
145147
$color = $color->toColorspace(Rgb::class)->toColorspace($this::class);

src/Colors/Oklch/Colorspace.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Intervention\Image\Colors\Oklch\Color as OklchColor;
1818
use Intervention\Image\Colors\Rgb\Color as RgbColor;
1919
use Intervention\Image\Colors\Rgb\Colorspace as Rgb;
20+
use Intervention\Image\Colors\Rgb\NamedColor;
2021
use Intervention\Image\Exceptions\ColorDecoderException;
2122
use Intervention\Image\Exceptions\InvalidArgumentException;
2223
use Intervention\Image\Exceptions\NotSupportedException;
@@ -80,6 +81,7 @@ public function importColor(ColorInterface $color): OklchColor
8081
return match ($color::class) {
8182
CmykColor::class,
8283
HsvColor::class,
84+
NamedColor::class,
8385
HslColor::class => $this->importViaRgbColor($color),
8486
OklabColor::class => $this->importOklabColor($color),
8587
RgbColor::class => $this->importRgbColor($color),
@@ -133,7 +135,7 @@ private function importRgbColor(RgbColor $color): OklchColor
133135
*
134136
* @throws ColorDecoderException
135137
*/
136-
private function importViaRgbColor(HslColor|HsvColor|CmykColor $color): OklchColor
138+
private function importViaRgbColor(NamedColor|HslColor|HsvColor|CmykColor $color): OklchColor
137139
{
138140
try {
139141
$color = $color->toColorspace(Rgb::class)->toColorspace($this::class);

src/Colors/Rgb/Colorspace.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use Intervention\Image\Colors\Oklab\Colorspace as Oklab;
1313
use Intervention\Image\Colors\Oklch\Color as OklchColor;
1414
use Intervention\Image\Colors\Rgb\Color as RgbColor;
15+
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
1516
use Intervention\Image\Exceptions\ColorDecoderException;
1617
use Intervention\Image\Exceptions\InvalidArgumentException;
1718
use Intervention\Image\Exceptions\NotSupportedException;
19+
use Intervention\Image\InputHandler;
1820
use Intervention\Image\Interfaces\ColorChannelInterface;
1921
use Intervention\Image\Interfaces\ColorInterface;
2022
use TypeError;
@@ -83,6 +85,7 @@ public function importColor(ColorInterface $color): RgbColor
8385
HslColor::class => $this->importHslColor($color),
8486
OklabColor::class => $this->importOklabColor($color),
8587
OklchColor::class => $this->importOklchColor($color),
88+
NamedColor::class => $this->importNamedColor($color),
8689
RgbColor::class => $color,
8790
default => throw new NotSupportedException(
8891
'Unable to import color ' . $color::class . ' to ' . $this::class,
@@ -231,4 +234,20 @@ private function importOklchColor(OklchColor $color): RgbColor
231234

232235
return $this->importOklabColor($color);
233236
}
237+
238+
/**
239+
* Import given named color to RGB color space.
240+
*
241+
* @throws ColorDecoderException
242+
*/
243+
private function importNamedColor(NamedColor $color): RgbColor
244+
{
245+
$output = InputHandler::usingDecoders([
246+
HexColorDecoder::class,
247+
])->handle($color->toHex());
248+
249+
return $output instanceof RgbColor
250+
? $output
251+
: throw new ColorDecoderException('Failed to import named color to rgb color space');
252+
}
234253
}

src/Colors/Rgb/NamedColor.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,6 @@ public function withTransparency(float $transparency): ColorInterface
421421
*/
422422
private function toRgbColor(): RgbColor
423423
{
424-
$output = InputHandler::usingDecoders([
425-
HexColorDecoder::class,
426-
])->handle($this->toHex());
427-
428-
return $output instanceof RgbColor
429-
? $output
430-
: throw new ColorDecoderException('Failed to convert named color to rgb color object');
424+
return $this->colorspace()->importColor($this);
431425
}
432426
}

tests/Unit/Colors/Cmyk/ColorspaceTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Intervention\Image\Colors\Hsv\Color as HsvColor;
1616
use Intervention\Image\Colors\Hsl\Color as HslColor;
1717
use Intervention\Image\Colors\Cmyk\Colorspace;
18+
use Intervention\Image\Colors\Rgb\NamedColor;
1819
use Intervention\Image\Tests\BaseTestCase;
1920

2021
#[CoversClass(Colorspace::class)]
@@ -95,4 +96,16 @@ public function testImportHslColor(): void
9596
$this->assertEquals(0, $result->channel(Yellow::class)->value());
9697
$this->assertEquals(50, $result->channel(Key::class)->value());
9798
}
99+
100+
public function testImportNamedColor(): void
101+
{
102+
$colorspace = new Colorspace();
103+
104+
$result = $colorspace->importColor(NamedColor::BLACK);
105+
$this->assertInstanceOf(CmykColor::class, $result);
106+
$this->assertEquals(0, $result->channel(Cyan::class)->value());
107+
$this->assertEquals(0, $result->channel(Magenta::class)->value());
108+
$this->assertEquals(0, $result->channel(Yellow::class)->value());
109+
$this->assertEquals(100, $result->channel(Key::class)->value());
110+
}
98111
}

tests/Unit/Colors/Hsl/ColorspaceTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Intervention\Image\Colors\Hsv\Color as HsvColor;
1515
use Intervention\Image\Colors\Hsl\Colorspace;
1616
use Intervention\Image\Colors\Hsl\Channels\Alpha;
17+
use Intervention\Image\Colors\Rgb\NamedColor;
1718
use Intervention\Image\Tests\BaseTestCase;
1819

1920
#[CoversClass(Colorspace::class)]
@@ -94,4 +95,15 @@ public function testImportHsvColor(): void
9495
$this->assertEquals(0, $result->channel(Saturation::class)->value());
9596
$this->assertEquals(50, $result->channel(Luminance::class)->value());
9697
}
98+
99+
public function testImportNamedColor(): void
100+
{
101+
$colorspace = new Colorspace();
102+
103+
$result = $colorspace->importColor(NamedColor::WHITE);
104+
$this->assertInstanceOf(HslColor::class, $result);
105+
$this->assertEquals(0, $result->channel(Hue::class)->value());
106+
$this->assertEquals(0, $result->channel(Saturation::class)->value());
107+
$this->assertEquals(100, $result->channel(Luminance::class)->value());
108+
}
97109
}

tests/Unit/Colors/Hsv/ColorspaceTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Intervention\Image\Colors\Hsl\Color as HslColor;
1515
use Intervention\Image\Colors\Hsv\Channels\Alpha;
1616
use Intervention\Image\Colors\Hsv\Colorspace;
17+
use Intervention\Image\Colors\Rgb\NamedColor;
1718
use Intervention\Image\Tests\BaseTestCase;
1819

1920
#[CoversClass(Colorspace::class)]
@@ -93,4 +94,15 @@ public function testImportHslColor(): void
9394
$this->assertEquals(0, $result->channel(Saturation::class)->value());
9495
$this->assertEquals(50, $result->channel(Value::class)->value());
9596
}
97+
98+
public function testImportNamedColor(): void
99+
{
100+
$colorspace = new Colorspace();
101+
102+
$result = $colorspace->importColor(NamedColor::WHITE);
103+
$this->assertInstanceOf(HsvColor::class, $result);
104+
$this->assertEquals(0, $result->channel(Hue::class)->value());
105+
$this->assertEquals(0, $result->channel(Saturation::class)->value());
106+
$this->assertEquals(100, $result->channel(Value::class)->value());
107+
}
96108
}

0 commit comments

Comments
 (0)