{"record":{"id":"810e61f80e1c9e5c","repo":"Intervention/image","slug":"number-of-color-channels-must-be-3-or-4-for-class-810e61","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/Oklch/Colorspace.php","lineNumber":50,"sourceCode":"     */\n    public static array $channels = [\n        Lightness::class,\n        Chroma::class,\n        Hue::class,\n        Alpha::class,\n    ];\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::colorFromNormalized()\n     *\n     * @throws InvalidArgumentException\n     */\n    public static function colorFromNormalized(array $normalized): OklchColor\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":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Oklch/Colorspace.php#L32-L68","documentation":"Oklch\\Colorspace::colorFromNormalized() accepts exactly 3 normalized values (lightness, chroma, hue) or 4 (plus alpha), defaulting alpha to 1 when absent (src/Colors/Oklch/Colorspace.php:47-54). Any other array length throws InvalidArgumentException before values are examined.","triggerScenarios":"Calling Oklch\\Colorspace::colorFromNormalized() with 2 or 5+ entries or an empty array, e.g. after an array_map/array_filter step accidentally changed the array size.","commonSituations":"Dynamically built channel arrays where keys go missing or extra values are appended; code ported from RGB (3 channels) into OKLCH without adding hue, or from CMYK (4+alpha) without trimming.","solutions":["Pass exactly [l, c, h] or [l, c, h, alpha] as normalized floats","Guard with count($values) === 3 || count($values) === 4 before the call","Use new Oklch\\Color(l, c, h) when you hold real channel values"],"exampleFix":"// before\n$color = Oklch\\Colorspace::colorFromNormalized([0.5, 0.2]); // throws: must be 3 or 4\n\n// after\n$color = Oklch\\Colorspace::colorFromNormalized([0.5, 0.2, 30]);\n// or with alpha:\n$color = Oklch\\Colorspace::colorFromNormalized([0.5, 0.2, 30, 1.0]);","handlingStrategy":"validation","validationCode":"if (count($normalized) !== 3 && count($normalized) !== 4) {\n    throw new \\UnexpectedValueException('Expected [l, c, h(, alpha)] normalized values.');\n}\n$color = Oklch\\Colorspace::colorFromNormalized($normalized);","typeGuard":"function isOklchNormalizedArray(mixed $values): bool\n{\n    return is_array($values) && in_array(count($values), [3, 4], true);\n}","tryCatchPattern":null,"preventionTips":["Build [l, c, h] arrays explicitly; do not array_filter them dynamically","Assert the count after assembling from external data","Alpha is the optional 4th entry and defaults to 1"],"tags":["color","oklch","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"}