-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathcolor_rgb_test.go
296 lines (235 loc) · 7.78 KB
/
color_rgb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package color
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
/*************************************************************
* test rgb color
*************************************************************/
func TestRGBColor(t *testing.T) {
buf := forceOpenColorRender()
defer resetColorRender()
is := assert.New(t)
// empty
c := RGBColor{3: 99}
is.True(c.IsEmpty())
is.Equal("", c.String())
// bg
c = RGB(204, 204, 204, true)
is.False(c.IsEmpty())
is.Equal("48;2;204;204;204", c.FullCode())
is.Equal("48;2;204;204;204", c.String())
is.Equal("38;2;204;204;204", c.ToFg().FullCode())
// fg
c = RGB(204, 204, 204)
is.False(c.IsEmpty())
is.Equal("38;2;204;204;204", c.FullCode())
is.Equal("38;2;204;204;204", c.String())
is.Equal("48;2;204;204;204", c.ToBg().FullCode())
// RGBColor.Sprint
str := c.Sprint("msg")
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m", str)
// RGBColor.Sprintf
str = c.Sprintf("msg")
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m", str)
is.Equal("cccccc", c.Hex())
is.Equal("204;204;204", c.Code())
is.Equal("38;2;204;204;204", c.FullCode())
is.Equal("[204 204 204]", fmt.Sprint(c.Values()))
// RGBColor.Print
c.Print("msg")
str = buf.String()
buf.Reset()
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m", str)
// RGBColor.Printf
c.Printf("m%s", "sg")
str = buf.String()
buf.Reset()
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m", str)
// RGBColor.Println
c.Println("msg")
str = buf.String()
buf.Reset()
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m\n", str)
}
func TestRGBColor_set_reset(t *testing.T) {
_, err := Reset()
assert.NoError(t, err)
c := RGBFromHEX("6256CC")
assert.NoError(t, c.Set())
fmt.Println("a message on True color set.")
assert.NoError(t, c.Reset())
fmt.Println()
}
func TestRGBFromString(t *testing.T) {
forceOpenColorRender()
defer resetColorRender()
is := assert.New(t)
c := RGBFromString("170,187,204")
is.Equal("\x1b[38;2;170;187;204mmsg\x1b[0m", c.Sprint("msg"))
c = RGBFromString("170,187,204", true)
is.Equal("\x1b[48;2;170;187;204mmsg\x1b[0m", c.Sprint("msg"))
c = RGBFromString("brown")
is.Equal("\x1b[38;2;165;42;42mmsg\x1b[0m", c.Sprint("msg"))
c = RGBFromString("170,187,")
is.Equal("msg", c.Sprint("msg"))
c = RGBFromString("")
is.Equal("msg", c.Sprint("msg"))
c = RGBFromString("170,187,error")
is.Equal("msg", c.Sprint("msg"))
c = RGBFromString("170,-187,-34")
is.Equal("msg", c.Sprint("msg"))
}
func TestHexToRGB(t *testing.T) {
is := assert.New(t)
c := HEX("ccc") // rgb: [204 204 204]
is.False(c.IsEmpty())
is.Equal("38;2;204;204;204", c.String())
is.Equal("cccccc", c.Hex())
c = HEX("aabbcc") // rgb: [170 187 204]
is.Equal("38;2;170;187;204", c.String())
is.Equal("aabbcc", c.Hex())
c = Hex("#aabbcc") // rgb: [170 187 204]
is.Equal("38;2;170;187;204", c.String())
c = HEX("0xad99c0") // rgb: [170 187 204]
is.Equal("38;2;173;153;192", c.String())
c = HEX(" ")
is.True(c.IsEmpty())
is.Equal("", c.String())
c = HEX("!#$bbcc")
is.Equal("", c.String())
c = HEX("#invalid")
is.Equal("", c.String())
c = HEX("invalid code")
is.Equal("", c.String())
}
func TestRGBStyle(t *testing.T) {
is := assert.New(t)
ForceColor()
fg := RGB(20, 144, 234)
bg := RGB(234, 78, 23)
s := &RGBStyle{}
is.True(s.IsEmpty())
is.Equal("", s.String())
s.Set(fg, bg)
is.False(s.IsEmpty())
is.Equal("38;2;20;144;234;48;2;234;78;23", s.String())
s = &RGBStyle{}
s.Set(fg, bg, OpUnderscore)
is.False(s.IsEmpty())
is.Equal("38;2;20;144;234;48;2;234;78;23;4", s.FullCode())
is.Equal("38;2;20;144;234;48;2;234;78;23;4", s.String())
s.SetOpts(Opts{OpBold, OpBlink})
is.Equal("38;2;20;144;234;48;2;234;78;23;1;5", s.Code())
is.Equal("38;2;20;144;234;48;2;234;78;23;1;5", s.String())
s.AddOpts(OpItalic)
is.Equal("38;2;20;144;234;48;2;234;78;23;1;5;3", s.String())
// NewRGBStyle
s = NewRGBStyle(RGB(20, 144, 234))
is.False(s.IsEmpty())
is.Equal("38;2;20;144;234", s.String())
s = NewRGBStyle(RGB(20, 144, 234), RGB(234, 78, 23))
is.False(s.IsEmpty())
is.Equal("38;2;20;144;234;48;2;234;78;23", s.String())
// HEXStyle
s = HEXStyle("555", "eee")
is.False(s.IsEmpty())
is.Equal("38;2;85;85;85;48;2;238;238;238", s.String())
// RGBStyleFromString
s = RGBStyleFromString("20, 144, 234", "234, 78, 23")
is.False(s.IsEmpty())
is.Equal("38;2;20;144;234;48;2;234;78;23", s.String())
// RGBColor.Sprint
is.Equal("\x1b[38;2;20;144;234;48;2;234;78;23mmsg\x1b[0m", s.Sprint("msg"))
// RGBColor.Sprintf
is.Equal("\x1b[38;2;20;144;234;48;2;234;78;23mmsg\x1b[0m", s.Sprintf("m%s", "sg"))
s.Println("hello, this is use RGB color")
fmt.Println("\x1b[38;2;20;144;234;48;2;234;78;23mTEXT\x1b[0m")
// add option: OpItalic
fmt.Println("\x1b[38;2;20;144;234;48;2;234;78;23;3mTEXT\x1b[0m")
buf := forceOpenColorRender()
defer resetColorRender()
// RGBColor.Print
s.Print("msg")
str := buf.String()
buf.Reset()
is.Equal("\x1b[38;2;20;144;234;48;2;234;78;23mmsg\x1b[0m", str)
// RGBColor.Printf
s.Printf("m%s", "sg")
str = buf.String()
buf.Reset()
is.Equal("\x1b[38;2;20;144;234;48;2;234;78;23mmsg\x1b[0m", str)
// RGBColor.Println
s.Println("msg")
str = buf.String()
buf.Reset()
is.Equal("\x1b[38;2;20;144;234;48;2;234;78;23mmsg\x1b[0m\n", str)
}
func TestPrintRGBColor(t *testing.T) {
RGB(30, 144, 255).Println("message. use RGB number")
HEX("#1976D2").Println("blue-darken")
RGBStyleFromString("213,0,0").Println("red-accent. use RGB number")
// foreground: eee, background: D50000
HEXStyle("eee", "D50000").Println("deep-purple color")
}
func TestRGBStyle_SetOpts(t *testing.T) {
s := NewRGBStyle(RGB(234, 78, 23), RGB(20, 144, 234))
s.Println("rgb style message")
s.SetOpts(Opts{OpItalic, OpBold, OpUnderscore})
s.Println("RGB style message with options")
}
func testRgbToC256Color(t *testing.T, name string, c RGBColor, expected uint8) {
t.Log("RGB Color:", c.Sprint(name))
t.Log("256 Color:", c.C256().Sprint(name))
actual := c.C256().Value()
if actual != expected {
t.Errorf("%s not converted correctly: expected %v, actual %v", name, actual, expected)
}
}
func TestRgbToC256(t *testing.T) {
testRgbToC256Color(t, "white", RGB(255, 255, 255), 15)
testRgbToC256Color(t, "red", RGB(255, 0, 0), 9)
testRgbToC256Color(t, "yellow", RGB(255, 255, 0), 11)
testRgbToC256Color(t, "greenBg", RgbFromInt(0, 255, 0, true), 10)
testRgbToC256Color(t, "blueBg", RGB(0, 0, 255, true), 12)
testRgbToC256Color(t, "light blue", RgbFromInts([]int{57, 187, 226}), 74)
}
func TestRgbToC256Background(t *testing.T) {
white := Rgb(255, 255, 255)
whiteBg := Bit24(255, 255, 255, true)
whiteFg := RGB(255, 255, 255, false)
if white.C256().String() != whiteFg.C256().String() {
t.Error("standard color didn't match foreground color")
}
if white.C256().String() == whiteBg.C256().String() {
t.Error("standard color matched background color")
}
prefix := whiteBg.C256().String()[:3]
if prefix != "48;" {
t.Errorf("background color didn't have background prefix: %v", prefix)
}
}
func TestRGBColor_C16(t *testing.T) {
rgb := RGB(57, 187, 226)
assert.Equal(t, "36", rgb.C16().String())
assert.Equal(t, "36", rgb.Color().String())
rgb = RGB(57, 187, 226, true)
assert.Equal(t, "46", rgb.C16().String())
assert.Equal(t, "46", rgb.Color().String())
}
func TestHSL(t *testing.T) {
// red #ff0000 255, 0, 0
rgb := HSL(0, 1, 0.5)
rgb.Println("rgb color create by HSL; hex:", rgb.Hex(), "rgb:", rgb.RgbString())
rgb = Hsl(0, 1, 0.5)
rgb.Println("rgb color create by HSL; hex:", rgb.Hex(), "rgb:", rgb.RgbString())
rgb = HslInt(0, 100, 50)
rgb.Println("rgb color create by HSL int; hex:", rgb.Hex(), "rgb:", rgb.RgbString())
// maroon #800000 128,0,0 0,100%,25%
rgb = HslInt(0, 100, 25)
rgb.Println("rgb color create by HSL int; hex:", rgb.Hex(), "rgb:", rgb.RgbString())
// darkgray #a9a9a9 169,169,169 0,0%,66%
rgb = HslInt(0, 0, 66)
rgb.Println("rgb color create by HSL int; hex:", rgb.Hex(), "rgb:", rgb.RgbString())
}