{"record":{"id":"8d93813a1c815e9a","repo":"tui-cs/Terminal.Gui","slug":"fraction-must-be-between-0-and-1","errorCode":null,"errorMessage":"Fraction must be between 0 and 1.","messagePattern":"Fraction must be between 0 and 1\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Drawing/Gradient.cs","lineNumber":110,"sourceCode":"    ///     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    {\n        if (double.IsNaN (fraction))\n        {\n            return Spectrum.Last ();\n        }\n\n        if (fraction is < 0 or > 1)\n        {\n            throw new ArgumentOutOfRangeException (nameof (fraction), @\"Fraction must be between 0 and 1.\");\n        }\n\n        var index = (int)(fraction * (Spectrum.Count - 1));\n\n        return Spectrum [index];\n    }\n\n    private List<Color> GenerateGradient (IEnumerable<int> steps)\n    {\n        List<Color> gradient = new ();\n\n        if (_stops.Count == 1)\n        {\n            for (var i = 0; i < steps.Sum (); i++)\n            {\n                gradient.Add (_stops [0]);\n            }\n","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Drawing/Gradient.cs#L92-L128","documentation":"Thrown by Gradient.GetColorAtFraction when the fraction is outside [0,1]. Note that NaN is explicitly handled (returns the last spectrum color) and does NOT throw; only values below 0 or above 1 do. It is an ArgumentOutOfRangeException. The fraction maps a position along the generated spectrum.","triggerScenarios":"Calling gradient.GetColorAtFraction(1.5), GetColorAtFraction(-0.1), or passing an un-normalized ratio like progress/50 where progress can exceed the denominator.","commonSituations":"Passing a raw progress value instead of a normalized fraction, animation/easing producing overshoot above 1, or division producing negative results.","solutions":["Clamp the fraction: double f = Math.Clamp(value, 0.0, 1.0); before calling.","Normalize the source value by its full range before calling.","Handle NaN explicitly if your source can produce it (it won't throw, but clamp for determinism)."],"exampleFix":"// before\nColor c = gradient.GetColorAtFraction(progress / max);\n\n// after\ndouble f = Math.Clamp(progress / (double)max, 0.0, 1.0);\nColor c = gradient.GetColorAtFraction(f);","handlingStrategy":"validation","validationCode":"double f = double.IsNaN(fraction) ? 1.0 : Math.Clamp(fraction, 0.0, 1.0);","typeGuard":"static bool IsValidFraction(double f) => double.IsNaN(f) || f is >= 0.0 and <= 1.0;","tryCatchPattern":"try { c = gradient.GetColorAtFraction(f); } catch (ArgumentOutOfRangeException) { c = gradient.GetColorAtFraction(0); }","preventionTips":["Always normalize and clamp ratios to [0,1] before sampling the gradient.","Guard against easing functions that overshoot beyond 1.","Remember NaN does not throw (returns last color)."],"tags":["gradient","drawing","validation","argument-out-of-range"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}