ColorTests.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System;
  2. using System.Linq;
  3. using Xunit;
  4. namespace Terminal.Gui.DrawingTests;
  5. public class ColorTests {
  6. [Fact]
  7. public void Color_Is_Value_Type () =>
  8. // prove that Color is a value type
  9. Assert.True (typeof (Color).IsValueType);
  10. [Fact]
  11. public void TestAllColors ()
  12. {
  13. var colorNames = Enum.GetValues (typeof (ColorName)).Cast<int> ().Distinct ().ToList ();
  14. var attrs = new Attribute [colorNames.Count];
  15. var idx = 0;
  16. foreach (ColorName bg in colorNames) {
  17. attrs [idx] = new Attribute (bg, colorNames.Count - 1 - bg);
  18. idx++;
  19. }
  20. Assert.Equal (16, attrs.Length);
  21. Assert.Equal (new Attribute (Color.Black, Color.White), attrs [0]);
  22. Assert.Equal (new Attribute (Color.Blue, Color.BrightYellow), attrs [1]);
  23. Assert.Equal (new Attribute (Color.Green, Color.BrightMagenta), attrs [2]);
  24. Assert.Equal (new Attribute (Color.Cyan, Color.BrightRed), attrs [3]);
  25. Assert.Equal (new Attribute (Color.Red, Color.BrightCyan), attrs [4]);
  26. Assert.Equal (new Attribute (Color.Magenta, Color.BrightGreen), attrs [5]);
  27. Assert.Equal (new Attribute (Color.Yellow, Color.BrightBlue), attrs [6]);
  28. Assert.Equal (new Attribute (Color.Gray, Color.DarkGray), attrs [7]);
  29. Assert.Equal (new Attribute (Color.DarkGray, Color.Gray), attrs [8]);
  30. Assert.Equal (new Attribute (Color.BrightBlue, Color.Yellow), attrs [9]);
  31. Assert.Equal (new Attribute (Color.BrightGreen, Color.Magenta), attrs [10]);
  32. Assert.Equal (new Attribute (Color.BrightCyan, Color.Red), attrs [11]);
  33. Assert.Equal (new Attribute (Color.BrightRed, Color.Cyan), attrs [12]);
  34. Assert.Equal (new Attribute (Color.BrightMagenta, Color.Green), attrs [13]);
  35. Assert.Equal (new Attribute (Color.BrightYellow, Color.Blue), attrs [14]);
  36. Assert.Equal (new Attribute (Color.White, Color.Black), attrs [^1]);
  37. }
  38. [Fact]
  39. public void ColorNames_HasOnly16DistinctElements () => Assert.Equal (16, Enum.GetValues (typeof (ColorName)).Cast<int> ().Distinct ().Count ());
  40. [Fact]
  41. public void ColorNames_HaveCorrectOrdinals ()
  42. {
  43. Assert.Equal (0, (int)ColorName.Black);
  44. Assert.Equal (1, (int)ColorName.Blue);
  45. Assert.Equal (2, (int)ColorName.Green);
  46. Assert.Equal (3, (int)ColorName.Cyan);
  47. Assert.Equal (4, (int)ColorName.Red);
  48. Assert.Equal (5, (int)ColorName.Magenta);
  49. Assert.Equal (6, (int)ColorName.Yellow);
  50. Assert.Equal (7, (int)ColorName.Gray);
  51. Assert.Equal (8, (int)ColorName.DarkGray);
  52. Assert.Equal (9, (int)ColorName.BrightBlue);
  53. Assert.Equal (10, (int)ColorName.BrightGreen);
  54. Assert.Equal (11, (int)ColorName.BrightCyan);
  55. Assert.Equal (12, (int)ColorName.BrightRed);
  56. Assert.Equal (13, (int)ColorName.BrightMagenta);
  57. Assert.Equal (14, (int)ColorName.BrightYellow);
  58. Assert.Equal (15, (int)ColorName.White);
  59. }
  60. [Fact]
  61. public void Color_Constructor_WithRGBValues ()
  62. {
  63. // Arrange
  64. var expectedR = 255;
  65. var expectedG = 0;
  66. var expectedB = 128;
  67. // Act
  68. var color = new Color (expectedR, expectedG, expectedB);
  69. // Assert
  70. Assert.Equal (expectedR, color.R);
  71. Assert.Equal (expectedG, color.G);
  72. Assert.Equal (expectedB, color.B);
  73. Assert.Equal (0xFF, color.A); // Alpha should be FF by default
  74. }
  75. [Fact]
  76. public void Color_Constructor_WithAlphaAndRGBValues ()
  77. {
  78. // Arrange
  79. var expectedA = 128;
  80. var expectedR = 255;
  81. var expectedG = 0;
  82. var expectedB = 128;
  83. // Act
  84. var color = new Color (expectedR, expectedG, expectedB, expectedA);
  85. // Assert
  86. Assert.Equal (expectedR, color.R);
  87. Assert.Equal (expectedG, color.G);
  88. Assert.Equal (expectedB, color.B);
  89. Assert.Equal (expectedA, color.A);
  90. }
  91. [Fact]
  92. public void Color_Constructor_WithRgbaValue ()
  93. {
  94. // Arrange
  95. var expectedRgba = unchecked((int)0xFF804040); // R: 128, G: 64, B: 64, Alpha: 255
  96. // Act
  97. var color = new Color (expectedRgba);
  98. // Assert
  99. Assert.Equal (128, color.R);
  100. Assert.Equal (64, color.G);
  101. Assert.Equal (64, color.B);
  102. Assert.Equal (255, color.A);
  103. }
  104. [Fact]
  105. public void Color_Constructor_WithColorName ()
  106. {
  107. // Arrange
  108. var colorName = ColorName.Blue;
  109. var expectedColor = new Color (0, 55, 218); // Blue
  110. // Act
  111. var color = new Color (colorName);
  112. // Assert
  113. Assert.Equal (expectedColor, color);
  114. }
  115. [Fact]
  116. public void Color_ToString_WithNamedColor ()
  117. {
  118. // Arrange
  119. var color = new Color (0, 55, 218); // Blue
  120. // Act
  121. var colorString = color.ToString ();
  122. // Assert
  123. Assert.Equal ("Blue", colorString);
  124. }
  125. [Fact]
  126. public void Color_ToString_WithRGBColor ()
  127. {
  128. // Arrange
  129. var color = new Color (1, 64, 32); // Custom RGB color
  130. // Act
  131. var colorString = color.ToString ();
  132. // Assert
  133. Assert.Equal ("#014020", colorString);
  134. }
  135. [Fact]
  136. public void Color_ImplicitOperator_FromInt ()
  137. {
  138. // Arrange
  139. var Rgba = unchecked((int)0xFF804020); // R: 128, G: 64, B: 32, Alpha: 255
  140. var expectedColor = new Color (128, 64, 32);
  141. // Act
  142. Color color = Rgba;
  143. // Assert
  144. Assert.Equal (expectedColor, color);
  145. }
  146. [Fact]
  147. public void Color_ExplicitOperator_ToInt ()
  148. {
  149. // Arrange
  150. var color = new Color (128, 64, 32);
  151. var expectedRgba = unchecked((int)0xFF804020); // R: 128, G: 64, B: 32, Alpha: 255
  152. // Act
  153. var Rgba = (int)color;
  154. // Assert
  155. Assert.Equal (expectedRgba, Rgba);
  156. }
  157. [Fact]
  158. public void Color_ImplicitOperator_FromColorNames ()
  159. {
  160. // Arrange
  161. var colorName = ColorName.Blue;
  162. var expectedColor = new Color (0, 55, 218); // Blue
  163. // Act
  164. var color = new Color (colorName);
  165. // Assert
  166. Assert.Equal (expectedColor, color);
  167. }
  168. [Fact]
  169. public void Color_ExplicitOperator_ToColorNames ()
  170. {
  171. // Arrange
  172. var color = new Color (0, 0, 0x80); // Blue
  173. var expectedColorName = ColorName.Blue;
  174. // Act
  175. var colorName = (ColorName)color;
  176. // Assert
  177. Assert.Equal (expectedColorName, colorName);
  178. }
  179. [Fact]
  180. public void Color_EqualityOperator_WithColorAndColor ()
  181. {
  182. // Arrange
  183. var color1 = new Color (255, 128, 64, 32);
  184. var color2 = new Color (255, 128, 64, 32);
  185. // Act & Assert
  186. Assert.True (color1 == color2);
  187. Assert.False (color1 != color2);
  188. }
  189. [Fact]
  190. public void Color_InequalityOperator_WithColorAndColor ()
  191. {
  192. // Arrange
  193. var color1 = new Color (255, 128, 64, 32);
  194. var color2 = new Color (128, 64, 32, 16);
  195. // Act & Assert
  196. Assert.False (color1 == color2);
  197. Assert.True (color1 != color2);
  198. }
  199. [Fact]
  200. public void Color_EqualityOperator_WithColorNamesAndColor ()
  201. {
  202. // Arrange
  203. var color1 = new Color (ColorName.Red);
  204. var color2 = new Color (197, 15, 31); // Red in RGB
  205. // Act & Assert
  206. Assert.True (ColorName.Red == color1);
  207. Assert.False (ColorName.Red != color1);
  208. Assert.True (color1 == ColorName.Red);
  209. Assert.False (color1 != ColorName.Red);
  210. Assert.True (color2 == ColorName.Red);
  211. Assert.False (color2 != ColorName.Red);
  212. }
  213. [Fact]
  214. public void Color_InequalityOperator_WithColorNamesAndColor ()
  215. {
  216. // Arrange
  217. var color1 = new Color (ColorName.Red);
  218. var color2 = new Color (58, 150, 221); // Cyan in RGB
  219. // Act & Assert
  220. Assert.False (ColorName.Red == color2);
  221. Assert.True (ColorName.Red != color2);
  222. Assert.False (color2 == ColorName.Red);
  223. Assert.True (color2 != ColorName.Red);
  224. }
  225. [Fact]
  226. public void Color_FromColorName_ConvertsColorNamesToColor ()
  227. {
  228. // Arrange
  229. var colorName = ColorName.Red;
  230. var expectedColor = new Color (197, 15, 31); // Red in RGB
  231. // Act
  232. var convertedColor = new Color (colorName);
  233. // Assert
  234. Assert.Equal (expectedColor, convertedColor);
  235. }
  236. [Fact]
  237. public void Color_ColorName_Get_ReturnsClosestColorName ()
  238. {
  239. // Arrange
  240. var color = new Color (128, 64, 40); // Custom RGB color, closest to Yellow
  241. var expectedColorName = ColorName.Yellow;
  242. // Act
  243. var colorName = color.ColorName;
  244. // Assert
  245. Assert.Equal (expectedColorName, colorName);
  246. }
  247. [Fact]
  248. public void FindClosestColor_ReturnsClosestColor ()
  249. {
  250. // Test cases with RGB values and expected closest color names
  251. var testCases = new [] {
  252. (new Color (0, 0, 0), ColorName.Black),
  253. (new Color (255, 255, 255), ColorName.White),
  254. (new Color (5, 100, 255), ColorName.BrightBlue),
  255. (new Color (0, 255, 0), ColorName.BrightGreen),
  256. (new Color (255, 70, 8), ColorName.BrightRed),
  257. (new Color (0, 128, 128), ColorName.Cyan),
  258. (new Color (128, 64, 32), ColorName.Yellow)
  259. };
  260. foreach (var testCase in testCases) {
  261. var inputColor = testCase.Item1;
  262. var expectedColorName = testCase.Item2;
  263. var actualColorName = Color.FindClosestColor (inputColor);
  264. Assert.Equal (expectedColorName, actualColorName);
  265. }
  266. }
  267. }