{"record":{"id":"4463bf6d1cbf0908","repo":"Intervention/image","slug":"failed-to-import-color-colorclass-to-class","errorCode":null,"errorMessage":"Failed to import color {colorClass} to {class}","messagePattern":"Failed to import color (.+?) to (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ColorException","httpStatus":null,"severity":"error","filePath":"src/Colors/Cmyk/Colorspace.php","lineNumber":110,"sourceCode":"     * Import given RGB color to CMYK colorspace.\n     *\n     * @throws ColorException\n     */\n    private function importRgbColor(RgbColor $color): CmykColor\n    {\n        $c = (255 - $color->red()->value()) / 255.0 * 100;\n        $m = (255 - $color->green()->value()) / 255.0 * 100;\n        $y = (255 - $color->blue()->value()) / 255.0 * 100;\n        $k = intval(round(min([$c, $m, $y])));\n\n        $c = intval(round($c - $k));\n        $m = intval(round($m - $k));\n        $y = intval(round($y - $k));\n\n        try {\n            return new CmykColor($c, $m, $y, $k, $color->alpha()->normalized());\n        } catch (InvalidArgumentException $e) {\n            throw new ColorException(\n                'Failed to import color ' . $color::class . ' to ' . $this::class,\n                previous: $e,\n            );\n        }\n    }\n\n    /**\n     * Import given color to CMYK colorspace by converting it to RGB first.\n     *\n     * @throws ColorException\n     */\n    private function importViaRgbColor(NamedColor|OklabColor|OklchColor|HslColor|HsvColor $color): CmykColor\n    {\n        try {\n            $color = $color->toColorspace(RgbColorspace::class);\n        } catch (InvalidArgumentException $e) {\n            throw new ColorException(\n                'Failed to import color ' . $color::class . ' to ' . $this::class,","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Cmyk/Colorspace.php#L92-L128","documentation":"Thrown when Intervention Image converts an RgbColor to CMYK and the resulting Cmyk\\Color constructor rejects the computed values. The original InvalidArgumentException (channel outside 0-100 or alpha outside 0.0-1.0) is re-thrown as a ColorException with the message 'Failed to import color ... to ...'. With a well-formed RgbColor this is nearly unreachable; it appears when the source color's alpha()->normalized() returns a value outside [0,1] or when degenerate/NaN values leak into the conversion math.","triggerScenarios":"Calling $rgbColor->toColorspace(Cmyk\\Colorspace::class) or (new Cmyk\\Colorspace())->importColor($rgbColor) where the source color object carries an out-of-range alpha (e.g. a custom ColorInterface implementation returning 1.5 or -0.1 from alpha()->normalized()), or float corruption (INF/NaN) in the red/green/blue channels.","commonSituations":"Custom color classes wrapping or decorating a library color (caching/serialization wrappers) that forward channel calls incorrectly; colors rebuilt from unserialized or database-stored channel values without re-validation; unit tests feeding hand-made color mocks with invalid alpha.","solutions":["Inspect $e->getPrevious() in the caught ColorException - it carries the exact constructor message telling which channel was out of range","Fix the source color so alpha()->normalized() returns a float in [0,1] and all channel values are within their documented bounds","If you convert user-supplied channel values, clamp them before building the color: $alpha = min(1.0, max(0.0, $alpha))","As a last resort build the CMYK color manually with Cmyk\\Color::create(int $c, int $m, int $y, int $k) after validating each value is an integer in 0-100"],"exampleFix":"// before (custom color returns unvalidated alpha)\n$alpha = $this->storedAlpha * 1.5; // may exceed 1.0\nreturn new RgbColor(255, 0, 0, $alpha);\n\n// after\n$alpha = min(1.0, max(0.0, $this->storedAlpha));\nreturn new RgbColor(255, 0, 0, $alpha);","handlingStrategy":"try-catch","validationCode":"$alpha = $sourceColor->alpha()->normalized();\nif (!is_finite($alpha) || $alpha < 0.0 || $alpha > 1.0) {\n    throw new RuntimeException('Source color alpha out of range: ' . var_export($alpha, true));\n}","typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\ColorException;\n\ntry {\n    $cmyk = $color->toColorspace(Cmyk\\Colorspace::class);\n} catch (ColorException $e) {\n    $reason = $e->getPrevious()?->getMessage() ?? $e->getMessage();\n    // log $reason; fall back to a default CMYK color\n    $cmyk = Cmyk\\Color::create(0, 0, 0, 0);\n}","preventionTips":["Always inspect getPrevious() on ColorException - it names the exact failing channel","Keep alpha on the 0.0-1.0 normalized scale everywhere in your domain model","Never hand-construct channel objects; use the validating Color::create() factories"],"tags":["php","intervention-image","color","cmyk","colorspace-conversion"],"backgroundTag":"colorspace-conversion-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}