-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreepalette_test.go
122 lines (115 loc) · 3.16 KB
/
treepalette_test.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
/*
* Copyright 2021 Philoj Johny
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain A copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package treepalette_test
import (
"fmt"
"github.com/philoj/tree-palette"
"github.com/stretchr/testify/assert"
"math"
"math/rand"
"testing"
)
func TestTreePalette_ConvertColor(t *testing.T) {
tests := []struct {
name string
color treepalette.Color
alpha bool
palette []treepalette.PaletteColor
output treepalette.Color
}{
{
name: "nil",
color: nil,
alpha: true,
palette: []treepalette.PaletteColor{
treepalette.NewOpaquePaletteColor(199, 44, 58, 7, "BRICK RED"),
},
output: nil,
},
{
name: "empty palette",
color: treepalette.NewOpaquePaletteColor(2, 181, 160, 12, "PERSIAN GREEN"),
alpha: true,
palette: []treepalette.PaletteColor{},
output: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
p := treepalette.NewPalette(test.palette, test.alpha)
assert.Equal(t, test.output, p.ConvertColor(test.color))
})
}
}
func TestTreePalette_ConvertColorRandomAlpha(t *testing.T) {
for i := int64(1); i <= 10; i++ {
rand.Seed(i)
c := randomColor(true)
p := randomPalette(12, true)
r := closestIndex(c, p)
t.Run(fmt.Sprintf("random transparent palette: seed %d", i), func(t *testing.T) {
p := treepalette.NewPalette(p, true)
cc := p.ConvertColor(c)
assert.Equal(t, r, cc.Index())
})
}
}
func TestTreePalette_ConvertColorRandomNoAlpha(t *testing.T) {
for i := int64(1); i <= 10; i++ {
rand.Seed(i)
c := randomColor(false)
p := randomPalette(12, false)
r := closestIndex(c, p)
t.Run(fmt.Sprintf("random opaque palette: seed %d", i), func(t *testing.T) {
p := treepalette.NewPalette(p, false)
cc := p.ConvertColor(c)
assert.Equal(t, r, cc.Index())
})
}
}
// Helper functions
func randomColor(alpha bool) treepalette.ColorRGBA {
if alpha {
return treepalette.NewTransparentColor(rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Float64())
} else {
return treepalette.NewOpaqueColor(rand.Intn(255), rand.Intn(255), rand.Intn(255))
}
}
func randomPalette(n int, alpha bool) []treepalette.PaletteColor {
p := make([]treepalette.PaletteColor, n)
for i := range p {
p[i] = &treepalette.IndexedColorRGBA{
ColorRGBA: randomColor(alpha),
Id: i,
}
}
return p
}
func closestIndex(c treepalette.Color, p []treepalette.PaletteColor) int {
var minD int64 = math.MaxInt64
var result int
for _, cc := range p {
var d int64 = 0
for i := 0; i < cc.Dimensions(); i++ {
v2, v1 := int64(cc.Dimension(i)), int64(c.Dimension(i))
d += (v2 - v1) * (v2 - v1)
}
if d < minD {
minD, result = d, cc.Index()
}
}
return result
}