{"record":{"id":"8cafe2089ac736ef","repo":"spectreconsole/spectre.console","slug":"invalid-hex-color-hex","errorCode":null,"errorMessage":"Invalid hex color '#{hex}'.","messagePattern":"Invalid hex color '#(.+?)'\\.","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":"ParseHexColor completed without throwing but the hex string was empty or had a length other than 3 or 6, so no Color could be produced. Reports 'Invalid hex color #{hex}.'","triggerScenarios":"A `#`-prefix with the wrong digit count: `[#1234]` (4 digits), `[#1234567]` (7 digits), `[#]` (empty), or `[#12]` (2 digits).","commonSituations":"Code that builds hex from a value not padded to 6 digits; stripping/trimming that shortens the string; off-by-one in substring slicing.","solutions":["Provide exactly 6 hex digits (#RRGGBB) or 3 (#RGB)","Format integers as X6 to guarantee 6 digits, e.g. $\"#{value:X6}\"","Validate length before embedding"],"exampleFix":"// before\nAnsiMarkup.Parse($\"[#{value:X}]text[/]\"); // X can produce fewer than 6 digits\n\n// after\nAnsiMarkup.Parse($\"[#{value:X6}]text[/]\");","handlingStrategy":"validation","validationCode":"public static bool IsValidHexLength(string token)\n{\n    var h = token.TrimStart('#');\n    return h.Length == 3 || h.Length == 6;\n}","typeGuard":null,"tryCatchPattern":"try { AnsiMarkup.Parse(markup); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Invalid hex color\"))\n{ /* pad to 6 digits with value.ToString(\"X6\") */ }","preventionTips":["Always use 6 digits (#RRGGBB) for programmatic colors","Pad with X6 format specifier to avoid short strings","Validate length before building the tag"],"tags":["markup","color","hex","parsing","spectre-console"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}