-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcolor.go
241 lines (235 loc) · 5.38 KB
/
color.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
package canvas
import (
"fmt"
"image/color"
"math"
"strconv"
"strings"
)
func parseHexRune(rn rune) (int, bool) {
switch {
case rn >= '0' && rn <= '9':
return int(rn - '0'), true
case rn >= 'a' && rn <= 'f':
return int(rn-'a') + 10, true
case rn >= 'A' && rn <= 'F':
return int(rn-'A') + 10, true
}
return 0, false
}
func parseHexRunePair(rn1, rn2 rune) (int, bool) {
i1, ok := parseHexRune(rn1)
if !ok {
return 0, false
}
i2, ok := parseHexRune(rn2)
if !ok {
return 0, false
}
return i1*16 + i2, true
}
func parseColorComponent(value interface{}) (uint8, bool) {
switch v := value.(type) {
case float32:
return uint8(math.Floor(float64(v) * 255)), true
case float64:
return uint8(math.Floor(v * 255)), true
case int:
return uint8(v), true
case uint:
return uint8(v), true
case uint8:
return v, true
case string:
if len(v) == 0 {
return 0, false
}
if v[0] == '#' {
str := v[1:]
if len(str) > 2 {
return 0, false
}
conv, err := strconv.ParseUint(v[1:], 16, 8)
if err != nil {
return 0, false
}
return uint8(conv), true
} else if strings.ContainsRune(v, '.') {
conv, err := strconv.ParseFloat(v, 32)
if err != nil {
return 0, false
}
if conv < 0 {
conv = 0
} else if conv > 1 {
conv = 1
}
return uint8(math.Round(conv * 255.0)), true
} else {
conv, err := strconv.ParseUint(v, 10, 8)
if err != nil {
return 0, false
}
return uint8(conv), true
}
}
return 0, false
}
func parseColor(value ...interface{}) (c color.RGBA, ok bool) {
if len(value) == 1 {
switch v := value[0].(type) {
case color.Color:
r, g, b, a := v.RGBA()
c = color.RGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(b >> 8), A: uint8(a >> 8)}
ok = true
return
case [3]float32:
return color.RGBA{
R: uint8(math.Floor(float64(v[0] * 255))),
G: uint8(math.Floor(float64(v[1] * 255))),
B: uint8(math.Floor(float64(v[2] * 255))),
A: 255}, true
case [4]float32:
return color.RGBA{
R: uint8(math.Floor(float64(v[0] * 255))),
G: uint8(math.Floor(float64(v[1] * 255))),
B: uint8(math.Floor(float64(v[2] * 255))),
A: uint8(math.Floor(float64(v[3] * 255)))}, true
case [3]float64:
return color.RGBA{
R: uint8(math.Floor(v[0] * 255)),
G: uint8(math.Floor(v[1] * 255)),
B: uint8(math.Floor(v[2] * 255)),
A: 255}, true
case [4]float64:
return color.RGBA{
R: uint8(math.Floor(v[0] * 255)),
G: uint8(math.Floor(v[1] * 255)),
B: uint8(math.Floor(v[2] * 255)),
A: uint8(math.Floor(v[3] * 255))}, true
case [3]int:
return color.RGBA{
R: uint8(v[0]),
G: uint8(v[1]),
B: uint8(v[2]),
A: 255}, true
case [4]int:
return color.RGBA{
R: uint8(v[0]),
G: uint8(v[1]),
B: uint8(v[2]),
A: uint8(v[3])}, true
case [3]uint:
return color.RGBA{
R: uint8(v[0]),
G: uint8(v[1]),
B: uint8(v[2]),
A: 255}, true
case [4]uint:
return color.RGBA{
R: uint8(v[0]),
G: uint8(v[1]),
B: uint8(v[2]),
A: uint8(v[3])}, true
case [3]uint8:
return color.RGBA{R: v[0], G: v[1], B: v[2], A: 255}, true
case [4]uint8:
return color.RGBA{R: v[0], G: v[1], B: v[2], A: v[3]}, true
case string:
if len(v) == 0 {
return
}
if v[0] == '#' {
str := v[1:]
if len(str) == 3 || len(str) == 4 {
var ir, ig, ib int
ia := 255
ir, ok = parseHexRune(rune(str[0]))
if !ok {
return
}
ir = ir*16 + ir
ig, ok = parseHexRune(rune(str[1]))
if !ok {
return
}
ig = ig*16 + ig
ib, ok = parseHexRune(rune(str[2]))
if !ok {
return
}
ib = ib*16 + ib
if len(str) == 4 {
ia, ok = parseHexRune(rune(str[3]))
if !ok {
return
}
ia = ia*16 + ia
}
return color.RGBA{R: uint8(ir), G: uint8(ig), B: uint8(ib), A: uint8(ia)}, true
} else if len(str) == 6 || len(str) == 8 {
var ir, ig, ib int
ia := 255
ir, ok = parseHexRunePair(rune(str[0]), rune(str[1]))
if !ok {
return
}
ig, ok = parseHexRunePair(rune(str[2]), rune(str[3]))
if !ok {
return
}
ib, ok = parseHexRunePair(rune(str[4]), rune(str[5]))
if !ok {
return
}
if len(str) == 8 {
ia, ok = parseHexRunePair(rune(str[6]), rune(str[7]))
if !ok {
return
}
}
return color.RGBA{R: uint8(ir), G: uint8(ig), B: uint8(ib), A: uint8(ia)}, true
} else {
return
}
} else {
v = strings.Replace(v, " ", "", -1)
var ir, ig, ib int
n, err := fmt.Sscanf(v, "rgb(%d,%d,%d)", &ir, &ig, &ib)
if err == nil && n == 3 {
return color.RGBA{R: uint8(ir), G: uint8(ig), B: uint8(ib), A: 255}, true
}
var fa float64
n, err = fmt.Sscanf(v, "rgba(%d,%d,%d,%f)", &ir, &ig, &ib, &fa)
fa = math.Max(0, math.Min(1, fa))
if err == nil && n == 4 {
return color.RGBA{R: uint8(ir), G: uint8(ig), B: uint8(ib), A: uint8(fa * 255)}, true
}
}
}
} else if len(value) == 3 || len(value) == 4 {
var pr, pg, pb, pa uint8
pr, ok = parseColorComponent(value[0])
if !ok {
return
}
pg, ok = parseColorComponent(value[1])
if !ok {
return
}
pb, ok = parseColorComponent(value[2])
if !ok {
return
}
if len(value) == 4 {
pa, ok = parseColorComponent(value[3])
if !ok {
return
}
} else {
pa = 255
}
return color.RGBA{R: pr, G: pg, B: pb, A: pa}, true
}
return color.RGBA{A: 255}, false
}