-
Notifications
You must be signed in to change notification settings - Fork 1
/
hsv.go
97 lines (80 loc) · 1.61 KB
/
hsv.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
package coco
import "math"
func Hsv2Rgb(h float64, s float64, v float64) [3]float64 {
h = h / 60
s = s / 100
v = v / 100
hi := math.Mod(math.Floor(h), 6)
f := h - math.Floor(h)
p := 255 * v * (1 - s)
q := 255 * v * (1 - (s * f))
t := 255 * v * (1 - (s * (1 - f)))
v *= 255
var result [3]float64
switch hi {
case 0:
result[0] = math.Round(v)
result[1] = math.Round(t)
result[2] = math.Round(p)
case 1:
result[0] = math.Round(q)
result[1] = math.Round(v)
result[2] = math.Round(p)
case 2:
result[0] = math.Round(p)
result[1] = math.Round(v)
result[2] = math.Round(t)
case 3:
result[0] = math.Round(p)
result[1] = math.Round(q)
result[2] = math.Round(v)
case 4:
result[0] = math.Round(t)
result[1] = math.Round(p)
result[2] = math.Round(v)
case 5:
result[0] = math.Round(v)
result[1] = math.Round(p)
result[2] = math.Round(q)
}
return result
}
func Hsv2Hsl(h float64, s float64, v float64) [3]float64 {
s = s / 100
v = v / 100
vmin := math.Max(v, 0.01)
var sl float64
var l float64
l = (2 - s) * v
lmin := (2 - s) * vmin
sl = s * vmin
if lmin <= 1 {
sl /= lmin
} else {
sl /= 2 - lmin
}
// sl = sl
if math.IsNaN(sl) {
sl = 0
}
l /= 2
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(sl * 100)
result[2] = math.Round(l * 100)
return result
}
func Hsv2Hcg(h float64, s float64, v float64) [3]float64 {
s = s / 100
v = v / 100
c := s * v
var g float64 = 0
if c < 1.0 {
g = (v - c) / (1 - c)
}
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(c * 100)
result[2] = math.Round(g * 100)
return result
}