Browse Source

Tests, xmldoc and guards

tznind 1 year ago
parent
commit
a167366b14

+ 29 - 0
Terminal.Gui/Drawing/Gradient.cs

@@ -58,10 +58,26 @@ public class Gradient
         Spectrum = GenerateGradient (_steps);
         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)
     public Color GetColorAtFraction (double fraction)
     {
     {
+        if (double.IsNaN (fraction))
+        {
+            return Spectrum.Last ();
+        }
+
         if (fraction < 0 || fraction > 1)
         if (fraction < 0 || fraction > 1)
+        {
             throw new ArgumentOutOfRangeException (nameof (fraction), "Fraction must be between 0 and 1.");
             throw new ArgumentOutOfRangeException (nameof (fraction), "Fraction must be between 0 and 1.");
+        }
+
         int index = (int)(fraction * (Spectrum.Count - 1));
         int index = (int)(fraction * (Spectrum.Count - 1));
         return Spectrum [index];
         return Spectrum [index];
     }
     }
@@ -106,6 +122,19 @@ public class Gradient
         }
         }
     }
     }
 
 
+    /// <summary>
+    /// <para>
+    /// Creates a mapping starting at 0,0 and going to <paramref name="maxRow"/> and <paramref name="maxColumn"/>
+    /// (inclusively) using the supplied <paramref name="direction"/>.
+    /// </para>
+    /// <para>
+    /// Note that this method is inclusive i.e. passing 1/1 results in 4 mapped coordinates.
+    /// </para>
+    /// </summary>
+    /// <param name="maxRow"></param>
+    /// <param name="maxColumn"></param>
+    /// <param name="direction"></param>
+    /// <returns></returns>
     public Dictionary<Point, Color> BuildCoordinateColorMapping (int maxRow, int maxColumn, GradientDirection direction)
     public Dictionary<Point, Color> BuildCoordinateColorMapping (int maxRow, int maxColumn, GradientDirection direction)
     {
     {
         var gradientMapping = new Dictionary<Point, Color> ();
         var gradientMapping = new Dictionary<Point, Color> ();

+ 1 - 1
Terminal.Gui/Drawing/GradientFill.cs

@@ -18,7 +18,7 @@ public class GradientFill : IFill
     /// <param name="direction"></param>
     /// <param name="direction"></param>
     public GradientFill (Rectangle area, Gradient gradient, GradientDirection direction)
     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);
     }
     }
 
 
     /// <summary>
     /// <summary>

+ 5 - 6
UnitTests/TextEffects/New/GradientFillTests.cs → UnitTests/Drawing/GradientFillTests.cs

@@ -1,6 +1,5 @@
-using Terminal.Gui.Drawing;
-
-namespace Terminal.Gui.TextEffects.Tests;
+
+namespace Terminal.Gui.DrawingTests;
 
 
 public class GradientFillTests
 public class GradientFillTests
 {
 {
@@ -29,9 +28,9 @@ public class GradientFillTests
 
 
         // Test the corners
         // Test the corners
         var topLeft = new Point (0, 0);
         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 topLeftColor = gradientFill.GetColor (topLeft);
         var topRightColor = gradientFill.GetColor (topRight);
         var topRightColor = gradientFill.GetColor (topRight);

+ 53 - 0
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<object []> GradientDirectionValues ()
+    {
+        return typeof (GradientDirection).GetEnumValues ()
+            .Cast<GradientDirection> ()
+            .Select (direction => new object [] { direction });
+    }
+
+    [Theory]
+    [MemberData (nameof (GradientDirectionValues))]
+    public void GradientIsInclusive_2_by_2 (GradientDirection direction)
+    {
+        // Define the colors of the gradient stops
+        var stops = new List<Color>
+            {
+                new Color(255, 0, 0),    // Red
+                new Color(0, 0, 255)     // Blue
+            };
+
+        // Define the number of steps between each color
+        var steps = new List<int> { 10 }; // 10 steps between Red -> Blue
+
+        var g = new Gradient (stops, steps, loop: false);
+        Assert.Equal (4, g.BuildCoordinateColorMapping (1, 1, direction).Count);
+    }
+
+    [Theory]
+    [MemberData (nameof (GradientDirectionValues))]
+    public void GradientIsInclusive_1_by_1 (GradientDirection direction)
+    {
+        // Define the colors of the gradient stops
+        var stops = new List<Color>
+            {
+                new Color(255, 0, 0),    // Red
+                new Color(0, 0, 255)     // Blue
+            };
+
+        // Define the number of steps between each color
+        var steps = new List<int> { 10 }; // 10 steps between Red -> Blue
+
+        var g = new Gradient (stops, steps, loop: false);
+
+        // Note that
+        var c = Assert.Single (g.BuildCoordinateColorMapping (0, 0, direction));
+        Assert.Equal (c.Key, new Point(0,0));
+        Assert.Equal (c.Value, new Color (0, 0, 255));
+    }
+}