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

Number of color channels must be 3 or 4 for {class}

Error message

Number of color channels must be 3 or 4 for {class}

What it means

Oklab\Colorspace::colorFromNormalized() builds a color from an array of normalized channel values and requires exactly 3 entries (lightness, a, b) or 4 entries (plus alpha), padding alpha to 1 when missing (src/Colors/Oklab/Colorspace.php:42-49). Any other count throws InvalidArgumentException before any value is inspected.

Source

Thrown at src/Colors/Oklab/Colorspace.php:45

     */
    public static array $channels = [
        Channels\Lightness::class,
        Channels\A::class,
        Channels\B::class,
        Channels\Alpha::class,
    ];

    /**
     * {@inheritdoc}
     *
     * @see ColorspaceInterface::colorFromNormalized()
     *
     * @throws InvalidArgumentException
     */
    public static function colorFromNormalized(array $normalized): OklabColor
    {
        if (!in_array(count($normalized), [3, 4])) {
            throw new InvalidArgumentException('Number of color channels must be 3 or 4 for ' . static::class);
        }

        // add alpha value if missing
        $normalized = count($normalized) === 3 ? array_pad($normalized, 4, 1) : $normalized;

        return new Color(...array_map(
            function (string $channel, null|float $normalized) {
                try {
                    return $channel::fromNormalized($normalized);
                } catch (TypeError $e) {
                    throw new InvalidArgumentException(
                        'Normalized color value must be in range 0 to 1',
                        previous: $e,
                    );
                }
            },
            self::$channels,
            $normalized,

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Pass exactly [l, a, b] or [l, a, b, alpha] with normalized floats in 0..1
  2. Check count($normalized) === 3 || count($normalized) === 4 before calling
  3. When values may be missing, default alpha explicitly instead of appending extra entries
  4. Prefer constructing via new Oklab\Color(l, a, b) when you have real channel values

Example fix

// before
$color = Oklab\Colorspace::colorFromNormalized([0.5, 0.1]); // throws: must be 3 or 4

// after
$color = Oklab\Colorspace::colorFromNormalized([0.5, 0.1, 0.0]);
// or with alpha:
$color = Oklab\Colorspace::colorFromNormalized([0.5, 0.1, 0.0, 1.0]);
Defensive patterns

Strategy: validation

Validate before calling

if (count($normalized) !== 3 && count($normalized) !== 4) {
    throw new \UnexpectedValueException('Expected [l, a, b(, alpha)] normalized values.');
}
$color = Oklab\Colorspace::colorFromNormalized($normalized);

Type guard

function isOklabNormalizedArray(mixed $values): bool
{
    return is_array($values) && in_array(count($values), [3, 4], true);
}

Prevention

When it happens

Trigger: Calling Oklab\Colorspace::colorFromNormalized([0.5, 0.1]) with 2 values, with 5+ values, or with an empty array; also when user code miscalculates a channel array (e.g. array_filter() removing entries) before calling it.

Common situations: Dynamically assembled channel arrays where a value is dropped or an extra key appended; porting code from another colorspace that expected a different channel count (RGB also uses 3-4 but CMYK-like arrays of 5 fail here).

Related errors


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