{"record":{"id":"ee8831fdd6b07fa1","repo":"Intervention/image","slug":"unable-to-import-color-color-class-to-colorspac","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/Hsv/Colorspace.php","lineNumber":86,"sourceCode":"    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::importColor()\n     *\n     * @throws InvalidArgumentException\n     * @throws ColorException\n     */\n    public function importColor(ColorInterface $color): HsvColor\n    {\n        return match ($color::class) {\n            CmykColor::class,\n            OklchColor::class,\n            NamedColor::class,\n            OklabColor::class => $this->importViaRgbColor($color),\n            RgbColor::class => $this->importRgbColor($color),\n            HslColor::class => $this->importHslColor($color),\n            HsvColor::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 to HSV colorspace.\n     *\n     * @throws ColorException\n     */\n    private function importRgbColor(RgbColor $color): HsvColor\n    {\n        // normalized values of rgb channels\n        $values = array_map(\n            fn(ColorChannelInterface $channel): float => $channel->normalized(),\n            $color->channels(),\n        );\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Hsv/Colorspace.php#L68-L104","documentation":"Hsv\\Colorspace::importColor() matches the concrete class of the incoming color against the supported built-ins (HsvColor, HslColor, RgbColor, NamedColor, OklabColor, OklchColor) and throws this ColorException ('Unable to import color X to Y') for anything else. It is a supported-type rejection: no conversion is even attempted for unrecognized ColorInterface implementations.","triggerScenarios":"$color->toColorspace(Hsv\\Colorspace::class) where $color is a custom class implementing ColorInterface directly (an adapter, decorator, or DTO) instead of extending one of the built-in color classes.","commonSituations":"Wrapper colors for caching/serialization/ORM embedding; migration code from other color libraries; test doubles implementing the interface; subclasses of AbstractColor that are not one of the six matched classes.","solutions":["Convert to a built-in first: turn your custom color into an RgbColor, then ->toColorspace(Hsv\\Colorspace::class)","Unwrap decorators and import the underlying built-in color instance","Extend a supported class (e.g. Hsv\\Color or Rgb\\Color) so the match arm recognizes your type","Handle your custom type in your own code and construct Hsv\\Color::create(...) directly"],"exampleFix":"// before\n$hsv = $customColor->toColorspace(Hsv\\Colorspace::class); // custom class, no match arm\n\n// after\n$rgb = new RgbColor(...$customColor->rgbComponents()); // built-in color\n$hsv = $rgb->toColorspace(Hsv\\Colorspace::class);","handlingStrategy":"type-guard","validationCode":"$supported = [HsvColor::class, HslColor::class, RgbColor::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\n}","typeGuard":"function isHsvImportable(ColorInterface $color): bool\n{\n    return $color instanceof HsvColor\n        || $color instanceof HslColor\n        || $color instanceof RgbColor\n        || $color instanceof NamedColor\n        || $color instanceof OklabColor\n        || $color instanceof OklchColor;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\ColorException;\n\ntry {\n    $hsv = $color->toColorspace(Hsv\\Colorspace::class);\n} catch (ColorException $e) {\n    // unsupported source class; convert manually or reject\n}","preventionTips":["Convert custom/wrapped colors to RgbColor before colorspace imports","Extend a built-in color class rather than reimplementing ColorInterface","Unwrap decorators at the library boundary"],"tags":["php","intervention-image","color","hsv","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"}