tui-cs/Terminal.Gui · error · ArgumentException

Number of colors must match the number of bars

Error message

Number of colors must match the number of bars

What it means

The MultiBarSeries constructor pre-allocates one BarSeries per bar in a category (numberOfBarsPerCategory). If a colors array is supplied, its length must exactly match so each bar gets a color; a mismatch means some bars would be uncolored or the array would overflow.

Source

Thrown at Terminal.Gui/Views/GraphView/MultiBarSeries.cs:27

    /// <summary>Creates a new series of clustered bars.</summary>
    /// <param name="numberOfBarsPerCategory">Each category has this many bars</param>
    /// <param name="barsEvery">How far apart to put each category (in graph space)</param>
    /// <param name="spacing">
    ///     How much spacing between bars in a category (should be less than <paramref name="barsEvery"/>/
    ///     <paramref name="numberOfBarsPerCategory"/>)
    /// </param>
    /// <param name="colors">
    ///     Array of colors that define bar color in each category. Length must match
    ///     <paramref name="numberOfBarsPerCategory"/>
    /// </param>
    public MultiBarSeries (int numberOfBarsPerCategory, float barsEvery, float spacing, Attribute []? colors = null)
    {
        _subSeries = new BarSeries [numberOfBarsPerCategory];

        if (colors is { } && colors.Length != numberOfBarsPerCategory)
        {
            throw new ArgumentException (@"Number of colors must match the number of bars", nameof (numberOfBarsPerCategory));
        }

        for (var i = 0; i < numberOfBarsPerCategory; i++)
        {
            _subSeries [i] = new ()
            {
                BarEvery = barsEvery,
                Offset = i * spacing,

                // Only draw labels for the first bar in each category
                DrawLabels = i == 0
            };

            if (colors is { })
            {
                _subSeries [i].OverrideBarColor = colors [i];
            }
        }

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Ensure colors.Length == numberOfBarsPerCategory, or pass null for default colors.
  2. Generate the colors array dynamically from numberOfBarsPerCategory.
  3. If colors are optional, omit the parameter entirely.

Example fix

// before
new MultiBarSeries(3, 1f, 0.5f, new Attribute[] { red, green });
// after
new MultiBarSeries(3, 1f, 0.5f, new Attribute[] { red, green, blue });
Defensive patterns

Strategy: validation

Validate before calling

Attribute[] palette = colors is null || colors.Length == n ? colors : throw new ArgumentException("colors mismatch");
var series = new MultiBarSeries (n, every, spacing, palette);

Type guard

static bool ColorsMatchBars (int n, Attribute[]? c) => c is null || c.Length == n;

Prevention

When it happens

Trigger: new MultiBarSeries(3, 1f, 1f, new Attribute[] { a, b }) — 3 bars but only 2 colors; colors array longer than numberOfBarsPerCategory.

Common situations: Hardcoding a colors array then changing numberOfBarsPerCategory; sharing a palette array across series of different widths; off-by-one in palette generation.

Related errors


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