{"record":{"id":"99e44941bca78e68","repo":"Intervention/image","slug":"normalized-color-value-must-be-in-range-0-to-1-99e449","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/Rgb/Colorspace.php","lineNumber":60,"sourceCode":"     * @see ColorspaceInterface::colorFromNormalized()\n     *\n     * @throws InvalidArgumentException\n     */\n    public static function colorFromNormalized(array $normalized): RgbColor\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 ColorException\n     */\n    public function importColor(ColorInterface $color): RgbColor","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Rgb/Colorspace.php#L42-L78","documentation":"Inside colorFromNormalized(), each array value is passed to the channel's static fromNormalized(float $normalized). That parameter is a non-nullable float, so a null entry (or a wrong type under strict_types) raises a PHP TypeError, which the library catches and rethrows as this InvalidArgumentException. Note the distinction: an out-of-range float such as 1.5 throws 'Normalized color channel value must be between 0 to 1' from the channel itself; this particular message means a value was null or not a float.","triggerScenarios":"colorFromNormalized([0.5, 0.2, 0.8, null]) with a null alpha; arrays where array_map/array_combine left holes; values coming from json_decode() or nullable database columns passed unfiltered.","commonSituations":"Nullable columns (e.g. alpha or optional channel) flowing into color building; array_pad() to a longer length than data provided; strict_types making numeric strings like '0.5' unacceptable as float.","solutions":["Substitute defaults for null entries before calling: alpha defaults to 1.0","Normalize the array first: array_map(fn($v) => (float) ($v ?? 1.0), $values)","Validate that every entry is a float between 0 and 1 before the call"],"exampleFix":"// before\n$color = Rgb\\Colorspace::colorFromNormalized([$r, $g, $b, $alpha ?? null]); // TypeError wrapped\n\n// after\n$color = Rgb\\Colorspace::colorFromNormalized([(float) $r, (float) $g, (float) $b, (float) ($alpha ?? 1.0)]);","handlingStrategy":"validation","validationCode":"$values = array_map(\n    fn($v) => (float) ($v ?? 1.0), // substitute defaults for nulls\n    $values\n);\nif (in_array(null, $values, true)) { /* still null somewhere */ }\n$color = Rgb\\Colorspace::colorFromNormalized($values);","typeGuard":"function hasNoNullChannelValues(array $values): bool\n{\n    return !in_array(null, $values, true)\n        && array_all($values, fn($v) => is_float($v) || is_int($v));\n}","tryCatchPattern":"try {\n    $color = Rgb\\Colorspace::colorFromNormalized($values);\n} catch (\\Intervention\\Image\\Exceptions\\InvalidArgumentException $e) {\n    // a null/non-float entry slipped in; coerce and retry\n    $safe = array_map(fn($v) => (float) ($v ?? 1.0), array_slice($values, 0, 4));\n    $color = Rgb\\Colorspace::colorFromNormalized($safe);\n}","preventionTips":["Coerce nullable values to defaults ((float) ($v ?? 1.0)) before building colors","Remember the out-of-range message ('must be between 0 to 1') differs from this null/type message","Cast values from json/database sources to float under strict_types"],"tags":["rgb","colorspace","normalized-values","type-error","php"],"backgroundTag":"invalid-normalized-color-value","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}