Debian Codegraph @

color/color_test.go

raw symbols SCIP
1 // Copyright 2025 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package color
16
17 import (
18 "fmt"
19 ic "image/color"
20 "testing"
21 )
22
23 // TestColorValues test variety of color edge cases.
24 func TestColorValues(t *testing.T) {
25 var values = []struct {
26 color Color
27 hex int32
28 }{
29 {Red, 0xFF0000},
30 {Green, 0x008000},
31 {Lime, 0x00FF00},
32 {Blue, 0x0000FF},
33 {Black, 0x000000},
34 {White, 0xFFFFFF},
35 {Silver, 0xC0C0C0},
36 {Navy, 0x000080},
37 {None, -1},
38 {Reset, -1},
39 {Color(300) | IsValid, -1}, // beyond the palette, marked valid
40 }
41
42 for _, tc := range values {
43 if tc.color.Hex() != tc.hex {
44 t.Errorf("Color: %x != %x", tc.color.Hex(), tc.hex)
45 }
46
47 if tc.color.TrueColor().Hex() != tc.hex {
48 t.Errorf("TrueColor %x != %x", tc.color.TrueColor().Hex(), tc.hex)
49 }
50 }
51 }
52
53 // TestColorFitting tests color matching.
54 func TestColorFitting(t *testing.T) {
55 var pal []Color
56 for i := range 255 {
57 pal = append(pal, PaletteColor(i))
58 }
59
60 // Exact color fitting on ANSI colors
61 for i := range 7 {
62 if Find(PaletteColor(i), pal[:8]) != PaletteColor(i) {
63 t.Errorf("Color ANSI fit fail at %d", i)
64 }
65 }
66 // Grey is closest to Silver
67 if Find(PaletteColor(8), pal[:8]) != PaletteColor(7) {
68 t.Errorf("Grey does not fit to silver")
69 }
70 // Color fitting of upper 8 colors.
71 for i := 9; i < 16; i++ {
72 if Find(PaletteColor(i), pal[:8]) != PaletteColor(i%8) {
73 t.Errorf("Color fit fail at %d", i)
74 }
75 }
76 // Imperfect fit
77 if Find(OrangeRed, pal[:16]) != Red ||
78 Find(AliceBlue, pal[:16]) != White ||
79 Find(Pink, pal) != XTerm217 ||
80 Find(Sienna, pal) != XTerm173 ||
81 Find(GetColor("#00FD00"), pal) != Lime {
82 t.Errorf("Imperfect color fit")
83 }
84
85 }
86
87 // TestColorNameLookup tests looking up colors by a string name.
88 func TestColorNameLookup(t *testing.T) {
89 var values = []struct {
90 name string
91 color Color
92 rgb bool
93 }{
94 {"#FF0000", Red, true},
95 {"black", Black, false},
96 {"orange", Orange, true},
97 {"door", Default, false},
98 }
99 for _, v := range values {
100 c := GetColor(v.name)
101 if c.Hex() != v.color.Hex() {
102 t.Errorf("Wrong color for %v: %v", v.name, c.Hex())
103 }
104 if v.rgb {
105 if !c.IsRGB() {
106 t.Errorf("Color should have RGB: %v", v.name)
107 }
108 } else {
109 if c.IsRGB() {
110 t.Errorf("Named color should not be RGB: %v", v.name)
111 }
112 }
113
114 if c.TrueColor().Hex() != v.color.Hex() {
115 t.Errorf("TrueColor did not match")
116 }
117 }
118
119 // these colors only have strings (for debugging), you cannot use them to create a color
120 if None.String() != "none" {
121 t.Errorf("ColorNone did not match")
122 }
123 if Reset.String() != "reset" {
124 t.Errorf("Reset did not match")
125 }
126 if Default.String() != "default" {
127 t.Errorf("Default did not match")
128 }
129 }
130
131 // TestColorRGB tests the Color.RGB API.
132 func TestColorRGB(t *testing.T) {
133 r, g, b := GetColor("#112233").RGB()
134 if r != 0x11 || g != 0x22 || b != 0x33 {
135 t.Errorf("RGB wrong (%x, %x, %x)", r, g, b)
136 }
137 r, g, b = None.RGB()
138 if r != -1 || g != -1 || b != -1 {
139 t.Errorf("None should not give valid RGB")
140 }
141 }
142
143 // TestFromImageColor tests converting from image.Color to Color.
144 func TestFromImageColor(t *testing.T) {
145 red := ic.RGBA{0xFF, 0x00, 0x00, 0x01}
146 white := ic.Gray{0xFF}
147 cyan := ic.CMYK{0xFF, 0x00, 0x00, 0x00}
148 transparent := ic.RGBA{0x01, 0x02, 0x03, 0x00}
149
150 if hex := FromImageColor(red).Hex(); hex != 0xFF0000 {
151 t.Errorf("%v is not 0xFF0000", hex)
152 }
153 if hex := FromImageColor(white).Hex(); hex != 0xFFFFFF {
154 t.Errorf("%v is not 0xFFFFFF", hex)
155 }
156 if hex := FromImageColor(cyan).Hex(); hex != 0x00FFFF {
157 t.Errorf("%v is not 0x00FFFF", hex)
158 }
159 if c := FromImageColor(transparent); c != Default {
160 t.Errorf("transparent should be default")
161 }
162 }
163
164 // TestColorRGBA tests the Color.RGBA API.
165 func TestColorRGBA(t *testing.T) {
166 r, g, b, a := Red.RGBA()
167 if r != 0xffff || g != 0 || b != 0 || a != 0xffff {
168 t.Errorf("Wrong RGBA for red: %x %x %x %x", r, g, b, a)
169 }
170 r, g, b, a = Red.TrueColor().RGBA()
171 if r != 0xffff || g != 0 || b != 0 || a != 0xffff {
172 t.Errorf("Wrong RGBA for red.TrueColor: %x %x %x %x", r, g, b, a)
173 }
174
175 r, g, b, a = Default.RGBA()
176 if r != 0 || g != 0 || b != 0 || a != 0 {
177 t.Errorf("Non-zero RGBA for default")
178 }
179
180 r, g, b, a = None.RGBA()
181 if r != 0 || g != 0 || b != 0 || a != 0 {
182 t.Errorf("Non-zero RGBA for none")
183 }
184 r, g, b, a = Reset.RGBA()
185 if r != 0 || g != 0 || b != 0 || a != 0 {
186 t.Errorf("Non-zero RGBA for reset")
187 }
188 }
189
190 // TestColorNames tests the color.Name() API.
191 func TestColorNames(t *testing.T) {
192 cases := []struct {
193 c Color
194 name string
195 css string
196 }{
197 {Red, "red", "#FF0000"},
198 {Pink, "pink", "#FFC0CB"},
199 {Black, "black", "#000000"},
200 {Black.TrueColor(), "", "#000000"},
201 {XTerm100, "", "#878700"},
202 {Color(1), "", ""}, // invalid color
203 }
204 for i, cs := range cases {
205 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
206 if cs.c.CSS() != cs.css {
207 t.Errorf("case %d: color css %q != %q", i, cs.c.CSS(), cs.css)
208 }
209 if cs.c.Name() != cs.name {
210 t.Errorf("case %d: color name %q != %q", i, cs.c.Name(), cs.name)
211 }
212 exp := cs.c.Name()
213 if exp == "" {
214 exp = cs.c.CSS()
215 }
216 if cs.c.Name(true) != exp { // test css
217 t.Errorf("case %d: color name(true) %q != %q", i, cs.c.Name(true), exp)
218 }
219 })
220 }
221 }
222
223 // TestColorString tests the color.String() API.
224 func TestColorString(t *testing.T) {
225 if s := Color(0).String(); s != "default" {
226 t.Errorf("zero color not default: %q", s)
227 }
228 if s := Color(10).String(); s != "" {
229 t.Errorf("invalid non-zero color did not yield empty string: %q", s)
230 }
231 if s := Red.String(); s != "red" {
232 t.Errorf("wrong string for red: %q", s)
233 }
234 }