-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcolor.go
274 lines (245 loc) · 5.95 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package config
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/morikuni/aec"
)
// Color is the color in the ANSI for configuration
type Color struct {
Type ColorType
Value8 uint8
ValueR uint8
ValueG uint8
ValueB uint8
Name ColorName
}
// MarshalYAML implements Marshaler
func (c Color) MarshalYAML() (interface{}, error) {
switch c.Type {
case ColorTypeNone:
return "", nil
case ColorTypeName:
return c.Name.String(), nil
case ColorType8Bit:
return c.Value8, nil
case ColorType24Bit:
return fmt.Sprintf(`#%x%x%x`, c.ValueR, c.ValueG, c.ValueB), nil
}
return nil, fmt.Errorf("invalid color type %s", c.Type)
}
// MarshalJSON implements Marshaler
func (c Color) MarshalJSON() ([]byte, error) {
switch c.Type {
case ColorTypeNone:
return []byte(`""`), nil
case ColorTypeName:
return []byte(fmt.Sprintf(`"%s"`, c.Name.String())), nil
case ColorType8Bit:
return []byte(fmt.Sprintf("%d", c.Value8)), nil
case ColorType24Bit:
return []byte(fmt.Sprintf(`"#%x%x%x"`, c.ValueR, c.ValueG, c.ValueB)), nil
}
return nil, fmt.Errorf("invalid color type %s", c.Type)
}
// UnmarshalYAML implements Unmarshaler
func (c *Color) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
return c.unmarshal(s, false)
}
var errInvalidFormat = errors.New("invalid format")
var reg8Bit = regexp.MustCompile(`(?mi)^(\d{1,3})$`)
func (c *Color) unmarshalAs8Bit(str string) error {
match := reg8Bit.FindStringSubmatch(str)
if len(match) != 2 {
return errInvalidFormat
}
v, err := atoi(match[1])
if err != nil {
return errInvalidFormat
}
c.Type = ColorType8Bit
c.Value8 = v
return nil
}
var reg8BitHex = regexp.MustCompile(`(?mi)^(0[xX][[:xdigit:]]{1,2})$`)
func (c *Color) unmarshalAs8BitHex(str string) error {
match := reg8BitHex.FindStringSubmatch(str)
if len(match) != 2 {
return errInvalidFormat
}
v, _ := atoi(match[1])
c.Type = ColorType8Bit
c.Value8 = v
return nil
}
var regRGB = regexp.MustCompile(`(?mi)^#([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})$`)
func (c *Color) unmarshalAs24BitRGB(str string) error {
match := regRGB.FindStringSubmatch(str)
if len(match) != 4 {
return errInvalidFormat
}
r, _ := strconv.ParseUint(match[1], 16, 8)
g, _ := strconv.ParseUint(match[2], 16, 8)
b, _ := strconv.ParseUint(match[3], 16, 8)
c.Type = ColorType24Bit
c.ValueR = uint8(r)
c.ValueG = uint8(g)
c.ValueB = uint8(b)
return nil
}
var regRGBFunc = regexp.MustCompile(`(?mi)^rgb\((0x[[:xdigit:]]{2}|\d{1,3}), *(0x[[:xdigit:]]{2}|\d{1,3}), *(0x[[:xdigit:]]{2}|\d{1,3})\)$`)
func (c *Color) unmarshalAsRGBFunc(str string) error {
match := regRGBFunc.FindStringSubmatch(str)
if len(match) != 4 {
return errInvalidFormat
}
r, err := atoi(match[1])
if err != nil {
return errInvalidFormat
}
g, err := atoi(match[2])
if err != nil {
return errInvalidFormat
}
b, err := atoi(match[3])
if err != nil {
return errInvalidFormat
}
c.Type = ColorType24Bit
c.ValueR = r
c.ValueG = g
c.ValueB = b
return nil
}
// UnmarshalJSON implements Unmarshaler
func (c *Color) UnmarshalJSON(raw []byte) error {
return c.unmarshal(string(raw), true)
}
func (c *Color) unmarshal(str string, unquote bool) error {
if unquote {
unquoted, err := strconv.Unquote(str)
if err == nil {
str = unquoted
}
}
if str == "" {
c.Type = ColorTypeNone
return nil
}
if err := c.unmarshalAs8Bit(str); err == nil {
return nil
}
if err := c.unmarshalAs8BitHex(str); err == nil {
return nil
}
if err := c.unmarshalAs24BitRGB(str); err == nil {
return nil
}
if err := c.unmarshalAsRGBFunc(str); err == nil {
return nil
}
for _, cn := range ColorNames() {
if cn.String() == str {
c.Type = ColorTypeName
c.Name = cn
return nil
}
}
return errInvalidFormat
}
func atoi(s string) (uint8, error) {
i, err := atoiCore(s)
if err != nil {
return 0, err
}
return uint8(i), nil
}
func atoiCore(s string) (uint64, error) {
if strings.HasPrefix(s, "0x") {
return strconv.ParseUint(strings.TrimPrefix(s, "0x"), 16, 8)
}
if strings.HasPrefix(s, "0X") {
return strconv.ParseUint(strings.TrimPrefix(s, "0X"), 16, 8)
}
return strconv.ParseUint(s, 10, 8)
}
var backColors = map[ColorName]aec.ANSI{
Black: aec.BlackB,
Red: aec.RedB,
Green: aec.GreenB,
Yellow: aec.YellowB,
Blue: aec.BlueB,
Magenta: aec.MagentaB,
Cyan: aec.CyanB,
White: aec.WhiteB,
LightBlack: aec.LightBlackB,
LightRed: aec.LightRedB,
LightGreen: aec.LightGreenB,
LightYellow: aec.LightYellowB,
LightBlue: aec.LightBlueB,
LightMagenta: aec.LightMagentaB,
LightCyan: aec.LightCyanB,
LightWhite: aec.LightWhiteB,
}
var emptyColor aec.ANSI
func init() {
emptyColor = aec.EmptyBuilder.ANSI
}
// B gets background ANSI color
func (c *Color) B() aec.ANSI {
switch c.Type {
case ColorTypeNone:
return emptyColor
case ColorTypeName:
b, ok := backColors[c.Name]
if ok {
return b
}
case ColorType8Bit:
return aec.Color8BitB(aec.RGB8Bit(c.Value8))
case ColorType24Bit:
return aec.FullColorB(c.ValueR, c.ValueG, c.ValueB)
}
return emptyColor
}
var frontColors = map[ColorName]aec.ANSI{
Black: aec.BlackF,
Red: aec.RedF,
Green: aec.GreenF,
Yellow: aec.YellowF,
Blue: aec.BlueF,
Magenta: aec.MagentaF,
Cyan: aec.CyanF,
White: aec.WhiteF,
LightBlack: aec.LightBlackF,
LightRed: aec.LightRedF,
LightGreen: aec.LightGreenF,
LightYellow: aec.LightYellowF,
LightBlue: aec.LightBlueF,
LightMagenta: aec.LightMagentaF,
LightCyan: aec.LightCyanF,
LightWhite: aec.LightWhiteF,
}
// F gets foreground ANSI color
func (c *Color) F() aec.ANSI {
switch c.Type {
case ColorTypeNone:
return emptyColor
case ColorTypeName:
f, ok := frontColors[c.Name]
if ok {
return f
}
case ColorType8Bit:
return aec.Color8BitF(aec.RGB8Bit(c.Value8))
case ColorType24Bit:
return aec.FullColorF(c.ValueR, c.ValueG, c.ValueB)
}
return emptyColor
}