ColorTests.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #nullable enable
  2. namespace Terminal.Gui.DrawingTests;
  3. public partial class ColorTests
  4. {
  5. [Theory]
  6. [CombinatorialData]
  7. public void Argb_Returns_Expected_Value (
  8. [CombinatorialValues (0, 255)] byte a,
  9. [CombinatorialRange (0, 255, 51)] byte r,
  10. [CombinatorialRange (0, 153, 51)] byte g,
  11. [CombinatorialRange (0, 128, 32)] byte b
  12. )
  13. {
  14. Color color = new (r, g, b, a);
  15. // Color.Rgba is expected to be a signed int32 in little endian order (a,b,g,r)
  16. ReadOnlySpan<byte> littleEndianBytes = [b, g, r, a];
  17. var expectedArgb = BitConverter.ToUInt32 (littleEndianBytes);
  18. Assert.Equal (expectedArgb, color.Argb);
  19. }
  20. [Fact]
  21. public void Color_ColorName_Get_ReturnsClosestColorName ()
  22. {
  23. // Arrange
  24. var color = new Color (128, 64, 40); // Custom RGB color, closest to Yellow
  25. var expectedColorName = ColorName.Yellow;
  26. // Act
  27. ColorName colorName = color.GetClosestNamedColor ();
  28. // Assert
  29. Assert.Equal (expectedColorName, colorName);
  30. }
  31. [Fact]
  32. public void Color_IsClosestToNamedColor_ReturnsExpectedValue ()
  33. {
  34. // Arrange
  35. var color1 = new Color (ColorName.Red);
  36. var color2 = new Color (197, 15, 31); // Red in RGB
  37. Assert.True (color1.IsClosestToNamedColor (ColorName.Red));
  38. Assert.True (color2.IsClosestToNamedColor (ColorName.Red));
  39. }
  40. [Theory]
  41. [MemberData (
  42. nameof (ColorTestsTheoryDataGenerators.FindClosestColor_ReturnsClosestColor),
  43. MemberType = typeof (ColorTestsTheoryDataGenerators)
  44. )]
  45. public void FindClosestColor_ReturnsClosestColor (Color inputColor, ColorName expectedColorName)
  46. {
  47. ColorName actualColorName = Color.GetClosestNamedColor (inputColor);
  48. Assert.Equal (expectedColorName, actualColorName);
  49. }
  50. [Theory]
  51. [CombinatorialData]
  52. public void Rgba_Returns_Expected_Value (
  53. [CombinatorialValues (0, 255)] byte a,
  54. [CombinatorialRange (0, 255, 51)] byte r,
  55. [CombinatorialRange (0, 153, 51)] byte g,
  56. [CombinatorialRange (0, 128, 32)] byte b
  57. )
  58. {
  59. Color color = new (r, g, b, a);
  60. // Color.Rgba is expected to be a signed int32 in little endian order (a,b,g,r)
  61. ReadOnlySpan<byte> littleEndianBytes = [b, g, r, a];
  62. var expectedRgba = BitConverter.ToInt32 (littleEndianBytes);
  63. Assert.Equal (expectedRgba, color.Rgba);
  64. }
  65. }
  66. public static partial class ColorTestsTheoryDataGenerators
  67. {
  68. public static TheoryData<Color, ColorName> FindClosestColor_ReturnsClosestColor ()
  69. {
  70. TheoryData<Color, ColorName> data = [];
  71. data.Add (new Color (0, 0), ColorName.Black);
  72. data.Add (new Color (255, 255, 255), ColorName.White);
  73. data.Add (new Color (5, 100, 255), ColorName.BrightBlue);
  74. data.Add (new Color (0, 255), ColorName.BrightGreen);
  75. data.Add (new Color (255, 70, 8), ColorName.BrightRed);
  76. data.Add (new Color (0, 128, 128), ColorName.Cyan);
  77. data.Add (new Color (128, 64, 32), ColorName.Yellow);
  78. return data;
  79. }
  80. }