GradientFillTests.cs 4.1 KB

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