-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtmx_hexcolor.go
124 lines (112 loc) · 2.74 KB
/
tmx_hexcolor.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
package tiled
import (
"encoding/hex"
"encoding/xml"
"errors"
"image/color"
"strings"
)
// HexColor handles the conversion between hex color strings in form #AARRGGBB
// to color.RGBA structure. Be aware that this doesn't match CSS hex color because
// the alpha channel appears first.
type HexColor struct {
c color.RGBA
}
// ParseHexColor converts hex color strings into HexColor structures
// This function can handle colors with and withouth optional alpha channel
// The leading '#' character is not required for backwards compatibility with Transparency Tiled filed
// https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#image
func ParseHexColor(s string) (HexColor, error) {
c, err := parseHexColor(s)
if err != nil {
return HexColor{}, err
}
return HexColor{c: c}, nil
}
// NewHexColor is a shorthand to build a HexColor
func NewHexColor(r, g, b, a uint32) HexColor {
return HexColor{
c: color.RGBA{
uint8(r),
uint8(g),
uint8(b),
uint8(a),
},
}
}
// RGBA implements color.Color interface
func (color *HexColor) RGBA() (r, g, b, a uint32) {
return color.c.RGBA()
}
func (color *HexColor) String() string {
src := []byte{
color.c.A,
color.c.R,
color.c.G,
color.c.B,
}
if color.c.A == 255 {
src = src[1:]
}
dst := make([]byte, hex.EncodedLen(len(src))+1)
hex.Encode(dst[1:], src)
dst[0] = '#'
return string(dst)
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr
func (color *HexColor) UnmarshalXMLAttr(attr xml.Attr) error {
c, err := parseHexColor(attr.Value)
if err != nil {
return err
}
color.c = c
return nil
}
// MarshalXMLAttr implements xml.MarshalerAttr
func (color *HexColor) MarshalXMLAttr(name xml.Name) (attr xml.Attr, err error) {
attr.Name = name
if color != nil {
attr.Value = color.String()
}
return
}
func parseHexColor(s string) (c color.RGBA, err error) {
hexToByte := func(b byte) byte {
switch {
case b >= '0' && b <= '9':
return b - '0'
case b >= 'a' && b <= 'f':
return b - 'a' + 10
case b >= 'A' && b <= 'F':
return b - 'A' + 10
}
err = errors.New("Invalid Format")
return 0
}
s = strings.TrimPrefix(s, "#")
switch len(s) {
case 8:
c.A = hexToByte(s[0])<<4 + hexToByte(s[1])
c.R = hexToByte(s[2])<<4 + hexToByte(s[3])
c.G = hexToByte(s[4])<<4 + hexToByte(s[5])
c.B = hexToByte(s[6])<<4 + hexToByte(s[7])
case 6:
c.A = 0xff
c.R = hexToByte(s[0])<<4 + hexToByte(s[1])
c.G = hexToByte(s[2])<<4 + hexToByte(s[3])
c.B = hexToByte(s[4])<<4 + hexToByte(s[5])
case 4:
c.A = hexToByte(s[0]) * 17
c.R = hexToByte(s[1]) * 17
c.G = hexToByte(s[2]) * 17
c.B = hexToByte(s[3]) * 17
case 3:
c.A = 0xff
c.R = hexToByte(s[0]) * 17
c.G = hexToByte(s[1]) * 17
c.B = hexToByte(s[2]) * 17
default:
err = errors.New("Invalid Format")
}
return
}