-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorhsl.go
90 lines (77 loc) · 1.97 KB
/
colorhsl.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package color
import (
"math"
"strconv"
"strings"
)
func convertStringToHSL(s string) (H, S, L float64) {
s = strings.ToLower(s)
s = strings.TrimPrefix(s, "hsl(")
s = strings.TrimSuffix(s, ")")
removeSpaceAndJoin := strings.Join(strings.Fields(s), "")
hsl := strings.Split(removeSpaceAndJoin, ",")
hTrimmed, err1 := strconv.Atoi(strings.Trim(hsl[0], "%,"))
sTrimmed, err2 := strconv.Atoi(strings.Trim(hsl[1], "%,"))
lTrimmed, err3 := strconv.Atoi(strings.Trim(hsl[2], "%,"))
if err1 != nil || err2 != nil || err3 != nil || hTrimmed < 0 || hTrimmed > 360 || sTrimmed < 0 || sTrimmed > 100 || lTrimmed < 0 || lTrimmed > 100 {
WrongColorType()
}
H = float64(hTrimmed)
S = float64(sTrimmed) / 100
L = float64(lTrimmed) / 100
return H, S, L
}
func converToRGB(H, S, L float64) (C, X, m float64) {
modC := math.Abs((2 * L) - 1)
modX := math.Abs(math.Mod((H/60), 2) - 1)
// we compute chroma, by multiplying saturation by the maximum chroma for a given lightness or value
C = (1 - modC) * S
// we find the point on one of the bottom three faces of the RGB cube which has the same hue and chroma as our color
X = C * (1 - modX)
m = L - C/2
return C, X, m
}
func checkHueValue(H, C, X, m float64) (R, G, B string) {
var R1 float64
var G1 float64
var B1 float64
// checking the range of the hue value for lightness
switch {
case H < 60:
R1 = C
G1 = X
B1 = 0
case H < 120:
R1 = X
G1 = C
B1 = 0
case H < 180:
R1 = 0
G1 = C
B1 = X
case H < 240:
R1 = 0
G1 = X
B1 = C
case H < 300:
R1 = X
G1 = 0
B1 = C
case H < 360:
R1 = C
G1 = 0
B1 = X
}
// we add equal amounts of R, G, and B to reach the proper lightness
R = strconv.Itoa(int((R1 + m) * 255))
G = strconv.Itoa(int((G1 + m) * 255))
B = strconv.Itoa(int((B1 + m) * 255))
return R, G, B
}
// get the hsl value in ansi code
func ColorHSL(s string) string {
H, S, L := convertStringToHSL(s)
C, X, m := converToRGB(H, S, L)
R, G, B := checkHueValue(H, C, X, m)
return RGBToANSI(R, G, B)
}