{"record":{"id":"db24de3398b44b80","repo":"Intervention/image","slug":"unable-to-import-color-color-class-to-colorspac-db24de","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/Oklab/Colorspace.php","lineNumber":84,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::importColor()\n     *\n     * @throws ColorException\n     */\n    public function importColor(ColorInterface $color): OklabColor\n    {\n        return match ($color::class) {\n            CmykColor::class,\n            HsvColor::class,\n            NamedColor::class,\n            HslColor::class => $this->importViaRgbColor($color),\n            RgbColor::class => $this->importRgbColor($color),\n            OklchColor::class => $this->importOklchColor($color),\n            OklabColor::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 RGB color OKLAB colorspace.\n     *\n     * @throws ColorException\n     */\n    private function importRgbColor(RgbColor $color): OklabColor\n    {\n        $cbrt = fn(float $x): float => $x < 0 ? -abs($x) ** (1 / 3) : $x ** (1 / 3);\n        $rgbToLinear = fn(float $x): float => $x <= 0.04045 ? $x / 12.92 : (($x + 0.055) / 1.055) ** 2.4;\n\n        $r = $color->red()->normalized();\n        $g = $color->green()->normalized();\n        $b = $color->blue()->normalized();","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Oklab/Colorspace.php#L66-L102","documentation":"Oklab\\Colorspace::importColor() dispatches on the exact class name of the color via match ($color::class) and only knows CmykColor, HsvColor, NamedColor, HslColor, RgbColor, OklchColor and OklabColor; everything else hits the default arm and throws ColorException (src/Colors/Oklab/Colorspace.php:74-88). Note the match is on the exact class, so even a subclass of a known color (e.g. a custom class extending Rgb\\Color) is rejected.","triggerScenarios":"Calling $color->toColorspace(Oklab\\Colorspace::class) or Oklab\\Colorspace::importColor() with a custom ColorInterface implementation, an anonymous subclass of a known color, or a color class added by a newer/older version than the running one.","commonSituations":"Application-defined color value objects passed into library APIs; third-party packages wrapping or extending the color classes; version mismatches where a colorspace class exists but is not listed in this match arm yet.","solutions":["Convert custom colors to a supported class first, typically Rgb\\Color, before toColorspace()","If you subclass a built-in color, pass an instance of the exact base class (compose instead of extend)","Upgrade Intervention Image so newly introduced colorspaces are recognized","Catch ColorException at conversion boundaries and report the unsupported class name"],"exampleFix":"// before\n$oklab = $myCustomColor->toColorspace(Oklab\\Colorspace::class); // ColorException: Unable to import color ...\n\n// after\n// convert your custom color to a supported intermediate first\n$rgb = new Rgb\\Color($myCustomColor->red(), $myCustomColor->green(), $myCustomColor->blue());\n$oklab = $rgb->toColorspace(Oklab\\Colorspace::class);","handlingStrategy":"type-guard","validationCode":"$supported = [\n    CmykColor::class, HsvColor::class, NamedColor::class, HslColor::class,\n    RgbColor::class, OklchColor::class, OklabColor::class,\n];\nif (!in_array($color::class, $supported, true)) {\n    $color = $color->toColorspace(Rgb\\Colorspace::class); // normalize first\n}\n$oklab = $color->toColorspace(Oklab\\Colorspace::class);","typeGuard":"function isImportableToOklab(ColorInterface $color): bool\n{\n    return in_array($color::class, [\n        CmykColor::class, HsvColor::class, NamedColor::class, HslColor::class,\n        RgbColor::class, OklchColor::class, OklabColor::class,\n    ], true); // exact class match, subclasses included in NO arm\n}","tryCatchPattern":"try {\n    $oklab = $color->toColorspace(Oklab\\Colorspace::class);\n} catch (ColorException $e) {\n    // unsupported color class; convert to RgbColor and retry, or report\n}","preventionTips":["Convert domain color objects to RgbColor at the library boundary","Do not subclass built-in colors when passing them to colorspace conversions","Test conversions against every color class your app produces"],"tags":["color","oklab","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"}