-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (63 loc) · 1.22 KB
/
Copy pathmain.go
File metadata and controls
73 lines (63 loc) · 1.22 KB
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
package colconv
import (
"fmt"
"math"
)
// RgbToHsl takes 3 rgb digits and turn them into a hsl string
//
// Based on https://github.com/gerow/go-color
func RgbToHsl(red, green, blue uint8) string {
var h, s float64
//region TYPE CONVERSIONS
// RGB INT TO FLOAT
r := float64(red) / 255
g := float64(green) / 255
b := float64(blue) / 255
//endregion
max := max(r, g, b)
min := min(r, g, b)
delta := max - min
deltaH := delta / 2
total := max + min
//region LUMINOSITY
l := total / 2
//endregion
//region SATURATION
switch {
// GRAY
case delta == 0:
return fmt.Sprint("hsl(0, 0%, ", math.Round(l*100), "%)")
// NOT GRAY
case l < 0.5:
s = delta / total
default:
s = delta / (2 - total)
}
//endregion
//region HUE
r2 := (((max - r) / 6) + deltaH) / delta
g2 := (((max - g) / 6) + deltaH) / delta
b2 := (((max - b) / 6) + deltaH) / delta
switch {
case r == max:
h = b2 - g2
case g == max:
h = (1.0 / 3.0) + r2 - b2
case b == max:
h = (2.0 / 3.0) + g2 - r2
}
//endregion
//region EDGE CASES
//Wraparounds
switch {
case h < 0:
h++
case h > 1:
h--
}
//endregion
h *= 360
s *= 100
l *= 100
return fmt.Sprintf("hsl(%.0f, %.0f%%, %.0f%%)", math.Round(h), math.Round(s), math.Round(l))
}