-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
176 lines (145 loc) · 3.98 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
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
package color
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var patterns = []struct {
pattern *regexp.Regexp
parser func(string) (Format, error)
}{
{
// Matches six and three digit hex colors (#FFFFFF, #FFF)
pattern: regexp.MustCompile(`#([ABCDEFabcdef0-9]{6}|[ABCDEFabcdef0-9]{3})`),
parser: parseHex,
},
{
// Matches RGB colors with int channel values (rgb(255, 255, 255)) including
// RGBA strings with a float alpha-channel value (rgba(255, 255, 255, 0.5).
pattern: regexp.MustCompile(`(?:rgba?\()?(?:[0-9]{1,3}(?:, |,| )?){3}(?:[01]\.[0-9]+)?\)?`),
parser: parseRGBint,
},
{
// Matches RGB colors with float channel values (rgb(1.0, 1.0, 1.0)) including
// RGBA strings with a an alpha-channel value (rgba(1.0, 1.0, 1.0, 0.5).
pattern: regexp.MustCompile(`(?:rgba?\()?(?:(?:[01])\.[0-9]+(?:, |,| )?){3,4}\)?`),
parser: parseRGBfloat,
},
}
func Parse(s string) (Format, error) {
for _, p := range patterns {
match := p.pattern.FindString(s)
if match != "" {
c, err := p.parser(match)
if err != nil {
return nil, err
}
return c, nil
}
}
return nil, fmt.Errorf("unknown color format: %s", s)
}
func parseHex(s string) (Format, error) {
h := strings.TrimPrefix(s, "#")
if !validHex(h) {
return nil, fmt.Errorf("invalid hex value %q", h)
}
// Here it is safe to take the byte length of the string, since no characters that are valid hexadecimal values
// are bigger than 1B.
hl := len(h)
switch hl {
case 6:
case 3:
h = extendShortHex(h)
default:
return nil, fmt.Errorf("invalid hex format %q, hex colors should be either 3 or 6 characters long", h)
}
v, err := strconv.ParseInt(h, 16, 32)
if err != nil {
return nil, err
}
c := Color(v)
if c < CMin || c > CMax {
return nil, fmt.Errorf("invalid color value %q, values should be between %x and %x", h, CMin, CMax)
}
return HexColor(h), nil
}
func parseRGBint(s string) (Format, error) {
rgbValues, err := rgbValues(s)
if err != nil {
return nil, err
}
var c []uint8
for i := 0; i < 3; i++ {
v, err := strconv.Atoi(rgbValues[i])
if err != nil {
return nil, fmt.Errorf("could not decode RGB int channel value %q in color string: %s", rgbValues[i], s)
} else if v > 0xff {
return nil, fmt.Errorf("invalid RGB channel value %q in color string: %s", strconv.Itoa(v), s)
}
c = append(c, uint8(v))
}
if len(rgbValues) == 4 {
a, err := parseFloatChanVal(rgbValues[3])
if err != nil {
return nil, fmt.Errorf("could not decode RGBA alpha channel value: %v", err)
}
return RGBAInt{RGBInt{c[0], c[1], c[2]}, a}, nil
}
return RGBInt{c[0], c[1], c[2]}, nil
}
func parseRGBfloat(s string) (Format, error) {
rgbValues, err := rgbValues(s)
if err != nil {
return nil, err
}
var c []float32
for _, cv := range rgbValues {
v, err := parseFloatChanVal(cv)
if err != nil {
return nil, err
}
c = append(c, v)
}
if len(c) == 4 {
return RGBAFloat{RGBFloat{c[0], c[1], c[2]}, c[3]}, nil
}
return RGBFloat{c[0], c[1], c[2]}, nil
}
func rgbValues(s string) ([]string, error) {
original := copyString(s)
// Remove "rgb()"
if (strings.HasPrefix(s, "rgb(") || strings.HasPrefix(s, "rgba(")) && strings.HasSuffix(s, ")") {
s = strings.TrimLeft(s, "rgba(")
s = strings.TrimRight(s, ")")
}
var channels []string
if strings.Index(s, ",") != -1 {
s = strings.Replace(s, " ", "", -1)
channels = strings.Split(s, ",")
} else {
channels = strings.Split(s, " ")
}
if len(channels) < 3 || len(channels) > 4 {
return nil, fmt.Errorf("invalid RGB color string: %s", original)
}
return channels, nil
}
func validHex(hex string) bool {
invalidChar := strings.IndexFunc(hex, func(r rune) bool {
return !('0' <= r && r <= 'f')
})
valid := invalidChar == -1
return valid
}
func parseFloatChanVal(s string) (float32, error) {
v, err := strconv.ParseFloat(s, 64)
if err != nil || v > 1.0 {
return 0.0, fmt.Errorf("invalid channel value %q", FormatRGBChannelFloat(v))
}
return float32(v), nil
}
func copyString(a string) string {
return (a + " ")[:len(a)]
}