Intervention/image · error · ColorException

Failed to import color {color::class} to {this::class}

Error message

Failed to import color {color::class} to {this::class}

What it means

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.

Source

Thrown at src/Colors/Rgb/Colorspace.php:107

                'Failed to import color ' . $color::class . ' to ' . $this::class,
            ),
        };
    }

    /**
     * @throws ColorException
     */
    private function importCmykColor(CmykColor $color): RgbColor
    {
        try {
            return new Color(
                (int) (255 * (1 - $color->cyan()->normalized()) * (1 - $color->key()->normalized())),
                (int) (255 * (1 - $color->magenta()->normalized()) * (1 - $color->key()->normalized())),
                (int) (255 * (1 - $color->yellow()->normalized()) * (1 - $color->key()->normalized())),
                $color->alpha()->normalized(),
            );
        } catch (InvalidArgumentException $e) {
            throw new ColorException(
                'Failed to import color ' . $color::class . ' to ' . $this::class,
                previous: $e,
            );
        }
    }

    /**
     * Import given HSV color to RGB color space.
     *
     * @throws ColorException
     */
    private function importHsvColor(HsvColor $color): RgbColor
    {
        $chroma = $color->value()->normalized() * $color->saturation()->normalized();
        $hue = $color->hue()->normalized() * 6;
        $x = $chroma * (1 - abs(fmod($hue, 2) - 1));

        // connect channel values

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Inspect $e->getPrevious() to see which channel value the RGB constructor rejected
  2. Rebuild the source color through validated construction, e.g. Cmyk\Colorspace::colorFromNormalized(array_map(fn($c) => $c->normalized(), $color->channels()))
  3. Construct CMYK colors from integers 0..100 for the color channels and a 0..1 alpha float only

Example fix

// before
$cmyk = new Cmyk\Color(new Cyan(150), 50, 50, 10); // custom out-of-range channel object
$rgb = $cmyk->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);

// after
$cmyk = new Cmyk\Color(50, 50, 50, 10);
$rgb = $cmyk->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
Defensive patterns

Strategy: try-catch

Validate before calling

$alpha = $cmyk->alpha()->normalized();
if ($alpha < 0.0 || $alpha > 1.0) {
    throw new \RuntimeException('CMYK alpha out of range');
}
$rgb = $cmyk->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);

Try / catch

try {
    $rgb = $cmyk->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
} catch (\Intervention\Image\Exceptions\ColorException $e) {
    $previous = $e->getPrevious(); // underlying InvalidArgumentException, log it
    $safe = \Intervention\Image\Colors\Cmyk\Colorspace::colorFromNormalized(
        array_map(fn($c) => $c->normalized(), $cmyk->channels())
    );
    $rgb = $safe->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
}

Prevention

When it happens

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

Common situations: 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.

Related errors


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