{"record":{"id":"7c47e1ffb93b143a","repo":"Intervention/image","slug":"unable-to-import-color-colorclass-to-class-7c47e1","errorCode":null,"errorMessage":"Unable to import color {colorClass} to {class}","messagePattern":"Unable to import color (.+?) to (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ColorException","httpStatus":null,"severity":"error","filePath":"src/Colors/Hsl/Colorspace.php","lineNumber":85,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::importColor()\n     *\n     * @throws ColorException\n     */\n    public function importColor(ColorInterface $color): HslColor\n    {\n        return match ($color::class) {\n            HslColor::class => $color,\n            OklchColor::class,\n            OklabColor::class,\n            NamedColor::class,\n            CmykColor::class => $this->importViaRgbColor($color),\n            RgbColor::class => $this->importRgbColor($color),\n            HsvColor::class => $this->importHsvColor($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 to HSL colorspace.\n     *\n     * @throws ColorException\n     */\n    private function importRgbColor(RgbColor $color): HslColor\n    {\n        // normalized values of rgb channels\n        $values = array_map(\n            fn(ColorChannelInterface $channel): float => $channel->normalized(),\n            $color->channels(),\n        );\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Hsl/Colorspace.php#L67-L103","documentation":"Hsl\\Colorspace::importColor() dispatches on the concrete class of the incoming color and supports only HslColor, HsvColor, RgbColor, CmykColor, NamedColor, OklabColor and OklchColor. Any other ColorInterface implementation falls through to a default arm that throws this ColorException ('Unable to import color X to Y'). It is a supported-type check, not a data validation failure.","triggerScenarios":"$color->toColorspace(Hsl\\Colorspace::class) where $color is a custom class implementing ColorInterface but extending none of the supported built-ins - e.g. a decorator, a legacy adapter, or a subclass of AbstractColor that is not one of the seven listed classes.","commonSituations":"Value objects wrapping a library color for caching, DTOs, or ORM embedding; projects upgrading custom color code from earlier library versions; test doubles implementing the interface directly.","solutions":["Convert through RGB first: $color->toColorspace(Rgb\\Colorspace::class) returns a supported RgbColor (if your class delegates conversion), then ->toColorspace(Hsl\\Colorspace::class)","Unwrap decorators and pass the underlying built-in color instance to the import","Make your custom class extend one of the supported colors (e.g. extend HslColor or RgbColor) so the match arm hits","Register/handle your type before calling importColor and convert it yourself to Hsl\\Color::create(...)"],"exampleFix":"// before\n$hsl = $customColor->toColorspace(Hsl\\Colorspace::class); // custom class not matched\n\n// after\n$rgb = $customColor->toRgb(); // your own conversion to a built-in RgbColor\n$hsl = $rgb->toColorspace(Hsl\\Colorspace::class);","handlingStrategy":"type-guard","validationCode":"$supported = [HslColor::class, HsvColor::class, RgbColor::class, CmykColor::class, NamedColor::class, OklabColor::class, OklchColor::class];\nif (!in_array(get_class($color), $supported, true)) {\n    $color = $color->toColorspace(Rgb\\Colorspace::class); // normalize to a built-in first\n}","typeGuard":"function isHslImportable(ColorInterface $color): bool\n{\n    return $color instanceof HslColor\n        || $color instanceof HsvColor\n        || $color instanceof RgbColor\n        || $color instanceof CmykColor\n        || $color instanceof NamedColor\n        || $color instanceof OklabColor\n        || $color instanceof OklchColor;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\ColorException;\n\ntry {\n    $hsl = $color->toColorspace(Hsl\\Colorspace::class);\n} catch (ColorException $e) {\n    // unsupported source class; convert manually or reject\n}","preventionTips":["Convert custom colors to a built-in RgbColor at your system boundary","Prefer extending a built-in color over implementing ColorInterface from scratch","Unwrap decorators before handing colors to the library"],"tags":["php","intervention-image","color","hsl","colorspace-conversion","unsupported-type"],"backgroundTag":"unsupported-color-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}