|
12 | 12 | use Intervention\Image\Colors\Oklab\Colorspace as Oklab; |
13 | 13 | use Intervention\Image\Colors\Oklch\Color as OklchColor; |
14 | 14 | use Intervention\Image\Colors\Rgb\Color as RgbColor; |
| 15 | +use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder; |
15 | 16 | use Intervention\Image\Exceptions\ColorDecoderException; |
16 | 17 | use Intervention\Image\Exceptions\InvalidArgumentException; |
17 | 18 | use Intervention\Image\Exceptions\NotSupportedException; |
| 19 | +use Intervention\Image\InputHandler; |
18 | 20 | use Intervention\Image\Interfaces\ColorChannelInterface; |
19 | 21 | use Intervention\Image\Interfaces\ColorInterface; |
20 | 22 | use TypeError; |
@@ -83,6 +85,7 @@ public function importColor(ColorInterface $color): RgbColor |
83 | 85 | HslColor::class => $this->importHslColor($color), |
84 | 86 | OklabColor::class => $this->importOklabColor($color), |
85 | 87 | OklchColor::class => $this->importOklchColor($color), |
| 88 | + NamedColor::class => $this->importNamedColor($color), |
86 | 89 | RgbColor::class => $color, |
87 | 90 | default => throw new NotSupportedException( |
88 | 91 | 'Unable to import color ' . $color::class . ' to ' . $this::class, |
@@ -231,4 +234,20 @@ private function importOklchColor(OklchColor $color): RgbColor |
231 | 234 |
|
232 | 235 | return $this->importOklabColor($color); |
233 | 236 | } |
| 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 | + } |
234 | 253 | } |
0 commit comments