-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.go
47 lines (37 loc) · 990 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
package tmx
import "strconv"
type hexcolor string
//Tiled uses this defaults when no background color
//was set
const (
defaultRed = 128
defaultBlue = 128
defaultGreen = 128
defaultAlpha = 255
)
//implement image.Color interface
func (c hexcolor) RGBA() (r, g, b, a uint32) {
data := []byte(string(c))
if len(data) == 0 {
return defaultRed, defaultBlue, defaultGreen, defaultAlpha
}
if data[0] == '#' {
data = data[1:]
}
if len(data) != 6 {
return defaultRed, defaultBlue, defaultGreen, defaultAlpha
}
rx, err := strconv.ParseInt(string(data[0:2]), 16, 0)
if err != nil {
return defaultRed, defaultBlue, defaultGreen, defaultAlpha
}
gx, err := strconv.ParseInt(string(data[2:4]), 16, 0)
if err != nil {
return defaultRed, defaultBlue, defaultGreen, defaultAlpha
}
bx, err := strconv.ParseInt(string(data[4:6]), 16, 0)
if err != nil {
return defaultRed, defaultBlue, defaultGreen, defaultAlpha
}
return uint32(rx), uint32(gx), uint32(bx), 255
}