forked from ungerik/go-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
116 lines (104 loc) · 2.4 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
package model
import (
"encoding/hex"
"errors"
"image/color"
"strings"
)
func NewColor(value string) *Color {
c := new(Color)
c.SetString(value)
return c
}
/*
Color holds a hex web-color with the # prefix.
The Set method accepts all valid web color syntaxes
except for color names.
Struct tag attributes:
`model:"required"`
*/
type Color string
func (self *Color) String() string {
return string(*self)
}
func (self *Color) SetString(s string) {
s = strings.ToLower(s)
switch len(s) {
case 0, 9:
*self = Color(s)
case 3:
*self = Color([]byte{'#', s[0], s[0], s[1], s[1], s[2], s[2], 'f', 'f'})
case 4:
if s[0] == '#' {
*self = Color([]byte{'#', s[1], s[1], s[2], s[2], s[3], s[3], 'f', 'f'})
} else {
*self = Color([]byte{'#', s[0], s[0], s[1], s[1], s[2], s[2], s[3], s[3]})
}
case 5:
*self = Color([]byte{'#', s[1], s[1], s[2], s[2], s[3], s[3], s[4], s[4]})
case 6:
*self = Color("#" + s + "ff")
case 7:
*self = Color(s + "ff")
case 8:
*self = Color("#" + s)
default:
panic("Invalid hex web-color length")
}
if self.IsValid() == false {
panic("Invalid hex web-color")
}
}
func (self *Color) IsEmpty() bool {
return len(*self) == 0
}
func (self *Color) GetOrDefault(defaultColor string) string {
if self.IsEmpty() {
return defaultColor
}
return self.String()
}
func (self *Color) Required(metaData *MetaData) bool {
return metaData.BoolAttrib(StructTagKey, "required")
}
func (self *Color) IsValid() bool {
s := string(*self)
l := len(s)
if l == 0 {
return true
}
if l != 9 || s[0] != '#' {
return false
}
_, err := hex.DecodeString(s[1:])
return err == nil
}
func (self *Color) Validate(metaData *MetaData) error {
if self.Required(metaData) && self.IsEmpty() {
return NewRequiredError(metaData)
}
if !self.IsValid() {
return errors.New("Invalid hex web-color: " + string(*self))
}
return nil
}
// RGBA returns the color as image/color.RGBA struct.
// If the color is empty, a default zero struct will be returned.
func (self *Color) RGBA() color.RGBA {
if self.IsEmpty() {
return color.RGBA{}
}
b, err := hex.DecodeString(self.String()[1:])
if err != nil {
panic(err.Error())
}
if len(b) != 4 {
panic("Invalid web-color length")
}
return color.RGBA{b[0], b[1], b[2], b[3]}
}
func (self *Color) EqualsColor(c color.Color) bool {
r0, g0, b0, a0 := self.RGBA().RGBA()
r1, g1, b1, a1 := c.RGBA()
return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
}