tui-cs/Terminal.Gui · error · ColorParseException

Text did not match any expected format.

Error message

Text did not match any expected format.

What it means

The final fallthrough of Color.Parse: the input did not start with '#', did not match rgb(...)/rgba(...), and was not a recognized named color. Because every prior pattern was exhausted, the parser gives up with this ColorParseException (FormatException). The empty badValueName array indicates no single component is to blame.

Source

Thrown at Terminal.Gui/Drawing/Color/Color.Formatting.cs:342

                        byte.Parse ([b1Char, b2Char], NumberStyles.HexNumber),
                        byte.Parse ([a1Char, a2Char], NumberStyles.HexNumber)
                    ),
                _ => throw new ColorParseException (
                        in hexString,
                        $"Color hex string {hexString} was not in a supported format",
                        in hexString
                    )
            },

            // rgb(r,g,b) or rgb(r,g,b,a)
            ['r', 'g', 'b', '(', .., ')'] => ParseRgbaFormat (in text, 4),

            // rgba(r,g,b,a) or rgba(r,g,b)
            ['r', 'g', 'b', 'a', '(', .., ')'] => ParseRgbaFormat (in text, 5),
            // Attempt named colors
            { } when char.IsLetter (text [0]) && ColorStrings.TryParseNamedColor (text, out Color color) => color,
            // Any other input
            _ => throw new ColorParseException (in text, "Text did not match any expected format.", in text, [])
        };

        [Pure]
        [SkipLocalsInit]
        static Color ParseRgbaFormat (in ReadOnlySpan<char> originalString, in int startIndex)
        {
            ReadOnlySpan<char> valuesSubstring = originalString [startIndex..^1];
            Span<Range> valueRanges = stackalloc Range [4];

            int rangeCount = valuesSubstring.Split (
                                                    valueRanges,
                                                    ',',
                                                    StringSplitOptions.RemoveEmptyEntries
                                                    | StringSplitOptions.TrimEntries
                                                   );

            switch (rangeCount)
            {

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Use Color.TryParse and validate the name against ColorName16 values or ColorStrings.TryParseNamedColor first.
  2. Convert unsupported formats (hsl, uppercase RGB) to a supported form (#RRGGBB or lowercase rgb()) before parsing.
  3. Check char.IsLetter(text[0]) then call ColorStrings.TryParseNamedColor to confirm the name is valid.

Example fix

// before
Color c = Color.Parse(userColorName);

// after
if (!Color.TryParse(userColorName, null, out Color c))
{
    throw new InvalidOperationException($"Unknown color '{userColorName}'.");
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Color.TryParse(userColor, null, out Color c)) return fallbackColor;

Type guard

static bool IsKnownColorName(string s) =>
    s.Length > 0 && char.IsLetter(s[0]) && ColorStrings.TryParseNamedColor(s, out _);

Try / catch

try { c = Color.Parse(s); } catch (ColorParseException) { c = fallback; }

Prevention

When it happens

Trigger: Passing an unrecognized color name (e.g. "banana"), an unsupported function format like "hsl(0,0%,100%)", an uppercase "RGB(1,2,3)" (the pattern is case-sensitive lowercase), or a stray symbol like "@red".

Common situations: Misspelled named colors, CSS hsl/hwb syntax that Terminal.Gui does not support, uppercase function names from user input, or locale-specific color names.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/f7f4bc01dbe9a004. Report an issue: GitHub.