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

Invalid oklch() color syntax "{input}"

Error message

Invalid oklch() color syntax "{input}"

What it means

A string starting with 'oklch' failed the decoder's regex (PATTERN in src/Colors/Oklch/Decoders/StringColorDecoder.php:21-28). Accepted syntax: oklch(L C H) with comma or space separators, L as '0', '1', '0.xx' or percent, C as decimal or percent, H as a plain number (0..360) with no unit, plus optional alpha ('/ 0.5', ', 50%', ' 0.5').

Source

Thrown at src/Colors/Oklch/Decoders/StringColorDecoder.php:56

            return false;
        }

        if (!str_starts_with(strtolower($input), 'oklch')) {
            return false;
        }

        return true;
    }

    /**
     * Decode hsl color string.
     *
     * @throws InvalidArgumentException
     */
    public function decode(mixed $input): ColorInterface
    {
        if (preg_match(self::PATTERN, $input, $matches) !== 1) {
            throw new InvalidArgumentException('Invalid oklch() color syntax "' . $input . '"');
        }

        $values = [
            $this->decodeChannelValue($matches['l'], Lightness::class),
            $this->decodeChannelValue($matches['c'], Chroma::class),
            $this->decodeChannelValue($matches['h'], Hue::class),
        ];

        // alpha value
        if (array_key_exists('a', $matches)) {
            $values[] = $this->decodeAlphaChannelValue($matches['a']);
        }

        return new Color(...$values);
    }

    /**
     * Decode channel value.

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Write hue without a unit: 'oklch(0.5, 0.1, 150)'
  2. Express integer lightness as percent ('70%') and negative chroma as decimal or percent
  3. Pre-validate external strings against the supported syntax before parsing
  4. Construct programmatically with Oklch\Color::create(l, c, h, alpha) when no string is needed

Example fix

// before
$color = Oklch\Color::parse('oklch(0.5, 0.1, 150deg)'); // hue unit rejected

// after
$color = Oklch\Color::parse('oklch(0.5, 0.1, 150)');
// or construct directly:
$color = Oklch\Color::create(0.5, 0.1, 150);
Defensive patterns

Strategy: validation

Validate before calling

$pattern = '/^oklch ?\( ?(1|0|0?\.[0-9]+|[0-9.]+%)([, ])(-?0|-?0?\.[0-9.]+|-?[0-9.]+%)\\1[0-9.]+(?: ?\/ ?|[, ] ?)?((?:0\.[0-9]+)|1\.0|\.[0-9]+|[0-9]{1,3}%|1|0)? ?\)$/i';
if (!is_string($input) || preg_match($pattern, $input) !== 1) {
    // strip 'deg' from hue, convert integer lightness to %, then retry parse()
}

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) {
    // normalize input (hue without unit, lightness as %) and retry or reject
}

Prevention

When it happens

Trigger: Failing examples: 'oklch(0.5, 0.1, 150deg)' (hue unit not supported), 'oklch(70, 0.1, 150)' (integer lightness without %), 'oklch(0.5, -1, 150)' (negative integer chroma without %), 'oklch(none 0.1 150)' ('none' keyword), missing closing parenthesis, or a missing channel.

Common situations: Pasting modern CSS oklch() notation (which permits 'deg' on hue, 'none', integer lightness) and assuming full CSS compatibility; feeding unvalidated picker input to the parser.

Related errors


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