-
Notifications
You must be signed in to change notification settings - Fork 2
/
hslscale.go
84 lines (73 loc) · 1.3 KB
/
hslscale.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
package draw
import (
"image/color"
"math"
)
// Colormap for 3D vector data.
func HSLMap(x, y, z float32) color.RGBA {
s := sqrtf(x*x + y*y + z*z)
l := 0.5*z + 0.5
h := float32(math.Atan2(float64(y), float64(x)))
return HSLtoRGB(h, s, l)
}
// h = 0..2pi, s=0..1, l=0..1
func HSLtoRGB(h, s, l float32) color.RGBA {
if s > 1 {
s = 1
}
if l > 1 {
l = 1
}
h = h * (180.0 / math.Pi / 60.0)
for h < 0 {
h += 6
}
for h >= 6 {
h -= 6
}
var c float32 // chroma
if l <= 0.5 {
c = 2 * l * s
} else {
c = (2 - 2*l) * s
}
x := c * (1 - abs(fmod(h, 2)-1))
var r, g, b float32
switch {
case 0 <= h && h < 1:
r, g, b = c, x, 0.
case 1 <= h && h < 2:
r, g, b = x, c, 0.
case 2 <= h && h < 3:
r, g, b = 0., c, x
case 3 <= h && h < 4:
r, g, b = 0, x, c
case 4 <= h && h < 5:
r, g, b = x, 0., c
case 5 <= h && h < 6:
r, g, b = c, 0., x
}
m := l - 0.5*c
r, g, b = r+m, g+m, b+m
R, G, B := uint8(255*r), uint8(255*g), uint8(255*b)
return color.RGBA{R, G, B, 255}
}
// modulo
func fmod(number, mod float32) float32 {
for number < mod {
number += mod
}
for number >= mod {
number -= mod
}
return number
}
func abs(number float32) float32 {
if number < 0 {
return -number
} // else
return number
}
func sqrtf(x float32) float32 {
return float32(math.Sqrt(float64(x)))
}