{"record":{"id":"f164dbda1d1c17c9","repo":"spectreconsole/spectre.console","slug":"color-number-must-be-less-than-or-equal-to-255-wa","errorCode":null,"errorMessage":"Color number must be less than or equal to 255 (was {number})","messagePattern":"Color number must be less than or equal to 255 \\(was (.+?)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console.Ansi/AnsiMarkupTagParser.cs","lineNumber":10,"sourceCode":"namespace Spectre.Console;\n\ninternal static class AnsiMarkupTagParser\n{\n    public static (Style Style, Link? Link) Parse(string text)\n    {\n        var result = Parse(text, out var error);\n        if (error != null)\n        {\n            throw new InvalidOperationException(error);\n        }\n\n        return result ?? throw new InvalidOperationException(\"Could not parse style.\");\n    }\n\n    public static bool TryParse(string text, [NotNullWhen(true)] out (Style Style, Link?)? result)\n    {\n        result = Parse(text, out _);\n        return result != null;\n    }\n\n    private static (Style Style, Link? Link)? Parse(string text, out string? error)\n    {\n        var effectiveDecoration = (Decoration?)null;\n        var effectiveForeground = (Color?)null;\n        var effectiveBackground = (Color?)null;\n        var effectiveLink = (string?)null;\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console.Ansi/AnsiMarkupTagParser.cs#L1-L28","documentation":"A tag part parses as an integer but exceeds 255, the maximum 256-color palette index. The value is reported in the message.","triggerScenarios":"Markup like `[256]`, `[1000]`, or code that interpolates an out-of-range integer into a tag, e.g. `$\"[{i}]\"` where i > 255.","commonSituations":"Looping over an unbounded range and using the counter as a color index; misreading the palette as 0-999; feeding an RGB component as a palette index.","solutions":["Clamp indices to 0-255 with Math.Clamp","Use `#rrggbb` or `rgb(r,g,b)` syntax for arbitrary true-color values instead of a palette index","Validate the range before constructing the tag"],"exampleFix":"// before\nAnsiMarkup.Parse($\"[{value}]x[/]\"); // value can be 300\n\n// after\nif (value is >= 0 and <= 255)\n    AnsiMarkup.Parse($\"[{value}]x[/]\");\nelse\n    AnsiMarkup.Parse($\"[#{value:X6}]x[/]\"); // or use true-color hex","handlingStrategy":"validation","validationCode":"int safeIndex = Math.Clamp(rawIndex, 0, 255);\nvar markup = rawIndex is >= 0 and <= 255 ? $\"[{rawIndex}]text[/]\" : $\"[#{rawIndex:X6}]text[/]\";","typeGuard":null,"tryCatchPattern":"try { AnsiMarkup.Parse(markup); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Color number must be less than or equal to 255\"))\n{ /* switch to true-color hex form instead of palette index */ }","preventionTips":["Treat palette indices as strictly 0-255","Use #rrggbb for values outside the palette range","Clamp or branch on numeric inputs before formatting markup"],"tags":["markup","color","parsing","spectre-console"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}