{"record":{"id":"65d742e75572552e","repo":"Intervention/image","slug":"number-of-color-channels-must-be-3-or-4-for-class-65d742","errorCode":null,"errorMessage":"Number of color channels must be 3 or 4 for {class}","messagePattern":"Number of color channels must be 3 or 4 for (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/Oklab/Colorspace.php","lineNumber":45,"sourceCode":"     */\n    public static array $channels = [\n        Channels\\Lightness::class,\n        Channels\\A::class,\n        Channels\\B::class,\n        Channels\\Alpha::class,\n    ];\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::colorFromNormalized()\n     *\n     * @throws InvalidArgumentException\n     */\n    public static function colorFromNormalized(array $normalized): OklabColor\n    {\n        if (!in_array(count($normalized), [3, 4])) {\n            throw new InvalidArgumentException('Number of color channels must be 3 or 4 for ' . static::class);\n        }\n\n        // add alpha value if missing\n        $normalized = count($normalized) === 3 ? array_pad($normalized, 4, 1) : $normalized;\n\n        return new Color(...array_map(\n            function (string $channel, null|float $normalized) {\n                try {\n                    return $channel::fromNormalized($normalized);\n                } catch (TypeError $e) {\n                    throw new InvalidArgumentException(\n                        'Normalized color value must be in range 0 to 1',\n                        previous: $e,\n                    );\n                }\n            },\n            self::$channels,\n            $normalized,","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Oklab/Colorspace.php#L27-L63","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Pass exactly [l, a, b] or [l, a, b, alpha] with normalized floats in 0..1","Check count($normalized) === 3 || count($normalized) === 4 before calling","When values may be missing, default alpha explicitly instead of appending extra entries","Prefer constructing via new Oklab\\Color(l, a, b) when you have real channel values"],"exampleFix":"// before\n$color = Oklab\\Colorspace::colorFromNormalized([0.5, 0.1]); // throws: must be 3 or 4\n\n// after\n$color = Oklab\\Colorspace::colorFromNormalized([0.5, 0.1, 0.0]);\n// or with alpha:\n$color = Oklab\\Colorspace::colorFromNormalized([0.5, 0.1, 0.0, 1.0]);","handlingStrategy":"validation","validationCode":"if (count($normalized) !== 3 && count($normalized) !== 4) {\n    throw new \\UnexpectedValueException('Expected [l, a, b(, alpha)] normalized values.');\n}\n$color = Oklab\\Colorspace::colorFromNormalized($normalized);","typeGuard":"function isOklabNormalizedArray(mixed $values): bool\n{\n    return is_array($values) && in_array(count($values), [3, 4], true);\n}","tryCatchPattern":null,"preventionTips":["Assemble [l, a, b] arrays explicitly rather than filtering/compressing dynamically","Add a count assertion right after building the array from external data","Remember alpha is optional as the 4th entry and defaults to 1"],"tags":["color","oklab","colorspace","channel-count","validation","php"],"backgroundTag":"invalid-channel-count","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}