tui-cs/Terminal.Gui · error · ColorParseException

Text was too short to be any possible supported format.

Error message

Text was too short to be any possible supported format.

What it means

Thrown by Color.Parse(ReadOnlySpan<char>) when the input length is between 1 and 2 inclusive. The shortest valid color representation is at least 3 characters long (e.g. a named color), so anything shorter cannot possibly match any supported format. It is a ColorParseException (FormatException). The string overload has its own equivalent guard for text shorter than 3 characters.

Source

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

    [SkipLocalsInit]
    public static Color Parse (ReadOnlySpan<char> text, IFormatProvider? formatProvider = null)
    {
        return text switch
        {
            // Null string or empty span provided
            { IsEmpty: true } when formatProvider is null =>
                throw new ColorParseException (in text, "The text provided was null or empty.", in text),

            // A valid ICustomColorFormatter was specified and the text wasn't null or empty
            { IsEmpty: false } when formatProvider is ICustomColorFormatter f => f.Parse (text),

            // Input string is only whitespace
            { Length: > 0 } when text.IsWhiteSpace () =>
                throw new ColorParseException (in text, "The text provided consisted of only whitespace characters.", in text),

            // Any string too short to possibly be any supported format.
            { Length: > 0 and < 3 } =>
                throw new ColorParseException (in text, "Text was too short to be any possible supported format.", in text),

            // The various hexadecimal cases
            ['#', ..] hexString => hexString switch
            {
                // #RGB
                ['#', var rChar, var gChar, var bChar] chars when chars [1..]
                    .IsAllAsciiHexDigits () =>
                        new Color (
                            byte.Parse ([rChar, rChar], NumberStyles.HexNumber),
                            byte.Parse ([gChar, gChar], NumberStyles.HexNumber),
                            byte.Parse ([bChar, bChar], NumberStyles.HexNumber)
                        ),

                // #ARGB
                ['#', var aChar, var rChar, var gChar, var bChar] chars when chars [1..]
                    .IsAllAsciiHexDigits () =>
                        new Color (
                            byte.Parse ([rChar, rChar], NumberStyles.HexNumber),

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Check input length >= 3 before calling Parse, or use TryParse.
  2. Use Color.TryParse(...) to avoid the throw and apply a fallback color.
  3. Provide an ICustomColorFormatter via the formatProvider argument if you need a custom short format.

Example fix

// before
Color c = Color.Parse(token.AsSpan());

// after
Color c = token.Length >= 3 && Color.TryParse(token.AsSpan(), null, out Color parsed)
    ? parsed
    : default;
Defensive patterns

Strategy: validation

Validate before calling

if (text.AsSpan().Length < 3) return fallbackColor;

Type guard

static bool IsParsableColorLength(ReadOnlySpan<char> s) => s.Length >= 3;

Try / catch

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

Prevention

When it happens

Trigger: Calling Color.Parse("a") or Color.Parse("#1") on a span of length 1 or 2 when no ICustomColorFormatter is supplied.

Common situations: Truncated color strings from config, a '#' prefix typed alone, or partial/incomplete user input passed straight to the parser.

Related errors


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