{"record":{"id":"3ce6faef2a5dcdaa","repo":"Intervention/image","slug":"normalized-color-value-must-be-in-range-0-to-1-3ce6fa","errorCode":null,"errorMessage":"Normalized color value must be in range 0 to 1","messagePattern":"Normalized color value must be in range 0 to 1","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/Hsv/Colorspace.php","lineNumber":57,"sourceCode":"     * @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,\n        ));\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorspaceInterface::importColor()\n     *\n     * @throws InvalidArgumentException\n     * @throws ColorException\n     */","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Hsv/Colorspace.php#L39-L75","documentation":"In Hsv\\Colorspace::colorFromNormalized() each array entry is passed to a channel factory whose parameter is typed float. A null entry therefore raises a TypeError, which is caught and converted to this InvalidArgumentException reading 'Normalized color value must be in range 0 to 1'. The message is misleading for this code path: the real trigger is a null (or non-float) element, not an out-of-range float.","triggerScenarios":"Hsv\\Colorspace::colorFromNormalized([0.3, null, 0.5]) - arrays sourced from nullable DB columns, optional JSON fields, or array_map over data containing nulls; also string values under strict_types=1.","commonSituations":"Nullable 'alpha' or 'saturation' columns where NULL means 'default'; API payloads with optional channel keys; applying defaults after the conversion call instead of before.","solutions":["Substitute defaults first: $values = array_map(fn ($v) => floatval($v ?? 0.0), $values)","If null means default alpha, pass a 3-element array so alpha defaults to 1.0","Filter nulls out only when you know which channel they belong to (order matters)","Enforce non-null float types in form/API validation before reaching image code"],"exampleFix":"// before\n$color = Hsv\\Colorspace::colorFromNormalized($payload); // [0.3, null, 0.5]\n\n// after\n$payload = array_map(fn ($v) => floatval($v ?? 0.0), $payload);\n$color = Hsv\\Colorspace::colorFromNormalized($payload);","handlingStrategy":"validation","validationCode":"$normalized = array_map(fn ($v) => floatval($v ?? 0.0), $normalized);\nif (in_array(null, $normalized, true)) {\n    throw new InvalidArgumentException('null channel value');\n}","typeGuard":"function hasNoNullChannels(array $values): bool\n{\n    return !in_array(null, $values, true);\n}","tryCatchPattern":null,"preventionTips":["Coerce nullable values to floats before conversion","Note that the 'range' message here actually indicates a null/non-float entry","Drop the alpha element when it is null; it defaults to 1.0"],"tags":["php","intervention-image","color","hsv","null-handling","type-error","normalized-value"],"backgroundTag":"null-argument-type-error","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}