/
color.go
48 lines (42 loc) · 933 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
48
package property
import (
"github.com/lucasb-eyer/go-colorful"
"github.com/sutd-statnlp/service-ladrawex/log"
)
// Color is the property of component.
type Color struct {
R uint8
G uint8
B uint8
}
// SetRGB sets rgb values.
func (color *Color) SetRGB(r, g, b uint8) {
color.R = r
color.G = g
color.B = b
}
// RGB returns rgb values.
func (color *Color) RGB() (uint8, uint8, uint8) {
return color.R, color.G, color.B
}
// Hex returns hexdecimal value.
func (color *Color) Hex() string {
hexColor := colorful.LinearRgb(
float64(color.R)/255,
float64(color.G)/255,
float64(color.B)/255,
)
return hexColor.Hex()
}
// FromHex sets rgb values from hex string.
func (color *Color) FromHex(hex string) bool {
hexColor, err := colorful.Hex(hex)
if err != nil {
log.Error(err)
return false
}
color.R = uint8(hexColor.R * 255)
color.G = uint8(hexColor.G * 255)
color.B = uint8(hexColor.B * 255)
return true
}