-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_test.go
350 lines (327 loc) · 8.6 KB
/
context_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package gg
import (
"image/color"
"math/rand"
"testing"
)
func checkHash(t *testing.T, dc *Context, expected string) {
actual := dc.String()
if actual != expected {
t.Fatalf("expected hash: %s != actual hash: %s", expected, actual)
}
}
func saveImage(dc *Context, name string) error {
return SavePNG(name+".png", dc.Image())
}
func TestBlank(t *testing.T) {
dc := NewContext(100, 100)
saveImage(dc, "TestBlank")
checkHash(t, dc, "<gg.Context 4e0a293a5b638f0aba2c4fe2c3418d0e>")
}
func TestGrid(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(1, 1, 1)
dc.Clear()
for i := 10; i < 100; i += 10 {
x := float64(i) + 0.5
dc.DrawLine(x, 0, x, 100)
dc.DrawLine(0, x, 100, x)
}
dc.SetRGB(0, 0, 0)
dc.Stroke()
saveImage(dc, "TestGrid")
checkHash(t, dc, "<gg.Context 78606adda71d8abfbd8bb271087e4d69>")
}
func TestLines(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(0.5, 0.5, 0.5)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < 100; i++ {
x1 := rnd.Float64() * 100
y1 := rnd.Float64() * 100
x2 := rnd.Float64() * 100
y2 := rnd.Float64() * 100
dc.DrawLine(x1, y1, x2, y2)
dc.SetLineWidth(rnd.Float64() * 3)
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Stroke()
}
saveImage(dc, "TestLines")
checkHash(t, dc, "<gg.Context 1dfa8c19ba801bf5af2e464aa1f46518>")
}
func TestCircles(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(1, 1, 1)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < 10; i++ {
x := rnd.Float64() * 100
y := rnd.Float64() * 100
r := rnd.Float64()*10 + 5
dc.DrawCircle(x, y, r)
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.FillPreserve()
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.SetLineWidth(rnd.Float64() * 3)
dc.Stroke()
}
saveImage(dc, "TestCircles")
checkHash(t, dc, "<gg.Context 1f694525d7ff55d99a81646f827168d7>")
}
func TestQuadratic(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(0.25, 0.25, 0.25)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < 100; i++ {
x1 := rnd.Float64() * 100
y1 := rnd.Float64() * 100
x2 := rnd.Float64() * 100
y2 := rnd.Float64() * 100
x3 := rnd.Float64() * 100
y3 := rnd.Float64() * 100
dc.MoveTo(x1, y1)
dc.QuadraticTo(x2, y2, x3, y3)
dc.SetLineWidth(rnd.Float64() * 3)
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Stroke()
}
saveImage(dc, "TestQuadratic")
checkHash(t, dc, "<gg.Context 87eec03808f687bd012a10752c61cdc8>")
}
func BenchmarkQuadratic(b *testing.B) {
dc := NewContext(100, 100)
dc.SetRGB(0.25, 0.25, 0.25)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
b.ResetTimer()
for i := 0; i < b.N; i++ {
x1 := rnd.Float64() * 100
y1 := rnd.Float64() * 100
x2 := rnd.Float64() * 100
y2 := rnd.Float64() * 100
x3 := rnd.Float64() * 100
y3 := rnd.Float64() * 100
dc.MoveTo(x1, y1)
dc.QuadraticTo(x2, y2, x3, y3)
dc.SetLineWidth(rnd.Float64() * 3)
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Stroke()
}
}
func TestCubic(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(0.75, 0.75, 0.75)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < 100; i++ {
x1 := rnd.Float64() * 100
y1 := rnd.Float64() * 100
x2 := rnd.Float64() * 100
y2 := rnd.Float64() * 100
x3 := rnd.Float64() * 100
y3 := rnd.Float64() * 100
x4 := rnd.Float64() * 100
y4 := rnd.Float64() * 100
dc.MoveTo(x1, y1)
dc.CubicTo(x2, y2, x3, y3, x4, y4)
dc.SetLineWidth(rnd.Float64() * 3)
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Stroke()
}
saveImage(dc, "TestCubic")
checkHash(t, dc, "<gg.Context ff87a762b42863198c78c923b17033d8>")
}
func BenchmarkCubic(b *testing.B) {
dc := NewContext(100, 100)
dc.SetRGB(0.75, 0.75, 0.75)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
b.ResetTimer()
for i := 0; i < b.N; i++ {
x1 := rnd.Float64() * 100
y1 := rnd.Float64() * 100
x2 := rnd.Float64() * 100
y2 := rnd.Float64() * 100
x3 := rnd.Float64() * 100
y3 := rnd.Float64() * 100
x4 := rnd.Float64() * 100
y4 := rnd.Float64() * 100
dc.MoveTo(x1, y1)
dc.CubicTo(x2, y2, x3, y3, x4, y4)
dc.SetLineWidth(rnd.Float64() * 3)
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Stroke()
}
}
func TestFill(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(1, 1, 1)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < 10; i++ {
dc.NewSubPath()
for j := 0; j < 10; j++ {
x := rnd.Float64() * 100
y := rnd.Float64() * 100
dc.LineTo(x, y)
}
dc.ClosePath()
dc.SetRGBA(rnd.Float64(), rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Fill()
}
saveImage(dc, "TestFill")
checkHash(t, dc, "<gg.Context 0c7c69398ab5c717a6277e79427e5bb6>")
}
func TestClip(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(1, 1, 1)
dc.Clear()
dc.DrawCircle(50, 50, 40)
dc.Clip()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < 1000; i++ {
x := rnd.Float64() * 100
y := rnd.Float64() * 100
r := rnd.Float64()*10 + 5
dc.DrawCircle(x, y, r)
dc.SetRGBA(rnd.Float64(), rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Fill()
}
saveImage(dc, "TestClip")
checkHash(t, dc, "<gg.Context bb4dd1b0fbbfb82fa27862cbea015582>")
}
func TestPushPop(t *testing.T) {
const S = 100
dc := NewContext(S, S)
dc.SetRGBA(0, 0, 0, 0.1)
for i := 0; i < 360; i += 15 {
dc.Push()
dc.RotateAbout(Radians(float64(i)), S/2, S/2)
dc.DrawEllipse(S/2, S/2, S*7/16, S/8)
dc.Fill()
dc.Pop()
}
saveImage(dc, "TestPushPop")
checkHash(t, dc, "<gg.Context 98813dcbd31ca163aed034743cdb1918>")
}
func TestDrawStringWrapped(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(1, 1, 1)
dc.Clear()
dc.SetRGB(0, 0, 0)
dc.DrawStringWrapped("Hello, world! How are you?", 50, 50, 0.5, 0.5, 90, 1.5, AlignCenter)
saveImage(dc, "TestDrawStringWrapped")
checkHash(t, dc, "<gg.Context 8d92f6aae9e8b38563f171abd00893f8>")
}
func TestDrawImage(t *testing.T) {
src := NewContext(100, 100)
src.SetRGB(1, 1, 1)
src.Clear()
for i := 10; i < 100; i += 10 {
x := float64(i) + 0.5
src.DrawLine(x, 0, x, 100)
src.DrawLine(0, x, 100, x)
}
src.SetRGB(0, 0, 0)
src.Stroke()
dc := NewContext(200, 200)
dc.SetRGB(0, 0, 0)
dc.Clear()
dc.DrawImage(src.Image(), 50, 50)
saveImage(dc, "TestDrawImage")
checkHash(t, dc, "<gg.Context 282afbc134676722960b6bec21305b15>")
}
func TestSetPixel(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(0, 0, 0)
dc.Clear()
dc.SetRGB(0, 1, 0)
i := 0
for y := 0; y < 100; y++ {
for x := 0; x < 100; x++ {
if i%31 == 0 {
dc.SetPixel(x, y)
}
i++
}
}
saveImage(dc, "TestSetPixel")
checkHash(t, dc, "<gg.Context 27dda6b4b1d94f061018825b11982793>")
}
func TestDrawPoint(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(0, 0, 0)
dc.Clear()
dc.SetRGB(0, 1, 0)
dc.Scale(10, 10)
for y := 0; y <= 10; y++ {
for x := 0; x <= 10; x++ {
dc.DrawPoint(float64(x), float64(y), 3)
dc.Fill()
}
}
saveImage(dc, "TestDrawPoint")
checkHash(t, dc, "<gg.Context a4e7546ed558cdf186e00fb6716b91bc>")
}
func TestLinearGradient(t *testing.T) {
dc := NewContext(100, 100)
g := NewLinearGradient(0, 0, 100, 100)
g.AddColorStop(0, color.RGBA{0, 255, 0, 255})
g.AddColorStop(1, color.RGBA{0, 0, 255, 255})
g.AddColorStop(0.5, color.RGBA{255, 0, 0, 255})
dc.SetFillStyle(g)
dc.DrawRectangle(0, 0, 100, 100)
dc.Fill()
saveImage(dc, "TestLinearGradient")
checkHash(t, dc, "<gg.Context 75eb9385c1219b1d5bb6f4c961802c7a>")
}
func TestRadialGradient(t *testing.T) {
dc := NewContext(100, 100)
g := NewRadialGradient(30, 50, 0, 70, 50, 50)
g.AddColorStop(0, color.RGBA{0, 255, 0, 255})
g.AddColorStop(1, color.RGBA{0, 0, 255, 255})
g.AddColorStop(0.5, color.RGBA{255, 0, 0, 255})
dc.SetFillStyle(g)
dc.DrawRectangle(0, 0, 100, 100)
dc.Fill()
saveImage(dc, "TestRadialGradient")
checkHash(t, dc, "<gg.Context f170f39c3f35c29de11e00428532489d>")
}
func TestDashes(t *testing.T) {
dc := NewContext(100, 100)
dc.SetRGB(1, 1, 1)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < 100; i++ {
x1 := rnd.Float64() * 100
y1 := rnd.Float64() * 100
x2 := rnd.Float64() * 100
y2 := rnd.Float64() * 100
dc.SetDash(rnd.Float64()*3+1, rnd.Float64()*3+3)
dc.DrawLine(x1, y1, x2, y2)
dc.SetLineWidth(rnd.Float64() * 3)
dc.SetRGB(rnd.Float64(), rnd.Float64(), rnd.Float64())
dc.Stroke()
}
saveImage(dc, "TestDashes")
checkHash(t, dc, "<gg.Context 2c4c6e23ae4219f1dbfd8c3ea5a8be68>")
}
func BenchmarkCircles(b *testing.B) {
dc := NewContext(1000, 1000)
dc.SetRGB(1, 1, 1)
dc.Clear()
rnd := rand.New(rand.NewSource(99))
for i := 0; i < b.N; i++ {
x := rnd.Float64() * 1000
y := rnd.Float64() * 1000
dc.DrawCircle(x, y, 10)
if i%2 == 0 {
dc.SetRGB(0, 0, 0)
} else {
dc.SetRGB(1, 1, 1)
}
dc.Fill()
}
}