| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package rgb
- import (
- "testing"
- )
- func TestQMKValueIDs(t *testing.T) {
- if Brightness != 1 {
- t.Errorf("Brightness = %d, want 1", Brightness)
- }
- if EffectID != 2 {
- t.Errorf("EffectID = %d, want 2", EffectID)
- }
- if Speed != 3 {
- t.Errorf("Speed = %d, want 3", Speed)
- }
- if ColorValue != 4 {
- t.Errorf("ColorValue = %d, want 4", ColorValue)
- }
- }
- func TestParseEffect(t *testing.T) {
- cases := []struct {
- name string
- input string
- want Effect
- err bool
- }{
- {"static", "static", Static, false},
- {"breathing", "breathing", Breathing, false},
- {"rainbow", "rainbow", Rainbow, false},
- {"rainbow_wave", "rainbow_wave", RainbowWave, false},
- {"snake", "snake", Snake, false},
- {"gradient", "gradient", Gradient, false},
- {"tail", "tail", Tail, false},
- {"unknown", "flubber", 0, true},
- {"empty", "", 0, true},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- got, err := ParseEffect(tc.input)
- if tc.err {
- if err == nil {
- t.Errorf("ParseEffect(%q) expected error, got nil", tc.input)
- }
- return
- }
- if err != nil {
- t.Errorf("ParseEffect(%q) unexpected error: %v", tc.input, err)
- return
- }
- if got != tc.want {
- t.Errorf("ParseEffect(%q) = %d, want %d", tc.input, got, tc.want)
- }
- })
- }
- }
- func TestEffectString(t *testing.T) {
- cases := []struct {
- effect Effect
- want string
- }{
- {Static, "static"},
- {Breathing, "breathing"},
- {Rainbow, "rainbow"},
- {RainbowWave, "rainbow_wave"},
- {Effect(99), "unknown"},
- }
- for _, tc := range cases {
- t.Run(tc.want, func(t *testing.T) {
- got := tc.effect.String()
- if got != tc.want {
- t.Errorf("Effect(%d).String() = %q, want %q", tc.effect, got, tc.want)
- }
- })
- }
- }
- func TestParseHexColor(t *testing.T) {
- cases := []struct {
- name string
- input string
- want Color
- err bool
- }{
- {"red", "ff0000", Color{R: 255, G: 0, B: 0}, false},
- {"green", "00ff00", Color{R: 0, G: 255, B: 0}, false},
- {"blue", "0000ff", Color{R: 0, G: 0, B: 255}, false},
- {"white", "ffffff", Color{R: 255, G: 255, B: 255}, false},
- {"black", "000000", Color{R: 0, G: 0, B: 0}, false},
- {"lowercase", "aabbcc", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
- {"uppercase", "AABBCC", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
- {"short", "ff000", Color{0, 0, 0}, true},
- {"long", "ff00000", Color{0, 0, 0}, true},
- {"empty", "", Color{0, 0, 0}, true},
- {"invalid", "gggggg", Color{}, true},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- got, err := ParseHexColor(tc.input)
- if tc.err {
- if err == nil {
- t.Errorf("ParseHexColor(%q) expected error, got nil", tc.input)
- }
- return
- }
- if err != nil {
- t.Errorf("ParseHexColor(%q) unexpected error: %v", tc.input, err)
- return
- }
- if got != tc.want {
- t.Errorf("ParseHexColor(%q) = %+v, want %+v", tc.input, got, tc.want)
- }
- })
- }
- }
- func TestColorToHSV(t *testing.T) {
- cases := []struct {
- name string
- color Color
- wantH uint8
- wantS uint8
- wantV uint8
- }{
- {"red", Color{R: 255, G: 0, B: 0}, 0, 255, 255},
- {"green", Color{R: 0, G: 255, B: 0}, 85, 255, 255},
- {"blue", Color{R: 0, G: 0, B: 255}, 170, 255, 255},
- {"white", Color{R: 255, G: 255, B: 255}, 0, 0, 255},
- {"black", Color{R: 0, G: 0, B: 0}, 0, 0, 0},
- {"yellow", Color{R: 255, G: 255, B: 0}, 42, 255, 255},
- {"cyan", Color{R: 0, G: 255, B: 255}, 127, 255, 255},
- {"magenta", Color{R: 255, G: 0, B: 255}, 212, 255, 255},
- {"gray", Color{R: 128, G: 128, B: 128}, 0, 0, 128},
- {"dark red", Color{R: 128, G: 0, B: 0}, 0, 255, 128},
- {"light red", Color{R: 255, G: 128, B: 128}, 0, 127, 255},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- h, s, v := tc.color.HSV()
- if h != tc.wantH {
- t.Errorf("HSV(%q).H = %d, want %d", tc.name, h, tc.wantH)
- }
- if s != tc.wantS {
- t.Errorf("HSV(%q).S = %d, want %d", tc.name, s, tc.wantS)
- }
- if v != tc.wantV {
- t.Errorf("HSV(%q).V = %d, want %d", tc.name, v, tc.wantV)
- }
- })
- }
- }
|