{"record":{"id":"57492bc2da2dea26","repo":"iOfficeAI/OfficeCLI","slug":"invalid-color-value-original-rgb-components","errorCode":null,"errorMessage":"Invalid color value: '{original}'. RGB components must be 0-255.","messagePattern":"Invalid color value: '(.+?)'\\. RGB components must be 0-255\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/ParseHelpers.cs","lineNumber":168,"sourceCode":"                : (byte?)null;\n            return ($\"{r:X2}{g:X2}{b:X2}\", a);\n        }\n\n        return null;\n    }\n\n    private static byte ParseRgbComponent(string token, string original)\n    {\n        token = token.Trim();\n        if (token.EndsWith('%'))\n        {\n            if (!double.TryParse(token[..^1], NumberStyles.Float, CultureInfo.InvariantCulture, out var pct)\n                || pct < 0 || pct > 100)\n                throw new ArgumentException($\"Invalid color value: '{original}'. RGB percentage components must be 0%-100%.\");\n            return (byte)Math.Round(pct / 100.0 * 255.0);\n        }\n        if (!int.TryParse(token, NumberStyles.Integer, CultureInfo.InvariantCulture, out var n) || n < 0 || n > 255)\n            throw new ArgumentException($\"Invalid color value: '{original}'. RGB components must be 0-255.\");\n        return (byte)n;\n    }\n\n    private static byte ParseAlphaComponent(string token, string original)\n    {\n        token = token.Trim();\n        double a;\n        if (token.EndsWith('%'))\n        {\n            if (!double.TryParse(token[..^1], NumberStyles.Float, CultureInfo.InvariantCulture, out var pct)\n                || pct < 0 || pct > 100)\n                throw new ArgumentException($\"Invalid color value: '{original}'. Alpha percentage must be 0%-100%.\");\n            a = pct / 100.0;\n        }\n        else\n        {\n            if (!double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, out a) || a < 0 || a > 1)\n                throw new ArgumentException($\"Invalid color value: '{original}'. Alpha must be 0-1 (e.g. 0.5) or 0%-100%.\");","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/ParseHelpers.cs#L150-L186","documentation":"Thrown by ParseRgbComponent when an RGB component is an integer-style token (no %) that fails to parse or is outside 0-255. This is the 0-255 branch; percentage components are handled separately. Each channel must be a whole number in the inclusive 0-255 range.","triggerScenarios":"Passing rgb(300,0,0), rgb(-10,0,0), or rgb(x,0,0). Also triggered by fractional 0-255 values like rgb(128.5,0,0) since this branch uses int.TryParse.","commonSituations":"Summing color channels and overflowing 255; using a color from a library that emits floats; negative values from a subtractive blend.","solutions":["Clamp each channel to [0,255] and keep it integer.","If you have a fractional value, use percentage form (0%-100%) instead.","Round float channels before formatting: (byte)Math.Round(f)."],"exampleFix":"// before\nvar c = $\"rgb({r},{g},{b}\")); // r == 300\nParseColor(c); // throws 285\n\n// after\nbyte Clamp(double v) => (byte)Math.Round(Math.Clamp(v,0,255));\nvar c = $\"rgb({Clamp(r)},{Clamp(g)},{Clamp(b)})\");\nParseColor(c);","handlingStrategy":"validation","validationCode":"static byte ClampByte(double v) => (byte)Math.Clamp(Math.Round(v),0,255);\n// build: $\"rgb({ClampByte(r)},{ClampByte(g)},{ClampByte(b)})\"","typeGuard":"static bool IsValidRgbByte(string token)\n    => int.TryParse(token, CultureInfo.InvariantCulture, out var n) && n >= 0 && n <= 255;","tryCatchPattern":"try { color = ParseColor(spec); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"0-255\"))\n{ color = Color.Black; }","preventionTips":["Clamp and round channel values to a byte before formatting.","Use percentage form for fractional channels.","Audit blend code for unbounded sums."],"tags":["color","validation","argument","rgb","range"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}