-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.go
53 lines (49 loc) · 973 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
49
50
51
52
53
package charts
import (
"math"
"regexp"
"strconv"
"strings"
"github.com/go-analyze/charts/chartdraw/drawing"
)
func isLightColor(c Color) bool {
r := float64(c.R) * float64(c.R) * 0.299
g := float64(c.G) * float64(c.G) * 0.587
b := float64(c.B) * float64(c.B) * 0.114
return math.Sqrt(r+g+b) > 127.5
}
func parseColor(color string) Color {
c := Color{}
if color == "" {
return c
}
if strings.HasPrefix(color, "#") {
return drawing.ColorFromHex(color[1:])
}
reg := regexp.MustCompile(`\((\S+)\)`)
result := reg.FindAllStringSubmatch(color, 1)
if len(result) == 0 || len(result[0]) != 2 {
return c
}
arr := strings.Split(result[0][1], ",")
if len(arr) < 3 {
return c
}
// set the default value to 255
c.A = 255
for index, v := range arr {
value, _ := strconv.Atoi(strings.TrimSpace(v))
ui8 := uint8(value)
switch index {
case 0:
c.R = ui8
case 1:
c.G = ui8
case 2:
c.B = ui8
default:
c.A = ui8
}
}
return c
}