-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgray_test.go
97 lines (80 loc) · 2.16 KB
/
gray_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
package pixl
import (
"image"
_ "image/jpeg"
"testing"
)
func TestGrayLightness(t *testing.T) {
image := image.NewNRGBA(image.Rectangle{Max: image.Point{X: 1, Y: 1}})
inputColor, _ := parseHexColor("#FF000F")
image.Set(0, 0, inputColor)
out := Gray{Algorithm: GrayAlgorithms.Lightness}.Convert(image)
r, _, _, _ := out.At(0, 0).RGBA()
r = r >> 8
if expected := uint32(127); r != expected {
t.Errorf("Invalid output color, got: %d, want: %d.", r, expected)
}
}
func TestGrayAverage(t *testing.T) {
image := image.NewNRGBA(image.Rectangle{Max: image.Point{X: 1, Y: 1}})
inputColor, _ := parseHexColor("#FF000F")
image.Set(0, 0, inputColor)
out := Gray{Algorithm: GrayAlgorithms.Average}.Convert(image)
r, _, _, _ := out.At(0, 0).RGBA()
r = r >> 8
if expected := uint32(90); r != expected {
t.Errorf("Invalid output color, got: %d, want: %d.", r, expected)
}
}
func TestGrayLuminosity(t *testing.T) {
image := image.NewNRGBA(image.Rectangle{Max: image.Point{X: 1, Y: 1}})
inputColor, _ := parseHexColor("#7F7F7F")
image.Set(0, 0, inputColor)
out := Gray{Algorithm: GrayAlgorithms.Luminosity}.Convert(image)
r, _, _, _ := out.At(0, 0).RGBA()
r = r >> 8
if expected := uint32(0.21*127 + 0.72*127 + 0.07*127); r != expected {
t.Errorf("Invalid output color, got: %d, want: %d.", r, expected)
}
}
func generateImage() image.Image {
size := 10
image := image.NewNRGBA(image.Rectangle{Max: image.Point{X: size, Y: size}})
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
color, _ := parseHexColor("#0F011F")
image.Set(i, j, color)
}
}
return image
}
func BenchmarkGrayAverage(b *testing.B) {
b.StopTimer()
input := generateImage()
b.StartTimer()
for n := 0; n < b.N; n++ {
Gray{
Algorithm: GrayAlgorithms.Average,
}.Convert(input)
}
}
func BenchmarkGrayLuminosity(b *testing.B) {
b.StopTimer()
input := generateImage()
b.StartTimer()
for n := 0; n < b.N; n++ {
Gray{
Algorithm: GrayAlgorithms.Luminosity,
}.Convert(input)
}
}
func BenchmarkGrayLightness(b *testing.B) {
b.StopTimer()
input := generateImage()
b.StartTimer()
for n := 0; n < b.N; n++ {
Gray{
Algorithm: GrayAlgorithms.Lightness,
}.Convert(input)
}
}