Debian Codegraph @

util.go

raw symbols SCIP
1 package rgbterm
2
3 import (
4 "math"
5 )
6
7 // stolen from gorilla color:
8 // https://code.google.com/p/gorilla/source/browse/?r=ef489f63418265a7249b1d53bdc358b09a4a2ea0#hg%2Fcolor
9
10 // RGBtoHSL is a helper that transforms 8bits HSL colors to
11 // 8bits RGB colors.
12 func RGBtoHSL(r, g, b uint8) (h, s, l float64) {
13 fR := float64(r) / 255
14 fG := float64(g) / 255
15 fB := float64(b) / 255
16 max := math.Max(math.Max(fR, fG), fB)
17 min := math.Min(math.Min(fR, fG), fB)
18 l = (max + min) / 2
19 if max == min {
20 // Achromatic.
21 h, s = 0, 0
22 } else {
23 // Chromatic.
24 d := max - min
25 if l > 0.5 {
26 s = d / (2.0 - max - min)
27 } else {
28 s = d / (max + min)
29 }
30 switch max {
31 case fR:
32 h = (fG - fB) / d
33 if fG < fB {
34 h += 6
35 }
36 case fG:
37 h = (fB-fR)/d + 2
38 case fB:
39 h = (fR-fG)/d + 4
40 }
41 h /= 6
42 }
43 return
44 }
45
46 // HSLtoRGB is a helper that transforms 8bits RGB colors to
47 // 8bits HSL colors.
48 func HSLtoRGB(h, s, l float64) (r, g, b uint8) {
49 var fR, fG, fB float64
50 if s == 0 {
51 fR, fG, fB = l, l, l
52 } else {
53 var q float64
54 if l < 0.5 {
55 q = l * (1 + s)
56 } else {
57 q = l + s - s*l
58 }
59 p := 2*l - q
60 fR = hueToRGB(p, q, h+1.0/3)
61 fG = hueToRGB(p, q, h)
62 fB = hueToRGB(p, q, h-1.0/3)
63 }
64 r = uint8((fR * 255) + 0.5)
65 g = uint8((fG * 255) + 0.5)
66 b = uint8((fB * 255) + 0.5)
67 return
68 }
69
70 // hueToRGB is a helper function for HSLtoRGB.
71 func hueToRGB(p, q, t float64) float64 {
72 if t < 0 {
73 t++
74 }
75 if t > 1 {
76 t--
77 }
78 if t < 1.0/6 {
79 return p + (q-p)*6*t
80 }
81 if t < 0.5 {
82 return q
83 }
84 if t < 2.0/3 {
85 return p + (q-p)*(2.0/3-t)*6
86 }
87 return p
88 }