-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolour.go
163 lines (134 loc) · 3.15 KB
/
colour.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
package revisor
import (
"encoding/hex"
"errors"
"fmt"
"slices"
"strconv"
"strings"
)
type ColourFormat string
const (
ColourUnknown ColourFormat = ""
ColourHex ColourFormat = "hex"
ColourRGB ColourFormat = "rgb"
ColourRGBA ColourFormat = "rgba"
)
type cFormatSpec struct {
Format ColourFormat
Prefix string
Validate func(spec cFormatSpec, code string) error
}
var (
defaultColourFormats = []ColourFormat{ColourRGB, ColourRGBA}
colourComponents = []string{"r", "g", "b", "alpha"}
colourFormats = []cFormatSpec{
{
Format: ColourHex,
Prefix: "#",
Validate: parseHex,
},
{
Format: ColourRGBA,
Prefix: "rgba",
Validate: parseRGBA,
},
{
Format: ColourRGB,
Prefix: "rgb",
Validate: parseRGBA,
},
}
)
func validateColour(value string, formats []ColourFormat) error {
var (
spec cFormatSpec
code string
)
for _, s := range colourFormats {
after, ok := strings.CutPrefix(value, s.Prefix)
if !ok {
continue
}
spec = s
code = after
break
}
if len(formats) == 0 {
formats = defaultColourFormats
}
if spec.Format == ColourUnknown || !slices.Contains(formats, spec.Format) {
if len(formats) == 1 {
return fmt.Errorf("expected a colour in the format %q",
formats[0])
}
return fmt.Errorf("expected a colour in one of the formats %s",
quotedSlice(formats))
}
return spec.Validate(spec, code)
}
const hexColourLength = 6
func parseHex(_ cFormatSpec, code string) error {
if len(code) != hexColourLength {
return fmt.Errorf("code length: expected %d characters, got %d",
hexColourLength, len(code))
}
_, err := hex.DecodeString(code)
if err != nil {
return fmt.Errorf("invalid hex code: %w", err)
}
return nil
}
func parseRGBA(spec cFormatSpec, code string) error {
rest, ok := strings.CutPrefix(code, "(")
if !ok {
return errors.New("missing starting '('")
}
rest, ok = strings.CutSuffix(rest, ")")
if !ok {
return errors.New("missing closing ')'")
}
numberStrings := strings.Split(rest, ",")
components := len(numberStrings)
//nolint: exhaustive
switch spec.Format {
case ColourRGB:
if components != 3 {
return fmt.Errorf("expected three components in a rgb() value, got %d", components)
}
case ColourRGBA:
if components != 4 {
return fmt.Errorf("expected four components in a rgba() value, got %d", components)
}
n, err := strconv.ParseFloat(strings.TrimSpace(numberStrings[3]), 64)
if err != nil {
return fmt.Errorf("invalid alpha value: %w", err)
}
if n < 0 || n > 1 {
return fmt.Errorf("%q out of range", colourComponents[3])
}
default:
return fmt.Errorf(
"configuration error: cannot parse %q with parseRGBA()",
spec.Format,
)
}
for i, ns := range numberStrings[:3] {
n, err := strconv.Atoi(strings.TrimSpace(ns))
if err != nil {
return fmt.Errorf("invalid %q value: %w",
colourComponents[i], err)
}
if n < 0 || n > 255 {
return fmt.Errorf("%q out of range", colourComponents[i])
}
}
return nil
}
func quotedSlice[T any](s []T) string {
ss := make([]string, len(s))
for i, v := range s {
ss[i] = strconv.Quote(fmt.Sprintf("%v", v))
}
return strings.Join(ss, ", ")
}