{"record":{"id":"636603f5e4106f1a","repo":"Intervention/image","slug":"failed-to-decode-rgb-color-string","errorCode":null,"errorMessage":"Failed to decode RGB color string","messagePattern":"Failed to decode RGB color string","errorType":"exception","errorClass":"ColorDecoderException","httpStatus":null,"severity":"error","filePath":"src/Colors/Rgb/Decoders/StringColorDecoder.php","lineNumber":75,"sourceCode":"\n        // rgb values\n        $values = array_map(fn(string $value): int => match (strpos($value, '%')) {\n            false => intval(trim($value)),\n            default => intval(round(floatval(trim(str_replace('%', '', $value))) / 100 * 255)),\n        }, [$matches['r'], $matches['g'], $matches['b']]);\n\n        // alpha value\n        if (array_key_exists('a', $matches)) {\n            $values[] = match (strpos($matches['a'], '%')) {\n                false => floatval(trim($matches['a'])),\n                default => floatval(trim(str_replace('%', '', $matches['a']))) / 100,\n            };\n        }\n\n        try {\n            return new Color(...$values);\n        } catch (InvalidArgumentException $e) {\n            throw new ColorDecoderException('Failed to decode RGB color string', previous: $e);\n        }\n    }\n}\n","sourceCodeStart":57,"sourceCodeEnd":79,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Rgb/Decoders/StringColorDecoder.php#L57-L79","documentation":"The rgb()/rgba() string matched the CSS syntax regex in StringColorDecoder, but constructing the Color object from the extracted numbers failed. This means the syntax was recognized yet a value is out of range: an RGB channel outside 0-255 or an alpha outside 0.0-1.0. The original InvalidArgumentException from the Color constructor is chained as the previous exception and names the offending value.","triggerScenarios":"Passing strings like 'rgb(300, 0, 0)' (channel above 255), 'rgba(255, 0, 0, 1.5)' (alpha above 1), or percentage values that resolve above 255 such as 'rgb(120%, 0%, 0%)' to any color-accepting API: fill(), text color, background color, or direct driver color decoding.","commonSituations":"Colors built dynamically from user input or a database without clamping; CSS values copied from design tools that permit out-of-range notation; percentage math that rounds above 255.","solutions":["Clamp channels to 0-255 and alpha to 0.0-1.0 before formatting the color string","Inspect ->getPrevious() of the ColorDecoderException to see exactly which value the Color constructor rejected","If you only need a guaranteed-parseable color, pass a hex string like '#ff0000' instead"],"exampleFix":"// before\n$color = $manager->color('rgb(300, 12, 12)');\n\n// after\n$color = $manager->color(sprintf('rgb(%d, %d, %d)',\n    min(255, max(0, $r)),\n    min(255, max(0, $g)),\n    min(255, max(0, $b))\n));","handlingStrategy":"validation","validationCode":"function safeRgbString(int $r, int $g, int $b, float $a = 1.0): string\n{\n    return sprintf(\n        'rgba(%d, %d, %d, %s)',\n        min(255, max(0, $r)),\n        min(255, max(0, $g)),\n        min(255, max(0, $b)),\n        min(1.0, max(0.0, $a))\n    );\n}","typeGuard":null,"tryCatchPattern":"try {\n    $color = $driver->decodeColor($rgbString);\n} catch (ColorDecoderException $e) {\n    // $e->getPrevious() carries the Color constructor error naming the bad value\n    $color = $driver->decodeColor('#ffffff');\n}","preventionTips":["Clamp dynamic channel values to 0-255 and alpha to 0.0-1.0 before formatting rgb()/rgba() strings","Prefer hex strings for programmatically generated colors","Log ->getPrevious() when this fires - it names the exact offending value"],"tags":["color","rgb","css-syntax","validation"],"backgroundTag":"invalid-color-value","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}