This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
90 lines (81 loc) · 1.73 KB
/
parse.go
File metadata and controls
90 lines (81 loc) · 1.73 KB
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
package color
import (
"regexp"
"strconv"
)
var reColor = regexp.MustCompile(`^color\(` +
`([a-zA-Z0-9-]+) ` +
`((?:[+-]?\d+|[+-]?\d*\.\d+(?:[eE][+-]?\d+)?)%?) ` +
`((?:[+-]?\d+|[+-]?\d*\.\d+(?:[eE][+-]?\d+)?)%?) ` +
`((?:[+-]?\d+|[+-]?\d*\.\d+(?:[eE][+-]?\d+)?)%?)` +
`(?: / ((?:[+-]?\d+|[+-]?\d*\.\d+(?:[eE][+-]?\d+)?)%?))?\);?$`)
// Parse parses colors in the CSS 'color()' format. The double dash for
// non-standard color spaces is optional.
func Parse(s string) (Color, bool) {
m := reColor.FindStringSubmatch(s)
if m == nil {
return Color{}, false
}
space := m[1]
x := m[2]
y := m[3]
z := m[4]
a := m[5]
if space == "xyz" {
space = "xyz-d65"
}
cs, ok := LookupSpace(space)
if !ok {
return Make(SRGB, 0, 0, 0, 1), false
}
var values [4]float64
parseValue := func(idx int, s string) bool {
if idx == 3 && len(s) == 0 {
values[3] = 1
return true
}
if s[len(s)-1] == '%' {
f, err := strconv.ParseFloat(s[:len(s)-1], 64)
if err != nil {
// Even inputs that pass the regex can get here, e.g. because of
// absurdly large values.
return false
}
if f < 0 {
f = 0
}
if f > 100 {
f = 100
}
f /= 100
if idx == 3 {
values[3] = f
} else {
rng := cs.Coords[idx].RefRange
values[idx] = lerp(rng[0], rng[1], f)
}
} else {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
// Even inputs that pass the regex can get here, e.g. because of
// absurdly large values.
return false
}
if idx == 3 {
if f < 0 {
f = 0
}
if f > 1 {
f = 1
}
}
values[idx] = f
}
return true
}
parseValue(0, x)
parseValue(1, y)
parseValue(2, z)
parseValue(3, a)
return Make(cs, values[0], values[1], values[2], values[3]), true
}