diff --git a/Terminal.Gui/Drawing/Gradient.cs b/Terminal.Gui/Drawing/Gradient.cs
index b0cdf072f..7aea37b90 100644
--- a/Terminal.Gui/Drawing/Gradient.cs
+++ b/Terminal.Gui/Drawing/Gradient.cs
@@ -58,10 +58,26 @@ public class Gradient
Spectrum = GenerateGradient (_steps);
}
+ ///
+ /// Returns the color to use at the given part of the spectrum
+ ///
+ /// Proportion of the way through the spectrum, must be between
+ /// 0 and 1 (inclusive). Returns the last color if is
+ /// .
+ ///
+ ///
public Color GetColorAtFraction (double fraction)
{
+ if (double.IsNaN (fraction))
+ {
+ return Spectrum.Last ();
+ }
+
if (fraction < 0 || fraction > 1)
+ {
throw new ArgumentOutOfRangeException (nameof (fraction), "Fraction must be between 0 and 1.");
+ }
+
int index = (int)(fraction * (Spectrum.Count - 1));
return Spectrum [index];
}
@@ -106,6 +122,19 @@ public class Gradient
}
}
+ ///
+ ///
+ /// Creates a mapping starting at 0,0 and going to and
+ /// (inclusively) using the supplied .
+ ///
+ ///
+ /// Note that this method is inclusive i.e. passing 1/1 results in 4 mapped coordinates.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
public Dictionary BuildCoordinateColorMapping (int maxRow, int maxColumn, GradientDirection direction)
{
var gradientMapping = new Dictionary ();
diff --git a/Terminal.Gui/Drawing/GradientFill.cs b/Terminal.Gui/Drawing/GradientFill.cs
index 393a183f4..31339ebe0 100644
--- a/Terminal.Gui/Drawing/GradientFill.cs
+++ b/Terminal.Gui/Drawing/GradientFill.cs
@@ -18,7 +18,7 @@ public class GradientFill : IFill
///
public GradientFill (Rectangle area, Gradient gradient, GradientDirection direction)
{
- _map = gradient.BuildCoordinateColorMapping (area.Height, area.Width, direction);
+ _map = gradient.BuildCoordinateColorMapping (area.Height-1, area.Width-1, direction);
}
///
diff --git a/UnitTests/TextEffects/New/GradientFillTests.cs b/UnitTests/Drawing/GradientFillTests.cs
similarity index 94%
rename from UnitTests/TextEffects/New/GradientFillTests.cs
rename to UnitTests/Drawing/GradientFillTests.cs
index 8349edd6d..fd6822539 100644
--- a/UnitTests/TextEffects/New/GradientFillTests.cs
+++ b/UnitTests/Drawing/GradientFillTests.cs
@@ -1,6 +1,5 @@
-using Terminal.Gui.Drawing;
-
-namespace Terminal.Gui.TextEffects.Tests;
+
+namespace Terminal.Gui.DrawingTests;
public class GradientFillTests
{
@@ -29,9 +28,9 @@ public class GradientFillTests
// Test the corners
var topLeft = new Point (0, 0);
- var topRight = new Point (area.Width, 0);
- var bottomLeft = new Point (0, area.Height );
- var bottomRight = new Point (area.Width, area.Height);
+ var topRight = new Point (area.Width - 1, 0);
+ var bottomLeft = new Point (0, area.Height - 1);
+ var bottomRight = new Point (area.Width - 1, area.Height - 1);
var topLeftColor = gradientFill.GetColor (topLeft);
var topRightColor = gradientFill.GetColor (topRight);
diff --git a/UnitTests/Drawing/GradientTests.cs b/UnitTests/Drawing/GradientTests.cs
new file mode 100644
index 000000000..e9e0194ea
--- /dev/null
+++ b/UnitTests/Drawing/GradientTests.cs
@@ -0,0 +1,53 @@
+
+namespace Terminal.Gui.DrawingTests;
+
+public class GradientTests
+{
+ // Static method to provide all enum values
+ public static IEnumerable