forked from SimonWaldherr/golang-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
executable file
·50 lines (44 loc) · 946 Bytes
/
color.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
package main
import (
"fmt"
"math"
)
func rgb2hsl(r int, g int, b int) (int, int, int) {
var rf, gf, bf, max, min, l, d, s, h float64
rf = math.Max(math.Min(float64(r)/255, 1), 0)
gf = math.Max(math.Min(float64(g)/255, 1), 0)
bf = math.Max(math.Min(float64(b)/255, 1), 0)
max = math.Max(rf, math.Max(gf, bf))
min = math.Min(rf, math.Min(gf, bf))
l = (max + min) / 2
if max != min {
d = max - min
if l > 0.5 {
s = d / (2 - max - min)
} else {
s = d / (max + min)
}
if max == rf {
if gf < bf {
h = (gf-bf)/d + 6
} else {
h = (gf - bf) / d
}
} else if max == gf {
h = (bf-rf)/d + 2
} else {
h = (rf-gf)/d + 4
}
} else {
h = 0
s = 0
}
return int(h * 60), int(s * 100), int(l * 100)
}
func main() {
fmt.Println(rgb2hsl(121, 167, 22))
fmt.Println(rgb2hsl(69, 209, 237))
fmt.Println(rgb2hsl(254, 207, 37))
fmt.Println(rgb2hsl(122, 167, 255))
fmt.Println(rgb2hsl(255, 255, 255))
}