{"record":{"id":"c851d81e6d66e9d8","repo":"Intervention/image","slug":"number-of-color-channels-must-be-4-or-5-for-class","errorCode":null,"errorMessage":"Number of color channels must be 4 or 5 for {class}","messagePattern":"Number of color channels must be 4 or 5 for (.+?)","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/Cmyk/Colorspace.php","lineNumber":46,"sourceCode":"    public static array $channels = [\n        Channels\\Cyan::class,\n        Channels\\Magenta::class,\n        Channels\\Yellow::class,\n        Channels\\Key::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): CmykColor\n    {\n        if (!in_array(count($normalized), [4, 5])) {\n            throw new InvalidArgumentException('Number of color channels must be 4 or 5 for ' . static::class);\n        }\n\n        // add alpha value if missing\n        $normalized = count($normalized) === 4 ? array_pad($normalized, 5, 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/Cmyk/Colorspace.php#L28-L64","documentation":"ReduceColorsModifier (quantization / palette reduction) validates that the color limit is at least 1 — a palette with zero colors is meaningless. The constructor throws InvalidArgumentException for any limit < 1.","triggerScenarios":"$image->reduceColors(0); passing a user-supplied 'numberOfColors' of 0; deriving the limit from a subtraction or from config that defaults to 0.","commonSituations":"Form fields with a 0 default meaning 'unlimited'/'disabled' being forwarded verbatim; dividing user input down (e.g. intdiv($n, 100) with small $n) reaching 0; API consumers assuming 0 disables quantization.","solutions":["Treat 0 or less as 'do not quantize' in your wrapper: if ($limit >= 1) { $image->reduceColors($limit); }.","Clamp: max(1, (int) $limit) when reduction must always happen.","Document in your API that the valid range is 1..(driver maximum) and validate at the boundary."],"exampleFix":"// before\n$image->reduceColors($colors); // 0 throws\n\n// after\nif ($colors >= 1) {\n    $image->reduceColors($colors);\n}","handlingStrategy":"validation","validationCode":"if ($limit >= 1) {\n    $image->reduceColors($limit);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide what 0 means in your API ('no quantization') and branch on it.","Clamp derived limits: max(1, $limit).","Validate numberOfColors form fields as min:1."],"tags":["reduce-colors","quantization","argument-validation","php"],"backgroundTag":"argument-out-of-range","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}