{"record":{"id":"d26548addbb0f2de","repo":"Intervention/image","slug":"number-of-color-channels-must-be-3-or-4-for-class","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/Hsl/Colorspace.php","lineNumber":46,"sourceCode":"     */\n    public static array $channels = [\n        Channels\\Hue::class,\n        Channels\\Saturation::class,\n        Channels\\Luminance::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): HslColor\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":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Hsl/Colorspace.php#L28-L64","documentation":"Hsl\\Colorspace::colorFromNormalized() builds an Hsl\\Color from an array of normalized (0.0-1.0) channel values and requires exactly 3 entries (hue, saturation, luminance) or 4 entries (plus alpha; alpha defaults to 1 when omitted). Any other count throws this InvalidArgumentException before any value is inspected.","triggerScenarios":"Hsl\\Colorspace::colorFromNormalized([0.5, 0.5]) (2 values); colorFromNormalized([0.1, 0.2, 0.3, 0.4, 0.5]) (5 values); arrays built by exploding a user string like 'hsl 0.1 0.2 0.3 0.4 0.5' on whitespace; forgetting that alpha belongs in the same array rather than a separate argument.","commonSituations":"Mapping generic 3/4-channel color data (e.g. from a color picker widget or API payload) that occasionally carries extra keys; appending a units suffix as an extra element; associative arrays with unexpected extra entries passed straight through.","solutions":["Pass exactly [hue, saturation, luminance] or [hue, saturation, luminance, alpha] with values in 0.0-1.0","Slice/array_slice or unset extra keys before the call","Validate the count at the boundary: if (!in_array(count($data), [3, 4])) reject the payload early","Build via Hsl\\Color::create($h, $s, $l) when your values are already in 0-360 / 0-100 units"],"exampleFix":"// before\n$color = Hsl\\Colorspace::colorFromNormalized([$h, $s, $l, $a, $extra]);\n\n// after\n$color = Hsl\\Colorspace::colorFromNormalized([$h, $s, $l, $a]);","handlingStrategy":"validation","validationCode":"if (!in_array(count($normalized), [3, 4], true)) {\n    throw new InvalidArgumentException('Expected 3 or 4 normalized channels, got ' . count($normalized));\n}\n$color = Hsl\\Colorspace::colorFromNormalized($normalized);","typeGuard":"function isHslNormalizedArray(array $values): bool\n{\n    return in_array(count($values), [3, 4], true)\n        && array_reduce($values, fn ($ok, $v) => $ok && is_float($v), true);\n}","tryCatchPattern":null,"preventionTips":["Build channel arrays with an explicit literal [h, s, l] or [h, s, l, a]","Slice external payloads to at most 4 entries before passing","Keep metadata out of value arrays; carry it in separate keys"],"tags":["php","intervention-image","color","hsl","array-validation","normalized-value"],"backgroundTag":"invalid-array-length","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}