ColorTests.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_IsClosestToNamedColor_ReturnsExpectedValue ()
  22. {
  23. // Arrange
  24. var color1 = new Color (ColorName16.Red);
  25. var color2 = new Color (197, 15, 31); // Red in RGB
  26. Assert.True (color1.IsClosestToNamedColor16 (ColorName16.Red));
  27. Assert.True (color2.IsClosestToNamedColor16 (ColorName16.Red));
  28. }
  29. [Theory]
  30. [CombinatorialData]
  31. public void Rgba_Returns_Expected_Value (
  32. [CombinatorialValues (0, 255)] byte a,
  33. [CombinatorialRange (0, 255, 51)] byte r,
  34. [CombinatorialRange (0, 153, 51)] byte g,
  35. [CombinatorialRange (0, 128, 32)] byte b
  36. )
  37. {
  38. Color color = new (r, g, b, a);
  39. // Color.Rgba is expected to be a signed int32 in little endian order (a,b,g,r)
  40. ReadOnlySpan<byte> littleEndianBytes = [b, g, r, a];
  41. var expectedRgba = BitConverter.ToInt32 (littleEndianBytes);
  42. Assert.Equal (expectedRgba, color.Rgba);
  43. }
  44. }
  45. public static partial class ColorTestsTheoryDataGenerators
  46. {
  47. public static TheoryData<Color, ColorName16> FindClosestColor_ReturnsClosestColor ()
  48. {
  49. TheoryData<Color, ColorName16> data = [];
  50. data.Add (new (0, 0), ColorName16.Black);
  51. data.Add (new (255, 255, 255), ColorName16.White);
  52. data.Add (new (5, 100, 255), ColorName16.BrightBlue);
  53. data.Add (new (0, 255), ColorName16.BrightGreen);
  54. data.Add (new (255, 70, 8), ColorName16.BrightRed);
  55. data.Add (new (0, 128, 128), ColorName16.Cyan);
  56. data.Add (new (128, 64, 32), ColorName16.Yellow);
  57. return data;
  58. }
  59. }