{"record":{"id":"a8833717ed4ec0d7","repo":"Intervention/image","slug":"failed-to-import-color-color-class-to-colorspac-a88337","errorCode":null,"errorMessage":"Failed to import color {color_class} to {colorspace_class}","messagePattern":"Failed to import color (.+?) to (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ColorException","httpStatus":null,"severity":"error","filePath":"src/Colors/Rgb/Colorspace.php","lineNumber":88,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::importColor()\n     *\n     * @throws ColorException\n     */\n    public function importColor(ColorInterface $color): RgbColor\n    {\n        return match ($color::class) {\n            CmykColor::class => $this->importCmykColor($color),\n            HsvColor::class => $this->importHsvColor($color),\n            HslColor::class => $this->importHslColor($color),\n            OklabColor::class => $this->importOklabColor($color),\n            OklchColor::class => $this->importOklchColor($color),\n            NamedColor::class => $this->importNamedColor($color),\n            RgbColor::class => $color,\n            default => throw new ColorException(\n                'Failed to import color ' . $color::class . ' to ' . $this::class,\n            ),\n        };\n    }\n\n    /**\n     * @throws ColorException\n     */\n    private function importCmykColor(CmykColor $color): RgbColor\n    {\n        try {\n            return new Color(\n                (int) (255 * (1 - $color->cyan()->normalized()) * (1 - $color->key()->normalized())),\n                (int) (255 * (1 - $color->magenta()->normalized()) * (1 - $color->key()->normalized())),\n                (int) (255 * (1 - $color->yellow()->normalized()) * (1 - $color->key()->normalized())),\n                $color->alpha()->normalized(),\n            );\n        } catch (InvalidArgumentException $e) {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Rgb/Colorspace.php#L70-L106","documentation":"Rgb\\Colorspace::importColor() converts foreign colors to RGB via a match on the concrete class name. Only Cmyk\\Color, Hsv\\Color, Hsl\\Color, Oklab\\Color, Oklch\\Color, NamedColor and Rgb\\Color are handled; any other ColorInterface implementation (typically your own color class) falls to the default arm and throws ColorException. Subclasses of the built-in colors also fail, because the match is on the exact class name.","triggerScenarios":"Calling toColorspace(Rgb\\Colorspace::class) or importColor() with an instance of a custom class implementing ColorInterface, a decorated/proxied color wrapper, or a color class from a third-party package extending the library.","commonSituations":"Adding a custom colorspace or wrapping colors with domain logic; decorator/proxy patterns where $color::class no longer equals a built-in class; subclassing Rgb\\Color expecting the colorspace to still accept it.","solutions":["Pass a native built-in color object (exact class must be one of the seven supported classes)","For custom colors, convert to an Rgb\\Color yourself before calling toColorspace()/importColor()","Unwrap decorators/proxies so the underlying native color instance is passed","Do not subclass built-in colors for conversions; use composition and convert explicitly"],"exampleFix":"// before\n$rgb = (new \\App\\Colors\\MyColor(0.2, 0.4, 0.9))->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);\n\n// after\n$my = new \\App\\Colors\\MyColor(0.2, 0.4, 0.9);\n$rgb = \\Intervention\\Image\\Colors\\Rgb\\Color::create($my->r8(), $my->g8(), $my->b8());","handlingStrategy":"type-guard","validationCode":"$importable = [Cmyk\\Color::class, Hsv\\Color::class, Hsl\\Color::class, Oklab\\Color::class, Oklch\\Color::class, NamedColor::class, Rgb\\Color::class];\nif (!in_array(get_class($color), $importable, true)) {\n    throw new \\RuntimeException('Color type not importable to RGB: ' . get_class($color));\n}\n$rgb = (new Rgb\\Colorspace())->importColor($color);","typeGuard":"function isImportableToRgb(\\Intervention\\Image\\Interfaces\\ColorInterface $color): bool\n{\n    return in_array(get_class($color), [\n        \\Intervention\\Image\\Colors\\Cmyk\\Color::class,\n        \\Intervention\\Image\\Colors\\Hsv\\Color::class,\n        \\Intervention\\Image\\Colors\\Hsl\\Color::class,\n        \\Intervention\\Image\\Colors\\Oklab\\Color::class,\n        \\Intervention\\Image\\Colors\\Oklch\\Color::class,\n        \\Intervention\\Image\\Colors\\Rgb\\NamedColor::class,\n        \\Intervention\\Image\\Colors\\Rgb\\Color::class,\n    ], true); // exact class match; subclasses fail\n}","tryCatchPattern":"try {\n    $rgb = $color->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);\n} catch (\\Intervention\\Image\\Exceptions\\ColorException $e) {\n    // custom color class: convert manually to Rgb\\Color\n    $rgb = \\Intervention\\Image\\Colors\\Rgb\\Color::create($my->r8(), $my->g8(), $my->b8());\n}","preventionTips":["The conversion match is on exact class names; subclasses and custom colors are rejected","Unwrap decorators/proxies before passing colors to colorspace conversion","Convert custom colors to a native Rgb\\Color yourself before toColorspace()"],"tags":["colorspace-conversion","custom-colors","php"],"backgroundTag":"colorspace-conversion-unsupported","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}