PopularityPaletteWithThresholdTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. namespace Terminal.Gui.DrawingTests;
  2. public class PopularityPaletteWithThresholdTests
  3. {
  4. private readonly IColorDistance _colorDistance;
  5. public PopularityPaletteWithThresholdTests () { _colorDistance = new EuclideanColorDistance (); }
  6. [Fact]
  7. public void BuildPalette_EmptyColorList_ReturnsEmptyPalette ()
  8. {
  9. // Arrange
  10. var paletteBuilder = new PopularityPaletteWithThreshold (_colorDistance, 50);
  11. List<Color> colors = new ();
  12. // Act
  13. List<Color> result = paletteBuilder.BuildPalette (colors, 256);
  14. // Assert
  15. Assert.Empty (result);
  16. }
  17. [Fact]
  18. public void BuildPalette_MaxColorsZero_ReturnsEmptyPalette ()
  19. {
  20. // Arrange
  21. var paletteBuilder = new PopularityPaletteWithThreshold (_colorDistance, 50);
  22. List<Color> colors = new () { new (255, 0), new (0, 255) };
  23. // Act
  24. List<Color> result = paletteBuilder.BuildPalette (colors, 0);
  25. // Assert
  26. Assert.Empty (result);
  27. }
  28. [Fact]
  29. public void BuildPalette_SingleColorList_ReturnsSingleColor ()
  30. {
  31. // Arrange
  32. var paletteBuilder = new PopularityPaletteWithThreshold (_colorDistance, 50);
  33. List<Color> colors = new () { new (255, 0), new (255, 0) };
  34. // Act
  35. List<Color> result = paletteBuilder.BuildPalette (colors, 256);
  36. // Assert
  37. Assert.Single (result);
  38. Assert.Equal (new (255, 0), result [0]);
  39. }
  40. [Fact]
  41. public void BuildPalette_ThresholdMergesSimilarColors_WhenColorCountExceedsMax ()
  42. {
  43. // Arrange
  44. var paletteBuilder = new PopularityPaletteWithThreshold (_colorDistance, 50); // Set merge threshold to 50
  45. List<Color> colors = new()
  46. {
  47. new (255, 0), // Red
  48. new (250, 0), // Very close to Red
  49. new (0, 255), // Green
  50. new (0, 250) // Very close to Green
  51. };
  52. // Act
  53. List<Color> result = paletteBuilder.BuildPalette (colors, 2); // Limit palette to 2 colors
  54. // Assert
  55. Assert.Equal (2, result.Count); // Red and Green should be merged with their close colors
  56. Assert.Contains (new (255, 0), result); // Red (or close to Red) should be present
  57. Assert.Contains (new (0, 255), result); // Green (or close to Green) should be present
  58. }
  59. [Fact]
  60. public void BuildPalette_NoMergingIfColorCountIsWithinMax ()
  61. {
  62. // Arrange
  63. var paletteBuilder = new PopularityPaletteWithThreshold (_colorDistance, 50);
  64. List<Color> colors = new ()
  65. {
  66. new (255, 0), // Red
  67. new (0, 255) // Green
  68. };
  69. // Act
  70. List<Color> result = paletteBuilder.BuildPalette (colors, 256); // Set maxColors higher than the number of unique colors
  71. // Assert
  72. Assert.Equal (2, result.Count); // No merging should occur since we are under the limit
  73. Assert.Contains (new (255, 0), result);
  74. Assert.Contains (new (0, 255), result);
  75. }
  76. [Fact]
  77. public void BuildPalette_MergesUntilMaxColorsReached ()
  78. {
  79. // Arrange
  80. var paletteBuilder = new PopularityPaletteWithThreshold (_colorDistance, 50);
  81. List<Color> colors = new()
  82. {
  83. new (255, 0), // Red
  84. new (254, 0), // Close to Red
  85. new (0, 255), // Green
  86. new (0, 254) // Close to Green
  87. };
  88. // Act
  89. List<Color> result = paletteBuilder.BuildPalette (colors, 2); // Set maxColors to 2
  90. // Assert
  91. Assert.Equal (2, result.Count); // Only two colors should be in the final palette
  92. Assert.Contains (new (255, 0), result);
  93. Assert.Contains (new (0, 255), result);
  94. }
  95. }