/
rgb_test.go
202 lines (184 loc) 路 4.21 KB
/
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
package colorcode_test
import (
"testing"
"github.com/YashdalfTheGray/colorcode"
)
func TestNewRGB(t *testing.T) {
testCases := []struct {
desc string
r, g, b uint8
out colorcode.RGB
}{
{
desc: "builds a color out of rgb values",
r: 10,
g: 45,
b: 234,
out: colorcode.RGB{10, 45, 234},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
if c := colorcode.NewRGB(tC.r, tC.g, tC.b); c.String() != tC.out.String() {
t.Errorf("Expected %s but got %s", tC.out.String(), c.String())
}
})
}
}
func TestRGBString(t *testing.T) {
testCases := []struct {
desc string
in colorcode.RGB
out string
}{
{
desc: "builds a string out of an RGB color",
in: colorcode.RGB{200, 90, 50},
out: "rgb(200, 90, 50)",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
if tC.in.String() != tC.out {
t.Errorf("Expected %s but got %s", tC.out, tC.in.String())
}
})
}
}
func TestRGBToHexCode(t *testing.T) {
testCases := []struct {
desc string
in colorcode.RGB
out string
}{
{
desc: "converts an RGB color to hex string",
in: colorcode.RGB{0, 0, 0},
out: "#000000",
},
{
desc: "converts another RGB color to hex string",
in: colorcode.RGB{68, 138, 255},
out: "#448aff",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
if result := tC.in.ToHexCode(); result.String() != tC.out {
t.Errorf("Expected %s but got %s", tC.out, result)
}
})
}
}
func TestRGBToHSL(t *testing.T) {
testCases := []struct {
desc string
in colorcode.RGB
out string
}{
{
desc: "converts a non-gray RGB color to HSL",
in: colorcode.RGB{13, 166, 242},
out: "hsl(200, 90%, 50%)",
},
{
desc: "converts a gray RGB color to HSL",
in: colorcode.RGB{128, 128, 128},
out: "hsl(0, 0%, 50%)",
},
{
desc: "converts a dark RGB color to HSL",
in: colorcode.RGB{40, 34, 90},
out: "hsl(246, 45%, 24%)",
},
{
desc: "converts a mostly green RGB color to HSL",
in: colorcode.RGB{40, 150, 90},
out: "hsl(147, 58%, 37%)",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
if result := tC.in.ToHSL(); result.String() != tC.out {
t.Errorf("Expected %s but got %s", tC.out, result.String())
}
})
}
}
func TestRGBToHSV(t *testing.T) {
testCases := []struct {
desc string
in colorcode.RGB
out string
}{
{
desc: "converts a non-gray RGB color to HSV",
in: colorcode.RGB{13, 166, 242},
out: "hsv(200, 95%, 95%)",
},
{
desc: "converts a gray RGB color to HSV",
in: colorcode.RGB{128, 128, 128},
out: "hsv(0, 0%, 50%)",
},
{
desc: "converts black color to HSV",
in: colorcode.RGB{0, 0, 0},
out: "hsv(0, 0%, 0%)",
},
{
desc: "converts a dark RGB color to HSV",
in: colorcode.RGB{40, 34, 90},
out: "hsv(246, 62%, 35%)",
},
{
desc: "converts a mostly green RGB color to HSV",
in: colorcode.RGB{40, 150, 90},
out: "hsv(147, 73%, 59%)",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
if result := tC.in.ToHSV(); result.String() != tC.out {
t.Errorf("Expected %s but got %s", tC.out, result.String())
}
})
}
}
func benchmarkRGBToHSL(be *testing.B, r, g, b uint8) {
for i := 0; i < be.N; i++ {
colorcode.RGB{r, g, b}.ToHSL()
}
}
func BenchmarkRGBToHSL_NonGray(b *testing.B) {
benchmarkRGBToHSL(b, 13, 166, 242)
}
func BenchmarkRGBToHSL_Gray(b *testing.B) {
benchmarkRGBToHSL(b, 128, 128, 128)
}
func BenchmarkRGBToHSL_Dark(b *testing.B) {
benchmarkRGBToHSL(b, 40, 34, 90)
}
func BenchmarkRGBToHSL_MostlyGreen(b *testing.B) {
benchmarkRGBToHSL(b, 40, 150, 90)
}
func benchmarkRGBToHSV(be *testing.B, r, g, b uint8) {
for i := 0; i < be.N; i++ {
colorcode.RGB{r, g, b}.ToHSV()
}
}
func BenchmarkRGBToHSV_NonGray(b *testing.B) {
benchmarkRGBToHSV(b, 13, 166, 242)
}
func BenchmarkRGBToHSV_Gray(b *testing.B) {
benchmarkRGBToHSV(b, 128, 128, 128)
}
func BenchmarkRGBToHSV_Black(b *testing.B) {
benchmarkRGBToHSV(b, 0, 0, 0)
}
func BenchmarkRGBToHSV_Dark(b *testing.B) {
benchmarkRGBToHSV(b, 40, 34, 90)
}
func BenchmarkRGBToHSV_MostlyGreen(b *testing.B) {
benchmarkRGBToHSV(b, 40, 150, 90)
}