-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
73 lines (64 loc) · 1.88 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
package glx
import (
"encoding/binary"
"image/color"
"math"
)
type Color uint32
//goland:noinspection GoUnusedConst
const (
ColorWhite Color = 0xFFFFFFFF
ColorBlack Color = 0x000000FF
ColorGray Color = 0x777777FF
ColorRed Color = 0xFF0000FF
ColorGreen Color = 0x00FF00FF
ColorBlue Color = 0x0000FFFF
)
// ColorFromRGBA will encode color from RGBA components
// each component is uint8 values (0 .. 255)
func ColorFromRGBA(r, g, b, a uint8) Color {
return Color(binary.LittleEndian.Uint32([]byte{r, g, b, a}))
}
// ColorFromHex just cast uint32 (hex) to color type
// is alias for cast:
//
// c := glx.Color(0xff0000ff)
// c := glx.ColorFromHex(0xff0000ff)
func ColorFromHex(hex uint32) Color {
return Color(hex)
}
// RGBA will convert color to golang representation
func (c Color) RGBA() color.RGBA {
vec := c.VecRGBA()
return color.RGBA{
R: uint8(vec.X * math.MaxUint8),
G: uint8(vec.Y * math.MaxUint8),
B: uint8(vec.Z * math.MaxUint8),
A: uint8(vec.W * math.MaxUint8),
}
}
// VecRGBA converts encoded uint32 color into 4 byte values (r,g,b,a)
// and cast in to Vec4 float32 in range of (0 .. 1)
func (c Color) VecRGBA() Vec4 {
return Vec4{
X: float32(c>>24&0xff) / math.MaxUint8,
Y: float32(c>>16&0xff) / math.MaxUint8,
Z: float32(c>>8&0xff) / math.MaxUint8,
W: float32(c&0xff) / math.MaxUint8,
}
}
// Split will decode hex to 4 color components
// each component is uint8 values (0 .. 255)
func (c Color) Split() (r uint8, g uint8, b uint8, a uint8) {
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, uint32(c))
return bytes[3], bytes[2], bytes[1], bytes[0]
}
// SplitRGB will decode hex to 3 color components
// each component is uint8 values (0 .. 255)
// alpha component is just ignored
func (c Color) SplitRGB() (r uint8, g uint8, b uint8) {
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, uint32(c))
return bytes[3], bytes[2], bytes[1]
}