{"record":{"id":"379189121dd56831","repo":"Intervention/image","slug":"failed-to-create-color","errorCode":null,"errorMessage":"Failed to create color","messagePattern":"Failed to create color","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\DriverException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/ColorProcessor.php","lineNumber":86,"sourceCode":"            }\n\n            return $pixel;\n        }\n\n        $color = $color->toColorspace(Rgb::class);\n\n        try {\n            return new ImagickPixel(\n                sprintf(\n                    \"srgba(%s, %s, %s, %s)\",\n                    $color->channel(Red::class)->value(),\n                    $color->channel(Green::class)->value(),\n                    $color->channel(Blue::class)->value(),\n                    $color->channel(Alpha::class)->toString(),\n                ),\n            );\n        } catch (ImagickException | ImagickPixelException $e) {\n            throw new DriverException('Failed to create color', previous: $e);\n        }\n    }\n\n    /**\n     * @throws InvalidArgumentException\n     * @throws DriverException\n     * @throws NotSupportedException\n     */\n    public function import(mixed $color): ColorInterface\n    {\n        if (!$color instanceof ImagickPixel) {\n            throw new InvalidArgumentException(\n                'Imagick driver can only process colors from instances of ' . ImagickPixel::class,\n            );\n        }\n\n        try {\n            return match ($this->colorspace::class) {","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/ColorProcessor.php#L68-L104","documentation":"For non-CMYK colorspaces export() converts the color to RGB and formats it as an 'srgba(r, g, b, aa)' string for the ImagickPixel constructor; a construction failure is wrapped as DriverException('Failed to create color'). The usual root cause is an RGB string whose components fall outside 0-255 (or an alpha hex pair the parser rejects) after colorspace conversions rounded out of gamut.","triggerScenarios":"Passing HSL/HSV/Oklab colors near gamut edges whose RGB conversion yields values like -3 or 260; colors carrying NaN/INF floats from degenerate conversions; using such colors for fill, text, or background operations on the Imagick driver.","commonSituations":"Dynamic color inputs from design-tool integrations; user-chosen HSL colors converted for rendering; numeric color arithmetic on raw channel values.","solutions":["Convert to RGB yourself first ($color->toColorspace(Rgb\\Colorspace::class)) and clamp each channel to 0-255 before use.","Validate color inputs at the boundary (hex/named strings decoded by the driver) rather than doing arithmetic on raw channel values.","Catch DriverException and inspect the chained ImagickPixelException, which names the exact rejected component."],"exampleFix":"// before\n$fill = $extremeHslColor; // RGB conversion yields -3..260 → ImagickPixel rejects the srgba() string\n\n// after: convert and clamp before using the color\n$rgb = $extremeHslColor->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);\n$clamp = fn (int $v): int => max(0, min(255, $v));\n$fill = new \\Intervention\\Image\\Colors\\Rgb\\Color(\n    $clamp($rgb->channel(\\Intervention\\Image\\Colors\\Rgb\\Channels\\Red::class)->value()),\n    $clamp($rgb->channel(\\Intervention\\Image\\Colors\\Rgb\\Channels\\Green::class)->value()),\n    $clamp($rgb->channel(\\Intervention\\Image\\Colors\\Rgb\\Channels\\Blue::class)->value()),\n);","handlingStrategy":"try-catch","validationCode":"$rgb = $color->toColorspace(\\Intervention\\Image\\Colors\\Rgb\\Colorspace::class);\n$clamp = fn (int $v): int => max(0, min(255, $v));","typeGuard":null,"tryCatchPattern":"try { $pixel = $processor->export($color); } catch (DriverException $e) { /* chained ImagickPixelException names the rejected srgba() component */ }","preventionTips":["Clamp RGB results of any manual colorspace math to 0-255.","Decode user-facing color strings through the driver instead of arithmetic.","Test rendering with extreme HSL/HSV inputs (pure saturated hues, near-black/white)."],"tags":["imagick","rgb","color","out-of-gamut","srgba"],"backgroundTag":"invalid-color-value","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}