-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshades.go
125 lines (100 loc) · 2.84 KB
/
shades.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package color
import "math"
// Shades returns n shades of the color. New colors are created by reducing
// lightness of the original color, making them darker.
//
// The first element of the resulting slice is the color itself. The last is
// black. Special case is n < 2: a slice with a single element being the color
// itself is returned.
func Shades(color Color, n int) []Color {
hsl := color.HSL()
if n < 2 {
return []Color{hsl}
}
shades := make([]Color, n)
shades[0] = hsl
floatL := float64(hsl.L)
step := floatL / float64(n-1)
for i := 1; i < n; i++ {
decrement := step * float64(i)
newL := int(math.Round(floatL - decrement))
normalizedL := max(newL, 0)
shades[i] = &HSL{
H: hsl.H,
S: hsl.S,
L: normalizedL,
}
}
return shades
}
// Tints returns n tints shades of the color. New colors are created by
// increasing lightness of the original color, making them lighter.
//
// The first element of the resulting slice is the color itself. The last is
// white. Special case is n < 2: a slice with a single element being the color
// itself is returned.
func Tints(color Color, n int) []Color {
hsl := color.HSL()
if n < 2 {
return []Color{hsl}
}
tints := make([]Color, n)
tints[0] = hsl
floatL := float64(hsl.L)
step := (100 - floatL) / float64(n-1)
for i := 1; i < n; i++ {
increment := step * float64(i)
newL := int(math.Round(floatL + increment))
normalizedL := min(newL, 100)
tints[i] = &HSL{
H: hsl.H,
S: hsl.S,
L: normalizedL,
}
}
return tints
}
// Tones returns n tones of the color. New colors are created by reducing
// saturation of the original color and changing lightness towards 50% (in
// HSL), making them more grayish.
//
// The first element of the resulting slice is the color itself. The last is
// gray. Special case is n < 2: a slice with a single element being the color
// itself is returned.
func Tones(color Color, n int) []Color {
hsl := color.HSL()
if n < 2 {
return []Color{hsl}
}
tones := make([]Color, n)
tones[0] = hsl
floatL := float64(hsl.L)
floatS := float64(hsl.S)
floatCount := float64(n - 1)
stepS := floatS / floatCount
stepL := (50 - floatL) / floatCount
for i := 1; i < n; i++ {
var normalizedL int
// Normalize new lightness. If the original color is lighter than gray,
// decrease the lightness, otherwise increase it. Ensure that lightness
// is not lower / greater than gray respectively.
if hsl.L > 50 {
decrement := stepL * float64(i)
newL := int(math.Round(floatL - decrement))
normalizedL = min(newL, 50)
} else {
increment := stepL * float64(i)
newL := int(math.Round(floatL + increment))
normalizedL = max(newL, 50)
}
decrementS := stepS * float64(i)
newS := int(math.Round(floatS - decrementS))
normalizedS := max(newS, 0)
tones[i] = &HSL{
H: hsl.H,
S: normalizedS,
L: normalizedL,
}
}
return tones
}