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

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

Error message

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

What it means

Oklch\Color::parse() runs the input through an InputHandler registered with only the OKLCH StringColorDecoder; when that pipeline reports it cannot process the input (NotSupportedException) or the decoder cannot be resolved (DriverException), it is rethrown as this InvalidArgumentException (src/Colors/Oklch/Color.php:57-68). A malformed-but-prefixed 'oklch(...)' string instead surfaces the decoder's own 'Invalid oklch() color syntax' error.

Source

Thrown at src/Colors/Oklch/Color.php:64

    public static function create(float|Lightness $l, float|Chroma $c, float|Hue $h, float|Alpha $a = 1): self
    {
        return new self($l, $c, $h, $a);
    }

    /**
     * Parse OKLCH color from string.
     *
     * @throws InvalidArgumentException
     * @throws ColorException
     */
    public static function parse(string $input): self
    {
        try {
            $color = InputHandler::usingDecoders([
                StringColorDecoder::class,
            ])->handle($input);
        } catch (NotSupportedException | DriverException $e) {
            throw new InvalidArgumentException(
                'Unable to parse OKLCH 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. Use the oklch(...) string format: 'oklch(0.5, 0.1, 150)'
  2. For other formats, parse with the matching Color::parse() or the generic pipeline and convert with toColorspace()
  3. Construct directly with Oklch\Color::create(l, c, h)
  4. Wrap parse() in try/catch InvalidArgumentException when input is external

Example fix

// before
$color = Oklch\Color::parse('rgb(255, 85, 0)'); // not an oklch() string

// after
$color = Rgb\Color::parse('rgb(255, 85, 0)')->toColorspace(Oklch\Colorspace::class);
// or construct directly:
$color = Oklch\Color::create(0.65, 0.2, 30);
Defensive patterns

Strategy: validation

Validate before calling

if (!is_string($input) || !preg_match('/^oklch\s*\(/i', $input)) {
    // do not call Oklch\Color::parse(); use a generic parser and convert
}

Type guard

function looksLikeOklchString(mixed $input): bool
{
    return is_string($input) && str_starts_with(strtolower($input), 'oklch');
}

Try / catch

try {
    $color = Oklch\Color::parse($input);
} catch (Intervention\Image\Exceptions\InvalidArgumentException $e) {
    // input not consumable as oklch(); fall back to generic decoding or reject
}

Prevention

When it happens

Trigger: Calling Oklch\Color::parse() with input the oklch decoder chain rejects, e.g. a hex string, color name, or any non-'oklch(...)' color format pushed through the OKLCH-specific parser.

Common situations: Passing '#ff5500' or 'red' to the OKLCH-specific parse() instead of a generic parser; assuming one parse() accepts every color format.

Understand the failure class

Related errors


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