tui-cs/Terminal.Gui · error · ArgumentException

Number of steps must be N-1

Error message

Number of steps must be N-1

What it means

Thrown by the Gradient constructor when the number of steps does not equal the number of stops minus one (N-1). Each adjacent stop pair needs exactly one step count, so for N stops you must supply N-1 steps. It is an ArgumentException. (A single step is auto-expanded only when there are more than two stops.)

Source

Thrown at Terminal.Gui/Drawing/Gradient.cs:84

            throw new ArgumentException ("At least one color stop must be provided.");
        }

        _steps = steps.ToList ();

        // If multiple colors and only 1 step assume same distance applies to all steps
        if (_stops.Count > 2 && _steps.Count == 1)
        {
            _steps = Enumerable.Repeat (_steps.Single (), _stops.Count () - 1).ToList ();
        }

        if (_steps.Any (step => step < 1))
        {
            throw new ArgumentException ("Steps must be greater than 0.");
        }

        if (_steps.Count != _stops.Count - 1)
        {
            throw new ArgumentException ("Number of steps must be N-1");
        }

        _loop = loop;
        Spectrum = GenerateGradient (_steps);
    }

    /// <summary>
    ///     Returns the color to use at the given part of the spectrum
    /// </summary>
    /// <param name="fraction">
    ///     Proportion of the way through the spectrum, must be between
    ///     0 and 1 (inclusive).  Returns the last color if <paramref name="fraction"/> is
    ///     <see cref="double.NaN"/>.
    /// </param>
    /// <returns></returns>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    public Color GetColorAtFraction (double fraction)
    {

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Compute steps as Enumerable.Repeat(1, stops.Count() - 1) when uniform spacing is acceptable.
  2. Ensure steps.Count == stops.Count - 1 before constructing.
  3. Pass a single step only when you have more than two stops (auto-expand) or exactly two stops.

Example fix

// before
var g = new Gradient(stops, steps);

// after
var steps = Enumerable.Repeat(defaultStep, Math.Max(1, stops.Count() - 1)).ToList();
var g = new Gradient(stops, steps);
Defensive patterns

Strategy: validation

Validate before calling

if (steps.Count() != stops.Count() - 1)
    steps = Enumerable.Repeat(1, stops.Count() - 1);

Type guard

static bool StepsMatchStops(IList<Color> stops, IList<int> steps) => steps.Count == stops.Count - 1;

Try / catch

try { g = new Gradient(stops, steps); } catch (ArgumentException) { g = new Gradient(stops, Enumerable.Repeat(1, Math.Max(1, stops.Count()-1))); }

Prevention

When it happens

Trigger: Calling new Gradient(new[]{red,green,blue}, new[]{1}) (3 stops need 2 steps), or supplying a steps list whose length != stops.Count - 1.

Common situations: Forgetting the N-1 rule, reusing a steps array after changing the stops, or off-by-one when generating steps programmatically.

Related errors


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