fic: Pick HSL function to generate random colors

This commit is contained in:
nemunaire 2021-09-04 20:51:30 +02:00
parent 6395eaaf5f
commit 63de5d64b1
3 changed files with 60 additions and 1 deletions

View file

@ -125,3 +125,57 @@ func Apr1Md5(password string, salt string) string {
return resultString
}
// Function copied from https://github.com/gerow/go-color/blob/master/color.go
type HSL struct {
H, S, L float64
}
func hueToRGB(v1, v2, h float64) uint32 {
if h < 0 {
h += 1
}
if h > 1 {
h -= 1
}
var res float64
switch {
case 6*h < 1:
res = (v1 + (v2-v1)*6*h)
case 2*h < 1:
res = v2
case 3*h < 2:
res = v1 + (v2-v1)*((2.0/3.0)-h)*6
default:
res = v1
}
return uint32((res + 1/512.0) * 255)
}
func (c HSL) ToRGB() (rgb uint32) {
h := c.H
s := c.S
l := c.L
if s == 0 {
// it's gray
v := uint32((l + 1/512.0) * 255)
return v*65536 + v*256 + v
}
var v1, v2 float64
if l < 0.5 {
v2 = l * (1 + s)
} else {
v2 = (l + s) - (s * l)
}
v1 = 2*l - v2
r := hueToRGB(v1, v2, h+(1.0/3.0))
g := hueToRGB(v1, v2, h)
b := hueToRGB(v1, v2, h-(1.0/3.0))
return r*65536 + g*256 + b
}