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

Invalid oklab() color syntax "{input}"

Error message

Invalid oklab() color syntax "{input}"

What it means

A string starting with 'oklab' failed the decoder's strict regex (PATTERN in src/Colors/Oklab/Decoders/StringColorDecoder.php:21-28). Supported syntax: oklab(L A B) with comma or space separators, where L must be '0', '1', '0.xx' or a percentage like '50%', A and B must be decimal form (0, -0.4, 0.123) or percentages, and an optional alpha ('/ 0.5', ', 50%', ' 0.5') may follow.

Source

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

            return false;
        }

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

        return true;
    }

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

        $values = [
            $this->decodeChannelValue($matches['l'], Lightness::class),
            $this->decodeChannelValue($matches['a'], A::class),
            $this->decodeChannelValue($matches['b'], B::class),
        ];

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

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

    /**
     * Decode channel value.

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Use the accepted forms: 'oklab(0.5, 0.1, 0.1)', 'oklab(50% 0.1 -0.05 / 0.5)'
  2. Express integer lightness as a percentage ('50%') and negative integer axes as decimals ('-1' -> '-1%' or '-0.4')
  3. Pre-validate user input with a regex mirroring the supported syntax
  4. Build colors programmatically with Oklab\Color::create(l, a, b, alpha) instead of parsing strings

Example fix

// before
$color = Oklab\Color::parse('oklab(50, 0.1, 0.1)'); // integer lightness rejected

// after
$color = Oklab\Color::parse('oklab(50%, 0.1, 0.1)');
// or construct directly:
$color = Oklab\Color::create(0.5, 0.1, 0.1);
Defensive patterns

Strategy: validation

Validate before calling

$pattern = '/^oklab ?\( ?(1|0|0?\.[0-9]+|[0-9.]+%)([, ])(-?0|-?0?\.[0-9.]+|-?[0-9.]+%)\\1(-?0|-?0?\.[0-9.]+|-?[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) {
    // reject or normalize (e.g. 'oklab(50, ...)' -> 'oklab(50%, ...)') before parse()
}

Type guard

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

Try / catch

try {
    $color = Oklab\Color::parse($input);
} catch (Intervention\Image\Exceptions\InvalidArgumentException $e) {
    // reformat the string (percent lightness, decimal axes) and retry or reject
}

Prevention

When it happens

Trigger: Examples that fail: 'oklab(50, 0.1, 0.1)' (integer lightness without %), 'oklab(0.5, -1, 0.1)' (negative integer a/b without %), 'oklab(0.5, 0.1)' (missing b), 'oklab(none 0.1 0.1)' (CSS 'none' keyword), 'oklab(0.5, 0.1, 0.1 / )' (empty alpha), or a missing closing parenthesis.

Common situations: Copying modern CSS oklab() syntax (which allows integers, 'none', 'deg' variants) and assuming the library accepts every CSS form; unvalidated user input in color pickers feeding the parser.

Related errors


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