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

Unable to parse RGB color from input "{input}"

Error message

Unable to parse RGB color from input "{input}"

What it means

Rgb\Color::parse() runs the input through three decoders (rgb() strings, named CSS colors, hex strings). This wrapped error appears when no decoder even claims the input, i.e. the InputHandler throws NotSupportedException ('Unprocessable input') or a DriverException; the original exception is chained and readable via $e->getPrevious(). Note the distinction: input that looks like an rgb() string or hex but has bad syntax makes the decoder itself throw its own InvalidArgumentException, which propagates unwrapped.

Source

Thrown at src/Colors/Rgb/Color.php:67

        return new self($r, $g, $b, $a);
    }

    /**
     * Parse RGB color from string.
     *
     * @throws InvalidArgumentException
     * @throws ColorException
     */
    public static function parse(string $input): self
    {
        try {
            $color = InputHandler::usingDecoders([
                StringColorDecoder::class,
                NamedColorDecoder::class,
                HexColorDecoder::class,
            ])->handle($input);
        } catch (NotSupportedException | DriverException $e) {
            throw new InvalidArgumentException(
                'Unable to parse RGB color from input "' . $input . '"',
                previous: $e,
            );
        }

        if (!$color instanceof self) {
            throw new ColorException('Result must be instance of ' . self::class);
        }

        return $color;
    }

    /**
     * {@inheritdoc}
     *
     * @see ColorInterface::colorspace()
     */
    public function colorspace(): ColorspaceInterface

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Feed a supported format: 'rgb(255 0 0)', a named color like 'tomato', or hex like '#ff0000'
  2. For other colorspaces, parse with the matching class then convert, e.g. Hsl\Color::parse('hsl(0, 100%, 50%)')->toColorspace(Rgb\Colorspace::class)
  3. Inspect $e->getPrevious() to find which stage rejected the input
  4. Validate free-form input against the expected formats before calling parse()

Example fix

// before
$color = \Intervention\Image\Colors\Rgb\Color::parse('hsl(0, 100%, 50%)');

// after
$color = \Intervention\Image\Colors\Hsl\Color::parse('hsl(0, 100%, 50%)')
    ->toColorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
Defensive patterns

Strategy: try-catch

Validate before calling

$ok = is_string($input)
    && (
        preg_match('/^s?rgba?\s*\(/i', $input) === 1
        || preg_match('/^#?([a-f\d]{3}|[a-f\d]{4}|[a-f\d]{6}|[a-f\d]{8})$/i', $input) === 1
        || \Intervention\Image\Colors\Rgb\Decoders\NamedColorDecoder::class !== null // named colors: consult CSS name list
    );
if (!$ok) {
    throw new \RuntimeException('Unsupported color format');
}

Try / catch

use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\ColorException;

try {
    $color = \Intervention\Image\Colors\Rgb\Color::parse($input);
} catch (InvalidArgumentException | ColorException $e) {
    $color = \Intervention\Image\Colors\Rgb\Color::create(0, 0, 0); // or report validation error
}

Prevention

When it happens

Trigger: Rgb\Color::parse('xyz'), parse('hsl(0, 50%, 50%)') (the RGB parser only handles rgb()/named/hex), parse('12,34,56') or any free-form string matched by no decoder's supports().

Common situations: Parsing user-supplied color values from forms, spreadsheets or CMS fields; assuming the RGB parser accepts every CSS color format (hsl(), cmyk(), oklch() each have their own parse methods); migrating from v2 where color creation accepted looser input.

Understand the failure class

Related errors


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