Intervention/image · error · Intervention\Image\Exceptions\DriverException

Failed to create color

Error message

Failed to create color

What it means

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.

Source

Thrown at src/Drivers/Imagick/ColorProcessor.php:86

            }

            return $pixel;
        }

        $color = $color->toColorspace(Rgb::class);

        try {
            return new ImagickPixel(
                sprintf(
                    "srgba(%s, %s, %s, %s)",
                    $color->channel(Red::class)->value(),
                    $color->channel(Green::class)->value(),
                    $color->channel(Blue::class)->value(),
                    $color->channel(Alpha::class)->toString(),
                ),
            );
        } catch (ImagickException | ImagickPixelException $e) {
            throw new DriverException('Failed to create color', previous: $e);
        }
    }

    /**
     * @throws InvalidArgumentException
     * @throws DriverException
     * @throws NotSupportedException
     */
    public function import(mixed $color): ColorInterface
    {
        if (!$color instanceof ImagickPixel) {
            throw new InvalidArgumentException(
                'Imagick driver can only process colors from instances of ' . ImagickPixel::class,
            );
        }

        try {
            return match ($this->colorspace::class) {

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Convert to RGB yourself first ($color->toColorspace(Rgb\Colorspace::class)) and clamp each channel to 0-255 before use.
  2. Validate color inputs at the boundary (hex/named strings decoded by the driver) rather than doing arithmetic on raw channel values.
  3. Catch DriverException and inspect the chained ImagickPixelException, which names the exact rejected component.

Example fix

// before
$fill = $extremeHslColor; // RGB conversion yields -3..260 → ImagickPixel rejects the srgba() string

// after: convert and clamp before using the color
$rgb = $extremeHslColor->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
$clamp = fn (int $v): int => max(0, min(255, $v));
$fill = new \Intervention\Image\Colors\Rgb\Color(
    $clamp($rgb->channel(\Intervention\Image\Colors\Rgb\Channels\Red::class)->value()),
    $clamp($rgb->channel(\Intervention\Image\Colors\Rgb\Channels\Green::class)->value()),
    $clamp($rgb->channel(\Intervention\Image\Colors\Rgb\Channels\Blue::class)->value()),
);
Defensive patterns

Strategy: try-catch

Validate before calling

$rgb = $color->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
$clamp = fn (int $v): int => max(0, min(255, $v));

Try / catch

try { $pixel = $processor->export($color); } catch (DriverException $e) { /* chained ImagickPixelException names the rejected srgba() component */ }

Prevention

When it happens

Trigger: 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.

Common situations: Dynamic color inputs from design-tool integrations; user-chosen HSL colors converted for rendering; numeric color arithmetic on raw channel values.

Related errors


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/379189121dd56831. Report an issue: GitHub.