-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette_test.go
120 lines (88 loc) · 3.16 KB
/
palette_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
package camalian
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPaletteBuildFromHex(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#FF0000", true)
c2, _ := new(Color).BuildFromHex("#00FF00", true)
p, err := new(Palette).BuildFromHex("#FF0000", "#00FF00")
assert.NoError(t, err)
assert.Len(t, *p, 2)
assert.Equal(t, (*p)[0], c1)
assert.Equal(t, (*p)[1], c2)
}
func TestPaletteBuildFromImage(t *testing.T) {
filePath := "testdata/palette.png"
var image *Image = &Image{FilePath: filePath}
p := new(Palette).BuildFromImage(image)
assert.Len(t, *p, 54000)
}
func TestPaletteSortByRGBAsc(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#00FF00", true)
p, _ := new(Palette).BuildFromHex("#00FF00", "#FF0000")
p.SortBy(SortProperty(Green), true)
assert.Equal(t, (*p)[1], c1)
}
func TestPaletteSortByRGBDesc(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#00FF00", true)
p, _ := new(Palette).BuildFromHex("#FF0000", "#00FF00")
p.SortBy(SortProperty(Green), false)
assert.Equal(t, (*p)[0], c1)
}
func TestPaletteSortByHSLAsc(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#00FF00", true)
p, _ := new(Palette).BuildFromHex("#00FF00", "#FF0000")
p.SortBy(SortProperty(HSLHue), true)
assert.Equal(t, (*p)[1], c1)
}
func TestPaletteSortByHSLDesc(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#00FF00", true)
p, _ := new(Palette).BuildFromHex("#00FF00", "#FF0000")
p.SortBy(SortProperty(HSLHue), false)
assert.Equal(t, (*p)[0], c1)
}
func TestPaletteSortSimilarColors(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#00FF00", true)
c2, _ := new(Color).BuildFromHex("#009900", true)
c3, _ := new(Color).BuildFromHex("#FF0000", true)
c4, _ := new(Color).BuildFromHex("#550000", true)
c5, _ := new(Color).BuildFromHex("#0000FF", true)
c6, _ := new(Color).BuildFromHex("#000055", true)
p := Palette{c1, c3, c5, c2, c4, c6}
p.SortSimilarColors()
assert.Equal(t, []string{"#FF0000", "#550000", "#00FF00", "#009900", "#0000FF", "#000055"}, p.ToHex())
}
func TestAverageColor(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#FF0000", true)
c2, _ := new(Color).BuildFromHex("#FF00FF", true)
p := Palette{c1, c2}
avg := p.AverageColor()
expected, _ := new(Color).BuildFromHex("#FF007f", true)
assert.Equal(t, expected, avg)
}
func TestLightColors(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#000000", true)
c2, _ := new(Color).BuildFromHex("#666666", true)
c3, _ := new(Color).BuildFromHex("#999999", true)
p := Palette{c1, c2, c3}
p2 := p.LightColors(30, 50)
assert.Equal(t, []string{"#666666"}, p2.ToHex())
}
func TestUniqueColors(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#FF0000", true)
c2, _ := new(Color).BuildFromHex("#FF0000", true)
c3, _ := new(Color).BuildFromHex("#999999", true)
p := Palette{c1, c2, c3}
p2 := p.Unique()
assert.Equal(t, []string{"#FF0000", "#999999"}, p2.ToHex())
}
func TestCommonColors(t *testing.T) {
c1, _ := new(Color).BuildFromHex("#FF0000", true)
c2, _ := new(Color).BuildFromHex("#00FF00", true)
c3, _ := new(Color).BuildFromHex("#999999", true)
p := Palette{c1, c2, c3}
p2 := Palette{c1, c2}
p3 := p.Common(&p2)
assert.Equal(t, []string{"#FF0000", "#00FF00"}, p3.ToHex())
}