-
Notifications
You must be signed in to change notification settings - Fork 8
/
color.go
53 lines (43 loc) · 1009 Bytes
/
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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var colorRegex = regexp.MustCompile(`^#?[0-9a-fA-F]{6}$`)
// color is a small utility type which parses colors in html format and drops
// them into a type which we can use for some basic conversions. It also adds
// UnmarshalYAML so it can be parsed directly by the yaml parser.
type color struct {
R, G, B int
}
// UnmarshalYAML implements yaml.Unmarshaler
func (c *color) UnmarshalYAML(f func(interface{}) error) error {
var in string
var tmp int64
err := f(&in)
if err != nil {
return err
}
if !colorRegex.MatchString(in) {
return fmt.Errorf("Color %q is not formatted correctly", in)
}
in = strings.TrimPrefix(in, "#")
tmp, err = strconv.ParseInt(in[0:2], 16, 32)
if err != nil {
return err
}
c.R = int(tmp)
tmp, err = strconv.ParseInt(in[2:4], 16, 32)
if err != nil {
return err
}
c.G = int(tmp)
tmp, err = strconv.ParseInt(in[4:6], 16, 32)
if err != nil {
return err
}
c.B = int(tmp)
return err
}