{"record":{"id":"1d4b3963b22e7479","repo":"Intervention/image","slug":"number-of-color-channels-must-be-3-or-4-for-class-1d4b39","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/Hsv/Colorspace.php","lineNumber":46,"sourceCode":"     */\n    public static array $channels = [\n        Channels\\Hue::class,\n        Channels\\Saturation::class,\n        Channels\\Value::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): HsvColor\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/Hsv/Colorspace.php#L28-L64","documentation":"Hsv\\Colorspace::colorFromNormalized() constructs an Hsv\\Color from an array of normalized values and requires exactly 3 entries (hue, saturation, value) or 4 entries (plus alpha, defaulted to 1 when omitted). Any other element count throws this InvalidArgumentException before values are examined.","triggerScenarios":"Hsv\\Colorspace::colorFromNormalized([0.3, 0.6]) or an array with 5+ entries; arrays assembled by exploding user strings like '0.3 0.6 0.5 1.0 extra'; passing an associative array that carries additional metadata keys.","commonSituations":"Color-picker or API payloads where an extra field (name, id) rides along in the same array; loop code appending units or labels; off-by-one slicing of channel arrays.","solutions":["Pass exactly [hue, saturation, value] or [hue, saturation, value, alpha], all 0.0-1.0","array_slice($values, 0, 4) or unset() extra keys before the call","Validate count at the input boundary and reject/fix malformed payloads early","Use Hsv\\Color::create($h, $s, $v, $a) when values are already in 0-360 / 0-100 units"],"exampleFix":"// before\n$color = Hsv\\Colorspace::colorFromNormalized([$h, $s, $v, $a, 'label']);\n\n// after\n$color = Hsv\\Colorspace::colorFromNormalized([$h, $s, $v, $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 = Hsv\\Colorspace::colorFromNormalized($normalized);","typeGuard":"function isHsvNormalizedArray(array $values): bool\n{\n    return in_array(count($values), [3, 4], true)\n        && !in_array(null, $values, true);\n}","tryCatchPattern":null,"preventionTips":["Pass a literal [h, s, v] or [h, s, v, a] array","array_slice external arrays to 4 entries maximum","Keep labels/units out of channel arrays"],"tags":["php","intervention-image","color","hsv","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"}