{"record":{"id":"5ea7224eeadb5bb8","repo":"spectreconsole/spectre.console","slug":"color-number-must-be-greater-than-or-equal-to-0-w","errorCode":null,"errorMessage":"Color number must be greater than or equal to 0 (was {number})","messagePattern":"Color number must be greater than or equal to 0 \\(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":"In AnsiMarkupTagParser.Parse, a tag part parses as an integer (so it is treated as a 256-palette color index) but the value is negative. Color indices must be 0-255; the offending number is interpolated into the message.","triggerScenarios":"A tag like `[on -1]`, `[red -5]`, or any markup tag containing a bare negative integer in a color position.","commonSituations":"Off-by-one or sign error in code generating a color index; arithmetic that produces a negative value fed into a markup tag.","solutions":["Clamp the color index to the 0-255 range before embedding it in markup","Use a named color or `#rrggbb`/`rgb(...)` form instead of a raw index","Validate generated indices against 0-255"],"exampleFix":"// before\nAnsiMarkup.Parse($\"[{computedIndex}]text[/]\"); // computedIndex may be -1\n\n// after\nvar safe = Math.Clamp(computedIndex, 0, 255);\nAnsiMarkup.Parse($\"[{safe}]text[/]\");","handlingStrategy":"validation","validationCode":"// Clamp any numeric color index into range before building markup:\nint safeIndex = Math.Clamp(rawIndex, 0, 255);\nvar markup = $\"[{safeIndex}]text[/]\";","typeGuard":null,"tryCatchPattern":"try { AnsiMarkup.Parse(markup); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Color number must be greater than or equal to 0\"))\n{ /* re-clamp and retry with index 0 */ }","preventionTips":["Validate numeric color indices against 0-255 at the source","Prefer named colors or true-color hex when the value is computed","Use Math.Clamp as a cheap safety net"],"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"}