-
Notifications
You must be signed in to change notification settings - Fork 1
/
hsl.go
124 lines (96 loc) · 1.72 KB
/
hsl.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
114
115
116
117
118
119
120
121
122
123
124
package coco
import "math"
func Hsl2Rgb(h float64, s float64, l float64) [3]float64 {
h = h / 360
s = s / 100
l = l / 100
var t2 float64
var t3 float64
var val float64
var result [3]float64
if s == 0 {
val = l * 255
result[0] = math.Round(val)
result[1] = math.Round(val)
result[2] = math.Round(val)
return result
}
if l < 0.5 {
t2 = l * (1 + s)
} else {
t2 = l + s - l*s
}
t1 := 2*l - t2
rgb := [3]float64{0, 0, 0}
for i := 0; i < 3; i++ {
t3 = h + 1.0/3.0*(-(float64(i) - 1))
if t3 < 0 {
t3++
}
if t3 > 1 {
t3--
}
if (6 * t3) < 1 {
val = t1 + (t2-t1)*6*t3
} else if (2 * t3) < 1 {
val = t2
} else if (3 * t3) < 2 {
val = t1 + (t2-t1)*(2.0/3.0-t3)*6
} else {
val = t1
}
rgb[i] = val * 255
}
result[0] = math.Round(rgb[0])
result[1] = math.Round(rgb[1])
result[2] = math.Round(rgb[2])
return result
}
func Hsl2Hsv(h float64, s float64, l float64) [3]float64 {
s = s / 100
l = l / 100
smin := s
lmin := math.Max(l, 0.01)
l *= 2
if l <= 1 {
s *= l
} else {
s *= 2 - l
}
if lmin <= 1 {
smin *= lmin
} else {
smin *= 2 - lmin
}
v := (l + s) / 2
var sv float64
if l == 0 {
sv = (2 * smin) / (lmin + smin)
} else {
sv = (2 * s) / (l + s)
}
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(sv * 100)
result[2] = math.Round(v * 100)
return result
}
func Hsl2Hcg(h float64, s float64, l float64) [3]float64 {
s = s / 100
l = l / 100
var c float64
if l < 0.5 {
c = 2.0 * s * l
} else {
c = 2.0 * s * (1.0 - l)
}
var f float64
if c < 1.0 {
f = (l - 0.5*c) / (1.0 - c)
}
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(c * 100)
result[2] = math.Round(f * 100)
return result
}