-
Notifications
You must be signed in to change notification settings - Fork 6
/
color.go
112 lines (99 loc) · 1.83 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
package main
import (
"log"
"strconv"
)
type Color128 struct {
R, G, B, A uint32
}
func (c Color128) RGBA() (r, g, b, a uint32) {
return c.R, c.G, c.B, c.A
}
func NewColor128(r, g, b, a uint32) Color128 {
return Color128{
R: r,
G: g,
B: b,
A: a,
}
}
func ParseColor(text string) Color128 {
if text == "transparent" {
return Color128{
R: 0,
G: 0,
B: 0,
A: 0,
}
}
var err error
r, g, b, a := uint64(0), uint64(0), uint64(0), uint64(0xffffffff)
switch len(text) {
case 4:
a, err = strconv.ParseUint(text[3:4], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
a |= a << 4
a |= a << 8
a |= a << 16
fallthrough
case 3:
r, err = strconv.ParseUint(text[0:1], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
r |= r << 4
r |= r << 8
r |= r << 16
g, err = strconv.ParseUint(text[1:2], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
g |= g << 4
g |= g << 8
g |= g << 16
b, err = strconv.ParseUint(text[2:3], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
b |= b << 4
b |= b << 8
b |= b << 16
case 8:
a, err = strconv.ParseUint(text[6:8], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
a |= a << 8
a |= a << 16
fallthrough
case 6:
r, err = strconv.ParseUint(text[0:2], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
r |= r << 8
r |= r << 16
g, err = strconv.ParseUint(text[2:4], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
g |= g << 8
g |= g << 16
b, err = strconv.ParseUint(text[4:6], 16, 8)
if err != nil {
log.Fatalf("invalid color %q", text)
}
b |= b << 8
b |= b << 16
default:
log.Fatalf("invalid color %q", text)
}
return Color128{
R: uint32(r),
G: uint32(g),
B: uint32(b),
A: uint32(a),
}
}