-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
colorfinder_test.go
172 lines (140 loc) · 3.7 KB
/
colorfinder_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
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
package colorfinder
import (
"bytes"
"compress/gzip"
"fmt"
"image"
"io"
"io/ioutil"
"log"
"os"
"reflect"
"strings"
"testing"
)
func TestSimplePixel(t *testing.T) {
assertFindsRightColor(t, "white1x1.png", "ffffff")
assertFindsRightColor(t, "black1x1.png", "000000")
}
func TestImageFormats(t *testing.T) {
assertFindsRightColor(t, "white1x1.gif", "ffffff")
assertFindsRightColor(t, "white1x1.jpg", "ffffff")
assertFindsRightColor(t, "white1x1.png", "ffffff")
}
func TestFindColors01(t *testing.T) {
assertFindsRightColor(t, "icon01.png.gz", "113671")
}
func TestFindColors02(t *testing.T) {
assertFindsRightColor(t, "icon02.png.gz", "cb1c1f")
}
func TestFindColors03(t *testing.T) {
assertFindsRightColor(t, "icon03.png.gz", "f48024")
}
func TestFindColors04(t *testing.T) {
assertFindsRightColor(t, "icon04.png.gz", "cfdc00")
}
func TestFindColors05(t *testing.T) {
assertFindsRightColor(t, "icon05.png.gz", "ffa700")
}
func TestFindColors06(t *testing.T) {
assertFindsRightColor(t, "icon06.png.gz", "ff6600")
}
func TestFindColors07(t *testing.T) {
assertFindsRightColor(t, "icon07.png.gz", "e61a30")
}
func TestFindColors08(t *testing.T) {
// .ico with png
assertFindsRightColor(t, "icon08.ico.gz", "14e06e")
}
func TestFindColors09(t *testing.T) {
// .ico with 32-bit bmp
assertFindsRightColor(t, "icon09.ico.gz", "1c5182")
}
func TestFindColors10(t *testing.T) {
// .ico with 8-bit bmp, ColorsUsed=0
assertFindsRightColor(t, "icon10.ico.gz", "fe6d4c")
}
func TestFindColors11(t *testing.T) {
// .ico with 8-bit bmp, ColorsUsed=256
assertFindsRightColor(t, "icon11.ico.gz", "a30000")
}
func BenchmarkFindMainColor152x152(b *testing.B) {
file, _ := os.Open(testdataDir + "icon02.png.gz")
gzReader, _ := gzip.NewReader(file)
byts, _ := ioutil.ReadAll(gzReader)
imgReader := bytes.NewReader(byts)
img, _, err := image.Decode(imgReader)
if err != nil {
log.Fatal(err)
}
b.ResetTimer()
cf := ColorFinder{}
for i := 0; i < b.N; i++ {
col, err := cf.FindMainColor(img)
if err != nil {
b.Errorf("Unexpected error: %#v", err)
}
if ColorToHex(col) != "cb1c1f" {
b.Errorf("Wrong color: %s", ColorToHex(col))
}
imgReader.Seek(0, 0)
}
}
func BenchmarkFindMainColor57x57(b *testing.B) {
file, _ := os.Open(testdataDir + "icon07.png.gz")
gzReader, _ := gzip.NewReader(file)
byts, _ := ioutil.ReadAll(gzReader)
imgReader := bytes.NewReader(byts)
img, _, err := image.Decode(imgReader)
if err != nil {
log.Fatal(err)
}
b.ResetTimer()
cf := ColorFinder{}
for i := 0; i < b.N; i++ {
col, err := cf.FindMainColor(img)
if err != nil {
b.Errorf("Unexpected error: %#v", err)
}
if ColorToHex(col) != "e61a30" {
b.Errorf("Wrong color: %s", ColorToHex(col))
}
imgReader.Seek(0, 0)
}
}
const testdataDir = "testdata/"
func assertFindsRightColor(t *testing.T, fileName string, expectedHexColor string) {
var imgReader io.ReadCloser
path := testdataDir + fileName
imgReader, err := os.Open(path)
check(t, err)
if strings.HasSuffix(path, ".gz") {
imgReader, err = gzip.NewReader(imgReader)
check(t, err)
}
defer imgReader.Close()
img, _, err := image.Decode(imgReader)
if err != nil {
log.Fatal(err)
}
cf := ColorFinder{}
actualColor, err := cf.FindMainColor(img)
check(t, err)
assertEquals(t, expectedHexColor, ColorToHex(actualColor))
}
func check(t *testing.T, err error) {
if err != nil {
fail(t, fmt.Sprintf("Unexpected error: %#v", err))
}
}
func assertEquals(t *testing.T, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
fail(t, fmt.Sprintf("Not equal: %#v (expected)\n"+
" != %#v (actual)", expected, actual))
}
}
func fail(t *testing.T, failureMessage string) {
t.Errorf("\t%s\n"+
"\r\t",
failureMessage)
}