tui-cs/Terminal.Gui · error · ColorParseException

Wrong number of values. Expected 3 or 4 decimal integers. Go

Error message

Wrong number of values. Expected 3 or 4 decimal integers. Got {rangeCount}.

What it means

Thrown by ParseRgbaFormat when the rgb()/rgba() string, after splitting on commas, does not yield exactly 3 or 4 value ranges. The message reports the actual count found. Only rgb(r,g,b) and rgb(r,g,b,a) (3 or 4 components) are valid; anything else hits the default switch arm.

Source

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

                                        out ReadOnlySpan<char> gSpan,
                                        out ReadOnlySpan<char> bSpan
                                       );
                        ReadOnlySpan<char> aSpan = valuesSubstring [valueRanges [3]];

                        if (!aSpan.IsAllAsciiDigits ())
                        {
                            throw new ColorParseException (
                                                           in originalString,
                                                           "Value was not composed entirely of decimal digits.",
                                                           in aSpan,
                                                           nameof (A)
                                                          );
                        }

                        return new Color (int.Parse (rSpan), int.Parse (gSpan), int.Parse (bSpan), int.Parse (aSpan));
                    }
                default:
                    throw new ColorParseException (
                                                   in originalString,
                                                   $"Wrong number of values. Expected 3 or 4 decimal integers. Got {rangeCount}.",
                                                   in originalString
                                                  );
            }

            [Pure]
            [SkipLocalsInit]
            static void ParseRgbValues (
                in ReadOnlySpan<char> valuesString,
                in Span<Range> valueComponentRanges,
                in ReadOnlySpan<char> originalString,
                out ReadOnlySpan<char> rSpan,
                out ReadOnlySpan<char> gSpan,
                out ReadOnlySpan<char> bSpan
            )
            {
                rSpan = valuesString [valueComponentRanges [0]];

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Construct rgb strings with exactly 3 (or 4 for alpha) integer components.
  2. Split and validate the component count is 3 or 4 before calling Parse.
  3. Use Color.TryParse to degrade gracefully on malformed input.

Example fix

// before
Color c = Color.Parse($"rgb({string.Join(',', values)})");

// after
if (values.Length is 3 or 4)
    Color c = Color.Parse($"rgb({string.Join(',', values)})");
Defensive patterns

Strategy: validation

Validate before calling

int n = values.Length;
if (n is not 3 and not 4) return fallbackColor;

Type guard

static bool HasValidRgbArity(string[] parts) => parts.Length is 3 or 4;

Try / catch

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

Prevention

When it happens

Trigger: Passing "rgb(1,2)" (2 values), "rgb(1,2,3,4,5)" (5 values), "rgb(1,,2,3)" (empty segment shifts count), or a trailing comma "rgb(1,2,3,)".

Common situations: Dynamically building rgb strings with the wrong number of components, stray/missing commas from string interpolation, or copying malformed CSS.

Related errors


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