{"record":{"id":"1b05140523b396ff","repo":"Intervention/image","slug":"invalid-rgb-color-syntax-input","errorCode":null,"errorMessage":"Invalid rgb() color syntax \"{input}\"","messagePattern":"Invalid rgb\\(\\) color syntax \"(.+?)\"","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Colors/Rgb/Decoders/StringColorDecoder.php","lineNumber":55,"sourceCode":"        }\n\n        if (preg_match('/^s?rgb/i', $input) !== 1) {\n            return false;\n        }\n\n        return true;\n    }\n\n    /**\n     * Decode rgb color strings.\n     *\n     * @throws InvalidArgumentException\n     * @throws ColorDecoderException\n     */\n    public function decode(mixed $input): ColorInterface\n    {\n        if (preg_match(self::PATTERN, $input, $matches) !== 1) {\n            throw new InvalidArgumentException('Invalid rgb() color syntax \"' . $input . '\"');\n        }\n\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);","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Colors/Rgb/Decoders/StringColorDecoder.php#L37-L73","documentation":"StringColorDecoder::decode() validates the input against a strict rgb()/rgba() pattern: values must be 1-3 digit integers (or percentages), separated consistently by commas or spaces, with an optional alpha as 0-1 float or percentage. Its supports() accepts any string starting with 'rgb', so malformed rgb(...) strings reach decode() and fail the full pattern, throwing this InvalidArgumentException with the offending input echoed back.","triggerScenarios":"Rgb\\Color::parse('rgb(255;0;0)'), parse('rgb(255, 0)'), parse('rgb(3000 0 0)'), parse('rgba(255 0 0 / 2)'), or mixing separators ('rgb(255, 0 0)'); also decimals in r/g/b like 'rgb(25.5 0 0)'.","commonSituations":"Passing CSS strings authored for browsers with syntax the pattern rejects (decimal channels, slash separators with unusual spacing, mixed comma/space separators); user input from color pickers that emit modern CSS color syntax; missing one of the three channels.","solutions":["Use the accepted syntax: 'rgb(255 0 0)', 'rgb(255, 0, 0)', 'rgba(255 0 0 / 0.5)' or 'rgb(100% 0% 0%)'","Normalize user input before parsing (e.g. strip extra whitespace, convert decimals to integers)","Catch InvalidArgumentException and show a validation message when accepting free-form color strings"],"exampleFix":"// before\n$color = \\Intervention\\Image\\Colors\\Rgb\\Color::parse('rgb(25.5, 0, 0)');\n\n// after\n$color = \\Intervention\\Image\\Colors\\Rgb\\Color::parse('rgb(26, 0, 0)'); // integer channels only","handlingStrategy":"validation","validationCode":"$pattern = '/^s?rgba?\\s*\\(\\s*\\d{1,3}(?:%|\\s|,)' // quick pre-check\n    . '/i';\nif (!preg_match('/^s?rgba? ?\\(/i', $input)) {\n    throw new \\RuntimeException('Not an rgb() string');\n}\n// full parity check with the library pattern is strictest; simplest: try parse and catch","typeGuard":"function isRgbString(mixed $input): bool\n{\n    return is_string($input) && preg_match(\n        '/^s?rgba? ?\\( ?\\d{1,3}[, ] ?\\d{1,3}[, ] ?\\d{1,3}'\n        . '(?: ?\\/ ?|[, ] ?(?:0\\.\\d+|1\\.0|\\.\\d+|\\d{1,3}%|1|0))? ?\\)$/i',\n        $input\n    ) === 1;\n}","tryCatchPattern":"try {\n    $color = \\Intervention\\Image\\Colors\\Rgb\\Color::parse('rgb(' . implode(' ', [$r, $g, $b]) . ')');\n} catch (\\Intervention\\Image\\Exceptions\\InvalidArgumentException $e) {\n    $color = \\Intervention\\Image\\Colors\\Rgb\\Color::create($r, $g, $b); // build directly from ints\n}","preventionTips":["Emit canonical syntax: 'rgb(255 0 0)' or 'rgba(255 0 0 / 0.5)' with consistent separators","Channels must be 1-3 digit integers or percentages; no decimals in r/g/b","When you already have numeric channels, skip parsing and call Color::create($r, $g, $b)"],"tags":["rgb","color-parsing","validation","php"],"backgroundTag":"color-parse-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}