-
Notifications
You must be signed in to change notification settings - Fork 1
/
color_map.go
64 lines (49 loc) · 1.28 KB
/
color_map.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
package extractor
import (
"container/heap"
"math"
)
type colorMap struct {
boxes priorityQueue
}
func newColorMap() *colorMap {
colorMap := &colorMap{}
colorMap.boxes = priorityQueue{make([]*box, 0), countTimesVolume}
heap.Init(&colorMap.boxes)
return colorMap
}
func (colorMap *colorMap) push(vbox *box) {
vbox.color = vbox.average()
colorMap.boxes.Push(vbox)
}
func (colorMap *colorMap) mapColor(color *pixel) *pixel {
for i := 0; i < colorMap.boxes.Len(); i++ {
box := colorMap.boxes.boxes[i]
if box.contains(color) {
return box.color
}
}
return colorMap.getNearestColor(color)
}
func (colorMap *colorMap) getNearestColor(color *pixel) *pixel {
d1 := -10000.0
var ret *pixel
for i := 0; i < colorMap.boxes.Len(); i++ {
d2 := math.Sqrt(math.Pow(float64(color.R-colorMap.boxes.boxes[i].color.R), 2) + math.Pow(float64(color.G-colorMap.boxes.boxes[i].color.G), 2) + math.Pow(float64(color.B-colorMap.boxes.boxes[i].color.B), 2))
if d2 < d1 {
d1 = d2
ret = colorMap.boxes.boxes[i].color
}
}
return ret
}
func (colorMap *colorMap) len() int {
return colorMap.boxes.Len()
}
func (colorMap *colorMap) getPalette() []pixel {
var pixels []pixel
for i := 0; i < colorMap.len(); i++ {
pixels = append(pixels, *colorMap.boxes.boxes[i].color)
}
return pixels
}