GradientFillTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 
  2. namespace Terminal.Gui.DrawingTests;
  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, GradientDirection.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. // Expected colors
  33. var expectedTopLeftColor = new Terminal.Gui.Color (255, 0, 0); // Red
  34. var expectedBottomRightColor = new Terminal.Gui.Color (0, 0, 255); // Blue
  35. Assert.Equal (expectedTopLeftColor, topLeftColor);
  36. Assert.Equal (expectedBottomRightColor, bottomRightColor);
  37. // Additional checks can be added to verify the exact expected colors if known
  38. Console.WriteLine ($"Top-left: {topLeftColor}");
  39. Console.WriteLine ($"Top-right: {topRightColor}");
  40. Console.WriteLine ($"Bottom-left: {bottomLeftColor}");
  41. Console.WriteLine ($"Bottom-right: {bottomRightColor}");
  42. }
  43. [Fact]
  44. public void TestGradientFillColorTransition ()
  45. {
  46. var area = new Rectangle (0, 0, 10, 10);
  47. var gradientFill = new GradientFill (area, _gradient, GradientDirection.Diagonal);
  48. for (int row = 0; row < area.Height; row++)
  49. {
  50. int previousRed = 255;
  51. int previousBlue = 0;
  52. for (int col = 0; col < area.Width; col++)
  53. {
  54. var point = new Point (col, row);
  55. var color = gradientFill.GetColor (point);
  56. // Check if the current color is 'more blue' and 'less red' as it goes right and down
  57. Assert.True (color.R <= previousRed, $"Failed at ({col}, {row}): {color.R} > {previousRed}");
  58. Assert.True (color.B >= previousBlue, $"Failed at ({col}, {row}): {color.B} < {previousBlue}");
  59. // Update the previous color values for the next iteration
  60. previousRed = color.R;
  61. previousBlue = color.B;
  62. }
  63. }
  64. for (int col = 0; col < area.Width; col++)
  65. {
  66. int previousRed = 255;
  67. int previousBlue = 0;
  68. for (int row = 0; row < area.Height; row++)
  69. {
  70. var point = new Point (col, row);
  71. var color = gradientFill.GetColor (point);
  72. // Check if the current color is 'more blue' and 'less red' as it goes right and down
  73. Assert.True (color.R <= previousRed, $"Failed at ({col}, {row}): {color.R} > {previousRed}");
  74. Assert.True (color.B >= previousBlue, $"Failed at ({col}, {row}): {color.B} < {previousBlue}");
  75. // Update the previous color values for the next iteration
  76. previousRed = color.R;
  77. previousBlue = color.B;
  78. }
  79. }
  80. }
  81. }