{"record":{"id":"fe7dc9d11baa363d","repo":"Intervention/image","slug":"normalized-color-channel-value-must-be-between-0-t-fe7dc9","errorCode":null,"errorMessage":"Normalized color channel value must be between 0 to 1","messagePattern":"Normalized color channel value must be between 0 to 1","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/IntegerColorChannel.php","lineNumber":29,"sourceCode":"    /**\n     * @throws InvalidArgumentException\n     */\n    final public function __construct(int $value)\n    {\n        $this->value = (int) $this->validValueOrFail($value);\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorChannelInterface::fromNormalized()\n     *\n     * @throws InvalidArgumentException\n     */\n    public static function fromNormalized(float $normalized): self\n    {\n        if ($normalized < 0 || $normalized > 1) {\n            throw new InvalidArgumentException(\n                'Normalized color channel value must be between 0 to 1',\n            );\n        }\n\n        return new static(intval(round($normalized * static::max())));\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorChannelInterface::value()\n     */\n    public function value(): int\n    {\n        return (int) $this->value;\n    }\n}\n","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/IntegerColorChannel.php#L11-L47","documentation":"IntegerColorChannel::fromNormalized() only accepts a float between 0 and 1 inclusive, because a normalized value is a fraction of the channel's full range, not the channel value itself (src/Colors/IntegerColorChannel.php:26-35). This method is the base for all integer channels (RGB red/green/blue 0-255, CMYK 0-100, HSV hue 0-360 / saturation / value 0-100, HSL), so passing anything outside [0, 1.0] throws InvalidArgumentException.","triggerScenarios":"Calling Red::fromNormalized(128) instead of Red::fromNormalized(128 / 255), Hsv\\Channels\\Saturation::fromNormalized(50) instead of 0.5, or passing a negative float; also hit indirectly when a colorspace's colorFromNormalized() receives out-of-range normalized values.","commonSituations":"Treating 'normalized' as a percentage (0-100) or as the raw channel value; computing ratios that can go slightly negative or above 1 due to float arithmetic or user math without clamping.","solutions":["Divide by the channel maximum (e.g. 128 / 255) or use channel->normalized() to produce the fraction","Clamp before calling: max(0.0, min(1.0, $value))","Construct the channel with its real value instead: new Red(128), which validates against 0-255","When building whole colors from normalized arrays, prefer Colorspace::colorFromNormalized() so alpha defaults are handled"],"exampleFix":"// before\n$channel = Rgb\\Channels\\Red::fromNormalized(128); // throws: must be between 0 to 1\n\n// after\n$channel = Rgb\\Channels\\Red::fromNormalized(128 / 255);\n// or with the raw integer value:\n$channel = new Rgb\\Channels\\Red(128);","handlingStrategy":"validation","validationCode":"function clamp01(float $v): float { return max(0.0, min(1.0, $v)); }\n\n$channel = Rgb\\Channels\\Red::fromNormalized(clamp01($pixel / 255));","typeGuard":"function isNormalizedValue(mixed $value): bool\n{\n    return is_float($value) && $value >= 0.0 && $value <= 1.0;\n}","tryCatchPattern":null,"preventionTips":["Remember 'normalized' means 0..1, never 0..100 or the raw channel value","Always divide by the channel max (255, 100, 360) before calling fromNormalized","Clamp computed ratios before passing them to color APIs"],"tags":["color","channel","normalized-value","value-out-of-range","php"],"backgroundTag":"value-out-of-range","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}