-
Notifications
You must be signed in to change notification settings - Fork 0
/
palette.go
218 lines (180 loc) · 4.5 KB
/
palette.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package camalian
import (
"sort"
utils "github.com/nazarhussain/camalian-go/pkg/utils"
)
type Palette []*Color
type SortProperty int
const (
HSLHue SortProperty = iota
HSLSaturation
HSLLightness
HSVHue
HSVSaturation
HSVValue
Red
Green
Blue
)
var colorSortPropMap = map[SortProperty]func(*Color, *Color, bool) bool{
HSLHue: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.HSL.H < c2.HSL.H
}
return c1.HSL.H > c2.HSL.H
},
HSLSaturation: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.HSL.S < c2.HSL.S
}
return c1.HSL.S > c2.HSL.S
},
HSLLightness: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.HSL.L < c2.HSL.L
}
return c1.HSL.L > c2.HSL.L
},
HSVHue: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.HSV.H < c2.HSV.H
}
return c1.HSV.H > c2.HSV.H
},
HSVSaturation: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.HSV.S < c2.HSV.S
}
return c1.HSV.S > c2.HSV.S
},
HSVValue: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.HSV.V < c2.HSV.V
}
return c1.HSV.V > c2.HSV.V
},
Red: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.R < c2.R
}
return c1.R > c2.R
},
Green: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.G < c2.G
}
return c1.G > c2.G
},
Blue: func(c1 *Color, c2 *Color, order bool) bool {
if order {
return c1.B < c2.B
}
return c1.B > c2.B
},
}
// Build color palette from hex strings
// new(Palette).BuildFromHex("#FF0000", "#00FF00")
func (p *Palette) BuildFromHex(colors ...string) (*Palette, error) {
for _, hex := range colors {
c, err := new(Color).BuildFromHex(hex, true)
if err != nil {
return nil, err
}
*p = append(*p, c)
}
return p, nil
}
// Build color palette from a Quantizeable interface
func (p *Palette) BuildFromImage(source Quantizeable) *Palette {
for _, c := range source.Pixels() {
c := new(Color).Build(c.R, c.G, c.B, true)
*p = append(*p, c)
}
return p
}
// Sort color palette by some properties
func (p *Palette) SortBy(v SortProperty, order bool) *Palette {
sort.SliceStable(*p, func(i, j int) bool {
return colorSortPropMap[v]((*p)[i], (*p)[j], order)
})
return p
}
// Sort palette to visually similar colors
func (p *Palette) SortSimilarColors() *Palette {
return p.SortBy(SortProperty(HSLHue), true)
}
// Calculate avargage color in RGB Color Space for the whole palette
func (p *Palette) AverageColor() *Color {
colors := make([]interface{}, len(*p))
for index, value := range *p {
colors[index] = value
}
reds := utils.Sum(utils.Collect(colors, func(v interface{}) interface{} {
return v.(*Color).R
}))
greens := utils.Sum(utils.Collect(colors, func(v interface{}) interface{} {
return v.(*Color).G
}))
blues := utils.Sum(utils.Collect(colors, func(v interface{}) interface{} {
return v.(*Color).B
}))
size := float64(len(*p))
return new(Color).Build(uint8(reds.(float64)/size), uint8(greens.(float64)/size), uint8(blues.(float64)/size), true)
}
// Filter colors in particular range for lightness in HSL color space
func (p *Palette) LightColors(limit1, limit2 uint16) *Palette {
newPalette := new(Palette)
min, max := utils.FindMinMax(limit1, limit2)
min1 := float32(min.(uint16))
max1 := float32(max.(uint16))
for _, c := range *p {
if c.HSL.L > min1 && c.HSL.L < max1 {
*newPalette = append(*newPalette, c)
}
}
return newPalette
}
func (p *Palette) ToHex() []string {
colors := []string{}
for _, c := range *p {
colors = append(colors, c.ToHex())
}
return colors
}
// Quantize the palette by specific Quantizer for given number of colors
func (p *Palette) Quantize(q Quantizer, count uint) *Palette {
return q.Quantize(p, count)
}
// Remove redundant colors in palette and get unique colors
func (p *Palette) Unique() *Palette {
keys := make(map[string]bool)
p2 := new(Palette)
for _, color := range *p {
key := color.ToHex()
if _, value := keys[key]; !value {
keys[key] = true
*p2 = append(*p2, color)
}
}
return p2
}
// Fetch common colors among two color palettes
func (p *Palette) Common(p2 *Palette) *Palette {
m := make(map[string]bool)
result := new(Palette)
for _, color := range *p {
m[color.ToHex()] = false
}
for _, color := range *p2 {
if _, v := m[color.ToHex()]; v {
m[color.ToHex()] = true
}
}
for hex, exists := range m {
if exists {
c, _ := new(Color).BuildFromHex(hex, true)
*result = append(*result, c)
}
}
return result.SortSimilarColors()
}