{"record":{"id":"8086b4a916bba962","repo":"tui-cs/Terminal.Gui","slug":"number-of-steps-must-be-n-1","errorCode":null,"errorMessage":"Number of steps must be N-1","messagePattern":"Number of steps must be N-1","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Drawing/Gradient.cs","lineNumber":84,"sourceCode":"            throw new ArgumentException (\"At least one color stop must be provided.\");\n        }\n\n        _steps = steps.ToList ();\n\n        // If multiple colors and only 1 step assume same distance applies to all steps\n        if (_stops.Count > 2 && _steps.Count == 1)\n        {\n            _steps = Enumerable.Repeat (_steps.Single (), _stops.Count () - 1).ToList ();\n        }\n\n        if (_steps.Any (step => step < 1))\n        {\n            throw new ArgumentException (\"Steps must be greater than 0.\");\n        }\n\n        if (_steps.Count != _stops.Count - 1)\n        {\n            throw new ArgumentException (\"Number of steps must be N-1\");\n        }\n\n        _loop = loop;\n        Spectrum = GenerateGradient (_steps);\n    }\n\n    /// <summary>\n    ///     Returns the color to use at the given part of the spectrum\n    /// </summary>\n    /// <param name=\"fraction\">\n    ///     Proportion of the way through the spectrum, must be between\n    ///     0 and 1 (inclusive).  Returns the last color if <paramref name=\"fraction\"/> is\n    ///     <see cref=\"double.NaN\"/>.\n    /// </param>\n    /// <returns></returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\"></exception>\n    public Color GetColorAtFraction (double fraction)\n    {","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drawing/Gradient.cs#L66-L102","documentation":"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.)","triggerScenarios":"Calling new Gradient(new[]{red,green,blue}, new[]{1}) (3 stops need 2 steps), or supplying a steps list whose length != stops.Count - 1.","commonSituations":"Forgetting the N-1 rule, reusing a steps array after changing the stops, or off-by-one when generating steps programmatically.","solutions":["Compute steps as Enumerable.Repeat(1, stops.Count() - 1) when uniform spacing is acceptable.","Ensure steps.Count == stops.Count - 1 before constructing.","Pass a single step only when you have more than two stops (auto-expand) or exactly two stops."],"exampleFix":"// before\nvar g = new Gradient(stops, steps);\n\n// after\nvar steps = Enumerable.Repeat(defaultStep, Math.Max(1, stops.Count() - 1)).ToList();\nvar g = new Gradient(stops, steps);","handlingStrategy":"validation","validationCode":"if (steps.Count() != stops.Count() - 1)\n    steps = Enumerable.Repeat(1, stops.Count() - 1);","typeGuard":"static bool StepsMatchStops(IList<Color> stops, IList<int> steps) => steps.Count == stops.Count - 1;","tryCatchPattern":"try { g = new Gradient(stops, steps); } catch (ArgumentException) { g = new Gradient(stops, Enumerable.Repeat(1, Math.Max(1, stops.Count()-1))); }","preventionTips":["Remember steps must be N-1 for N stops.","Recompute steps whenever the stops list changes.","Use a single step only with >2 stops (auto-expand) or exactly 2 stops."],"tags":["gradient","drawing","validation","off-by-one","argument-exception"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}