{"record":{"id":"17facb872855ab40","repo":"Intervention/image","slug":"unable-to-import-color-color-class-to-colorspac-17facb","errorCode":null,"errorMessage":"Unable to import color {color_class} to {colorspace_class}","messagePattern":"Unable to import color (.+?) to (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ColorException","httpStatus":null,"severity":"error","filePath":"src/Colors/Oklch/Colorspace.php","lineNumber":89,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::importColor()\n     *\n     * @throws ColorException\n     */\n    public function importColor(ColorInterface $color): OklchColor\n    {\n        return match ($color::class) {\n            CmykColor::class,\n            HsvColor::class,\n            NamedColor::class,\n            HslColor::class => $this->importViaRgbColor($color),\n            OklabColor::class => $this->importOklabColor($color),\n            RgbColor::class => $this->importRgbColor($color),\n            OklchColor::class => $color,\n            default => throw new ColorException(\n                'Unable to import color ' . $color::class . ' to ' . $this::class,\n            ),\n        };\n    }\n\n    /**\n     * Import given OKLAB color OKLCH colorspace.\n     *\n     * @throws ColorException\n     */\n    private function importOklabColor(OklabColor $color): OklchColor\n    {\n        $a = $color->a()->value();\n        $b = $color->b()->value();\n\n        $c = sqrt($a * $a + $b * $b);\n        $h = rad2deg(atan2($b, $a));\n        $h = $h < 0 ? $h + 360 : $h;","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Oklch/Colorspace.php#L71-L107","documentation":"Oklch\\Colorspace::importColor() matches on the exact class of the incoming color and supports only CmykColor, HsvColor, NamedColor, HslColor, OklabColor, RgbColor and OklchColor; any other class hits the default arm and throws ColorException (src/Colors/Oklch/Colorspace.php:79-93). Because the match uses $color::class, subclasses of supported colors are also rejected.","triggerScenarios":"Calling $color->toColorspace(Oklch\\Colorspace::class) with a custom ColorInterface implementation, an anonymous/subclassed color, or a color class unknown to the installed version of the library.","commonSituations":"Application-specific color value objects fed to library APIs; wrappers that extend built-in color classes; upgrading/downgrading across versions where color classes were added.","solutions":["Convert custom colors to Rgb\\Color (or another supported exact class) before toColorspace()","Compose built-in colors instead of subclassing them when interoperating with the library","Keep Intervention Image updated so newer colorspaces are listed in the match","Catch ColorException and handle/report the unsupported class name"],"exampleFix":"// before\n$oklch = $customColor->toColorspace(Oklch\\Colorspace::class); // ColorException: Unable to import color ...\n\n// after\n$rgb = Rgb\\Color::create($customColor->r, $customColor->g, $customColor->b);\n$oklch = $rgb->toColorspace(Oklch\\Colorspace::class);","handlingStrategy":"type-guard","validationCode":"$supported = [\n    CmykColor::class, HsvColor::class, NamedColor::class, HslColor::class,\n    OklabColor::class, RgbColor::class, OklchColor::class,\n];\nif (!in_array($color::class, $supported, true)) {\n    $color = $color->toColorspace(Rgb\\Colorspace::class);\n}\n$oklch = $color->toColorspace(Oklch\\Colorspace::class);","typeGuard":"function isImportableToOklch(ColorInterface $color): bool\n{\n    return in_array($color::class, [\n        CmykColor::class, HsvColor::class, NamedColor::class, HslColor::class,\n        OklabColor::class, RgbColor::class, OklchColor::class,\n    ], true);\n}","tryCatchPattern":"try {\n    $oklch = $color->toColorspace(Oklch\\Colorspace::class);\n} catch (ColorException $e) {\n    // unsupported class: convert to RgbColor and retry, or report upstream\n}","preventionTips":["Map domain color objects onto RgbColor at the library boundary","Prefer composition over subclassing built-in color classes","Keep the library version in sync with the colorspaces you convert to"],"tags":["color","oklch","colorspace","unsupported-type","conversion","php"],"backgroundTag":"unsupported-color-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}