-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
52 lines (49 loc) · 933 Bytes
/
utils.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
package main
import (
"image/color"
"time"
)
var randState = uint64(time.Now().UnixNano())
func RandINT64() uint64 {
randState = (randState ^ (randState << 13)) ^ (randState >> 7) ^ (randState << 17)
return randState
}
func RandFLOAT() float64 {
return float64(RandINT64()/2) / (1 << 63)
}
func hueToRGB(p, q, t float64) float64 {
if t < 0 {
t += 1
}
if t > 1 {
t -= 1
}
switch {
case t < 1.0/6.0:
return p + (q-p)*6*t
case t < 1.0/2.0:
return q
case t < 2.0/3.0:
return p + (q-p)*(2.0/3.0-t)*6
default:
return p
}
}
func hslToRGB(h, s, l float64) color.RGBA {
var r, g, b float64
if s == 0 {
r, g, b = l, l, l
} else {
var q, p float64
if l < 0.5 {
q = l * (1 + s)
} else {
q = l + s - l*s
}
p = 2*l - q
r = hueToRGB(p, q, h+1.0/3.0)
g = hueToRGB(p, q, h)
b = hueToRGB(p, q, h-1.0/3.0)
}
return color.RGBA{R: uint8(r * 255), G: uint8(g * 255), B: uint8(b * 255), A: 255}
}