GradientFillTests.cs 4.1 KB

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