-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
93 lines (72 loc) · 1.32 KB
/
parse.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
package hexcolor
// Parse parses a hex color code and returns the red, green, and blue components separatly as uint8 numbers.
func Parse(value string) (red uint8, green uint8, blue uint8, err error) {
if len(value) < 1 {
return 0, 0, 0, errNotHexColorCode(value)
}
var s string = value
{
s0 := s[0]
if '#' == s0 {
s = s[1:]
}
}
if 6 != len(s) {
return 0, 0, 0, errNotHexColorCode(value)
}
r1char, r0char := s[0], s[1]
g1char, g0char := s[2], s[3]
b1char, b0char := s[4], s[5]
var r1 byte
{
var ok bool
r1, ok = chartonum(r1char)
if !ok {
return 0, 0, 0, errNotHexColorCode(value)
}
}
var r0 byte
{
var ok bool
r0, ok = chartonum(r0char)
if !ok {
return 0, 0, 0, errNotHexColorCode(value)
}
}
var g1 byte
{
var ok bool
g1, ok = chartonum(g1char)
if !ok {
return 0, 0, 0, errNotHexColorCode(value)
}
}
var g0 byte
{
var ok bool
g0, ok = chartonum(g0char)
if !ok {
return 0, 0, 0, errNotHexColorCode(value)
}
}
var b1 byte
{
var ok bool
b1, ok = chartonum(b1char)
if !ok {
return 0, 0, 0, errNotHexColorCode(value)
}
}
var b0 byte
{
var ok bool
b0, ok = chartonum(b0char)
if !ok {
return 0, 0, 0, errNotHexColorCode(value)
}
}
red = (r1 << 4) | r0
green = (g1 << 4) | g0
blue = (b1 << 4) | b0
return red, green, blue, nil
}