| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 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 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)
- }
- })
- }
- }
|