{"record":{"id":"5c1a0e5a1af0b942","repo":"spectreconsole/spectre.console","slug":"invalid-hex-color-hex-ex-message","errorCode":null,"errorMessage":"Invalid hex color '#{hex}'. {ex.Message}","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 caught an exception while converting the hex digits (e.g. non-hex characters causing Convert.ToUInt32 to throw). The original exception message is appended, e.g. for `[#ZZZZZZ]`. Note the re-thrown message says '#{hex}' but the '#' was stripped internally, so the printed token lacks its leading '#'.","triggerScenarios":"A `#`-prefixed part containing non-hexadecimal characters, e.g. `[#GGGGGG]`, `[#12GH56]`, or a hex value with an embedded space.","commonSituations":"Programmatic color generation that produces invalid hex digits; user-typed color with a typo; locale/encoding issues inserting odd characters.","solutions":["Use only characters 0-9 and A-F/a-f in the hex portion","Validate with a regex like ^#[0-9A-Fa-f]{6}$ before building the tag","Fall back to a named color or rgb() form"],"exampleFix":"// before\nAnsiMarkup.Parse(\"[#GGGGGG]text[/]\");\n\n// after\nAnsiMarkup.Parse(\"[#00FFAA]text[/]\");","handlingStrategy":"validation","validationCode":"public static bool IsValidHex(string token) =>\n    Regex.IsMatch(token, @\"^#[0-9A-Fa-f]{6}$\") || Regex.IsMatch(token, @\"^#[0-9A-Fa-f]{3}$\");","typeGuard":null,"tryCatchPattern":"try { AnsiMarkup.Parse(markup); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Invalid hex color\"))\n{ /* replace with a known-good hex value */ }","preventionTips":["Restrict hex to [0-9A-Fa-f] only","Validate with a regex before embedding","Generate hex from integers via value.ToString(\"X6\")"],"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"}