effects_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package rgb
  2. import (
  3. "testing"
  4. )
  5. func TestQMKValueIDs(t *testing.T) {
  6. if Brightness != 1 {
  7. t.Errorf("Brightness = %d, want 1", Brightness)
  8. }
  9. if EffectID != 2 {
  10. t.Errorf("EffectID = %d, want 2", EffectID)
  11. }
  12. if Speed != 3 {
  13. t.Errorf("Speed = %d, want 3", Speed)
  14. }
  15. if ColorValue != 4 {
  16. t.Errorf("ColorValue = %d, want 4", ColorValue)
  17. }
  18. }
  19. func TestParseHexColor(t *testing.T) {
  20. cases := []struct {
  21. name string
  22. input string
  23. want Color
  24. err bool
  25. }{
  26. {"red", "ff0000", Color{R: 255, G: 0, B: 0}, false},
  27. {"green", "00ff00", Color{R: 0, G: 255, B: 0}, false},
  28. {"blue", "0000ff", Color{R: 0, G: 0, B: 255}, false},
  29. {"white", "ffffff", Color{R: 255, G: 255, B: 255}, false},
  30. {"black", "000000", Color{R: 0, G: 0, B: 0}, false},
  31. {"lowercase", "aabbcc", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
  32. {"uppercase", "AABBCC", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
  33. {"short", "ff000", Color{0, 0, 0}, true},
  34. {"long", "ff00000", Color{0, 0, 0}, true},
  35. {"empty", "", Color{0, 0, 0}, true},
  36. {"invalid", "gggggg", Color{}, true},
  37. }
  38. for _, tc := range cases {
  39. t.Run(tc.name, func(t *testing.T) {
  40. got, err := ParseHexColor(tc.input)
  41. if tc.err {
  42. if err == nil {
  43. t.Errorf("ParseHexColor(%q) expected error, got nil", tc.input)
  44. }
  45. return
  46. }
  47. if err != nil {
  48. t.Errorf("ParseHexColor(%q) unexpected error: %v", tc.input, err)
  49. return
  50. }
  51. if got != tc.want {
  52. t.Errorf("ParseHexColor(%q) = %+v, want %+v", tc.input, got, tc.want)
  53. }
  54. })
  55. }
  56. }
  57. func TestColorToHSV(t *testing.T) {
  58. cases := []struct {
  59. name string
  60. color Color
  61. wantH uint8
  62. wantS uint8
  63. wantV uint8
  64. }{
  65. {"red", Color{R: 255, G: 0, B: 0}, 0, 255, 255},
  66. {"green", Color{R: 0, G: 255, B: 0}, 85, 255, 255},
  67. {"blue", Color{R: 0, G: 0, B: 255}, 170, 255, 255},
  68. {"white", Color{R: 255, G: 255, B: 255}, 0, 0, 255},
  69. {"black", Color{R: 0, G: 0, B: 0}, 0, 0, 0},
  70. {"yellow", Color{R: 255, G: 255, B: 0}, 42, 255, 255},
  71. {"cyan", Color{R: 0, G: 255, B: 255}, 127, 255, 255},
  72. {"magenta", Color{R: 255, G: 0, B: 255}, 212, 255, 255},
  73. {"gray", Color{R: 128, G: 128, B: 128}, 0, 0, 128},
  74. {"dark red", Color{R: 128, G: 0, B: 0}, 0, 255, 128},
  75. {"light red", Color{R: 255, G: 128, B: 128}, 0, 127, 255},
  76. }
  77. for _, tc := range cases {
  78. t.Run(tc.name, func(t *testing.T) {
  79. h, s, v := tc.color.HSV()
  80. if h != tc.wantH {
  81. t.Errorf("HSV(%q).H = %d, want %d", tc.name, h, tc.wantH)
  82. }
  83. if s != tc.wantS {
  84. t.Errorf("HSV(%q).S = %d, want %d", tc.name, s, tc.wantS)
  85. }
  86. if v != tc.wantV {
  87. t.Errorf("HSV(%q).V = %d, want %d", tc.name, v, tc.wantV)
  88. }
  89. })
  90. }
  91. }