-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
85 lines (70 loc) · 1.63 KB
/
utils.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
package lights
import (
"encoding/json"
"fmt"
"io/ioutil"
"gopkg.in/go-playground/colors.v1"
)
type InvalidHexValue struct {
value string
msg string
}
func (err *InvalidHexValue) Error() string {
return fmt.Sprintf("%s : %s", err.msg, err.value)
}
func reverseMap(m map[string]string) map[string]string {
n := make(map[string]string)
for k, v := range m {
n[v] = k
}
return n
}
func validHexPrefix(hex string) bool {
return hex[0] == '#'
}
func HexToColor(hex string) (Color, error) {
var color Color
if !validHexPrefix(hex) {
hex = "#" + hex
} else {
return color, &InvalidHexValue{hex, "Hex Value is invalid"}
}
hexColor, _ := colors.ParseHEX(hex)
rgba := hexColor.ToRGBA()
color, err := NewColor(
rgba.R,
rgba.G,
rgba.B,
uint8(rgba.A*255),
)
if err != nil {
return color, &InvalidHexValue{hex, "Hex Value is invalid"}
}
return color, nil
}
func lookupColorByName(color string) (*Color, *InvalidColorError) {
// TODO: refactor file read to allow it to be mocked easily
content, err := ioutil.ReadFile("colors.min.json")
if err != nil {
fmt.Printf("Error while reading a file %v", err)
}
var hexes map[string]string
_ = json.Unmarshal(content, &hexes)
names := reverseMap(hexes)
hex, ok := names[string(color)]
if !ok {
return nil, &InvalidColorError{color, "Color is invalid"}
}
h, err := HexToColor(hex)
if err != nil {
return nil, &InvalidColorError{color, "Color is invalid"}
}
return &h, nil
}
func lookupPrimaryColor(color string) (*Color, *InvalidColorError) {
c, ok := ColorPresets[color]
if !ok {
return nil, &InvalidColorError{color, "Color is invalid"}
}
return &c, nil
}