{"record":{"id":"eb62a08eac4aab1a","repo":"Intervention/image","slug":"failed-to-import-color-color-class-to-this-cl","errorCode":null,"errorMessage":"Failed to import color {color::class} to {this::class}","messagePattern":"Failed to import color (.+?) to (.+?)","errorType":"exception","errorClass":"ColorException","httpStatus":null,"severity":"error","filePath":"src/Colors/Rgb/Colorspace.php","lineNumber":107,"sourceCode":"                'Failed to import color ' . $color::class . ' to ' . $this::class,\n            ),\n        };\n    }\n\n    /**\n     * @throws ColorException\n     */\n    private function importCmykColor(CmykColor $color): RgbColor\n    {\n        try {\n            return new Color(\n                (int) (255 * (1 - $color->cyan()->normalized()) * (1 - $color->key()->normalized())),\n                (int) (255 * (1 - $color->magenta()->normalized()) * (1 - $color->key()->normalized())),\n                (int) (255 * (1 - $color->yellow()->normalized()) * (1 - $color->key()->normalized())),\n                $color->alpha()->normalized(),\n            );\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 HSV color to RGB color space.\n     *\n     * @throws ColorException\n     */\n    private function importHsvColor(HsvColor $color): RgbColor\n    {\n        $chroma = $color->value()->normalized() * $color->saturation()->normalized();\n        $hue = $color->hue()->normalized() * 6;\n        $x = $chroma * (1 - abs(fmod($hue, 2) - 1));\n\n        // connect channel values","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Rgb/Colorspace.php#L89-L125","documentation":"During CMYK to RGB conversion, the computed 8-bit channel values plus the source alpha are passed to the Rgb\\Color constructor; if that constructor rejects a value, the InvalidArgumentException is rethrown as this ColorException with the original chained. Because the conversion multiplies already-normalized, validated channel values, this is a defensive failure that in practice points at a corrupted or hand-crafted CMYK color whose channel objects bypassed normal validation.","triggerScenarios":"A Cmyk\\Color carrying custom channel objects with out-of-range values (e.g. an injected Alpha channel whose normalized value is outside 0..1) followed by toColorspace(Rgb\\Colorspace::class) or importColor().","commonSituations":"Injecting custom ColorChannelInterface objects into a Cmyk\\Color instead of ints/floats; colors restored from corrupted serialized payloads or caches; colors received from a modified/patched fork of the library.","solutions":["Inspect $e->getPrevious() to see which channel value the RGB constructor rejected","Rebuild the source color through validated construction, e.g. Cmyk\\Colorspace::colorFromNormalized(array_map(fn($c) => $c->normalized(), $color->channels()))","Construct CMYK colors from integers 0..100 for the color channels and a 0..1 alpha float only"],"exampleFix":"// before\n$cmyk = new Cmyk\\Color(new Cyan(150), 50, 50, 10); // custom out-of-range channel object\n$rgb = $cmyk->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);\n\n// after\n$cmyk = new Cmyk\\Color(50, 50, 50, 10);\n$rgb = $cmyk->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);","handlingStrategy":"try-catch","validationCode":"$alpha = $cmyk->alpha()->normalized();\nif ($alpha < 0.0 || $alpha > 1.0) {\n    throw new \\RuntimeException('CMYK alpha out of range');\n}\n$rgb = $cmyk->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);","typeGuard":null,"tryCatchPattern":"try {\n    $rgb = $cmyk->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);\n} catch (\\Intervention\\Image\\Exceptions\\ColorException $e) {\n    $previous = $e->getPrevious(); // underlying InvalidArgumentException, log it\n    $safe = \\Intervention\\Image\\Colors\\Cmyk\\Colorspace::colorFromNormalized(\n        array_map(fn($c) => $c->normalized(), $cmyk->channels())\n    );\n    $rgb = $safe->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);\n}","preventionTips":["Build CMYK colors only from validated ints (0..100) and a 0..1 alpha float","Do not inject custom channel objects into built-in color classes","Always log getPrevious() when a ColorException wraps an inner failure"],"tags":["colorspace-conversion","cmyk","rgb","php"],"backgroundTag":"colorspace-conversion-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}