-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhcg.go
113 lines (90 loc) · 1.79 KB
/
hcg.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
package coco
import "math"
func Hcg2Rgb(h float64, c float64, g float64) [3]float64 {
h = h / 360
c = c / 100
g = g / 100
var result [3]float64
if c == 0 {
result[0] = math.Round(g * 255)
result[1] = math.Round(g * 255)
result[2] = math.Round(g * 255)
return result
}
pure := [3]float64{0, 0, 0}
hi := math.Mod(h, 1) * 6
v := math.Mod(hi, 1)
w := 1 - v
var mg float64 = 0
switch math.Floor(hi) {
case 0:
pure[0] = 1
pure[1] = v
pure[2] = 0
case 1:
pure[0] = w
pure[1] = 1
pure[2] = 0
case 2:
pure[0] = 0
pure[1] = 1
pure[2] = v
case 3:
pure[0] = 0
pure[1] = w
pure[2] = 1
case 4:
pure[0] = v
pure[1] = 0
pure[2] = 1
default:
pure[0] = 1
pure[1] = 0
pure[2] = w
}
mg = (1.0 - c) * g
result[0] = math.Round((c*pure[0] + mg) * 255)
result[1] = math.Round((c*pure[1] + mg) * 255)
result[2] = math.Round((c*pure[2] + mg) * 255)
return result
}
func Hcg2Hsv(h float64, c float64, g float64) [3]float64 {
c = c / 100
g = g / 100
v := c + g*(1.0-c)
var s float64 = 0
if v > 0.0 {
s = c / v
}
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(s * 100)
result[2] = math.Round(v * 100)
return result
}
func Hcg2Hsl(h float64, c float64, g float64) [3]float64 {
c = c / 100
g = g / 100
l := g*(1.0-c) + 0.5*c
var s float64 = 0
if l > 0.0 && l < 0.5 {
s = c / (2 * l)
} else if l >= 0.5 && l < 1.0 {
s = c / (2 * (1 - l))
}
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(s * 100)
result[2] = math.Round(l * 100)
return result
}
func Hcg2Hwb(h float64, c float64, g float64) [3]float64 {
c = c / 100
g = g / 100
v := c + g*(1.0-c)
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round((v - c) * 100)
result[2] = math.Round((1 - v) * 100)
return result
}