GradientFillTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. namespace Terminal.Gui.DrawingTests;
  2. public class GradientFillTests
  3. {
  4. private readonly Gradient _gradient;
  5. public GradientFillTests ()
  6. {
  7. // Define the colors of the gradient stops
  8. List<Color> stops = new List<Color>
  9. {
  10. new (255, 0), // Red
  11. new (0, 0, 255) // Blue
  12. };
  13. // Define the number of steps between each color
  14. List<int> steps = new() { 10 }; // 10 steps between Red -> Blue
  15. _gradient = new (stops, steps);
  16. }
  17. [Fact]
  18. public void TestGradientFillCorners_AtOrigin ()
  19. {
  20. var area = new Rectangle (0, 0, 10, 10);
  21. var gradientFill = new GradientFill (area, _gradient, GradientDirection.Diagonal);
  22. // Test the corners
  23. var topLeft = new Point (0, 0);
  24. var topRight = new Point (area.Width - 1, 0);
  25. var bottomLeft = new Point (0, area.Height - 1);
  26. var bottomRight = new Point (area.Width - 1, area.Height - 1);
  27. Color topLeftColor = gradientFill.GetColor (topLeft);
  28. Color topRightColor = gradientFill.GetColor (topRight);
  29. Color bottomLeftColor = gradientFill.GetColor (bottomLeft);
  30. Color bottomRightColor = gradientFill.GetColor (bottomRight);
  31. // Expected colors
  32. var expectedTopLeftColor = new Color (255, 0); // Red
  33. var expectedBottomRightColor = new Color (0, 0, 255); // Blue
  34. Assert.Equal (expectedTopLeftColor, topLeftColor);
  35. Assert.Equal (expectedBottomRightColor, bottomRightColor);
  36. }
  37. [Fact]
  38. public void TestGradientFillCorners_NotAtOrigin ()
  39. {
  40. var area = new Rectangle (5, 5, 10, 10);
  41. var gradientFill = new GradientFill (area, _gradient, GradientDirection.Diagonal);
  42. // Test the corners
  43. var topLeft = new Point (5, 5);
  44. var topRight = new Point (area.Right - 1, 5);
  45. var bottomLeft = new Point (5, area.Bottom - 1);
  46. var bottomRight = new Point (area.Right - 1, area.Bottom - 1);
  47. Color topLeftColor = gradientFill.GetColor (topLeft);
  48. Color topRightColor = gradientFill.GetColor (topRight);
  49. Color bottomLeftColor = gradientFill.GetColor (bottomLeft);
  50. Color bottomRightColor = gradientFill.GetColor (bottomRight);
  51. // Expected colors
  52. var expectedTopLeftColor = new Color (255, 0); // Red
  53. var expectedBottomRightColor = new Color (0, 0, 255); // Blue
  54. Assert.Equal (expectedTopLeftColor, topLeftColor);
  55. Assert.Equal (expectedBottomRightColor, bottomRightColor);
  56. }
  57. [Fact]
  58. public void TestGradientFillColorTransition ()
  59. {
  60. var area = new Rectangle (0, 0, 10, 10);
  61. var gradientFill = new GradientFill (area, _gradient, GradientDirection.Diagonal);
  62. for (var row = 0; row < area.Height; row++)
  63. {
  64. var previousRed = 255;
  65. var previousBlue = 0;
  66. for (var col = 0; col < area.Width; col++)
  67. {
  68. var point = new Point (col, row);
  69. Color color = gradientFill.GetColor (point);
  70. // Check if the current color is 'more blue' and 'less red' as it goes right and down
  71. Assert.True (color.R <= previousRed, $"Failed at ({col}, {row}): {color.R} > {previousRed}");
  72. Assert.True (color.B >= previousBlue, $"Failed at ({col}, {row}): {color.B} < {previousBlue}");
  73. // Update the previous color values for the next iteration
  74. previousRed = color.R;
  75. previousBlue = color.B;
  76. }
  77. }
  78. for (var col = 0; col < area.Width; col++)
  79. {
  80. var previousRed = 255;
  81. var previousBlue = 0;
  82. for (var row = 0; row < area.Height; row++)
  83. {
  84. var point = new Point (col, row);
  85. Color color = gradientFill.GetColor (point);
  86. // Check if the current color is 'more blue' and 'less red' as it goes right and down
  87. Assert.True (color.R <= previousRed, $"Failed at ({col}, {row}): {color.R} > {previousRed}");
  88. Assert.True (color.B >= previousBlue, $"Failed at ({col}, {row}): {color.B} < {previousBlue}");
  89. // Update the previous color values for the next iteration
  90. previousRed = color.R;
  91. previousBlue = color.B;
  92. }
  93. }
  94. }
  95. }