forked from miekg/gobook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.go
68 lines (54 loc) · 1.14 KB
/
callback.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
// From: Jonathan Kans
package main
import (
"image"
"image/png"
"os"
)
func DrawSquare(left, top, base int, draw func(x, y int)) {
if draw == nil {
return
}
right := left + base
bottom := top + base
for x := left; x <= right; x++ {
draw(x, top)
draw(x, bottom)
}
for y := top; y <= bottom; y++ {
draw(left, y)
draw(right, y)
}
}
func main() {
var thickFrame bool
rgba := image.NewRGBA(300, 200)
if rgba == nil {
return
}
redColor := image.RGBAColor{255, 0, 0, 255}
blueColor := image.RGBAColor{0, 0, 255, 255}
pngFile, err := os.Open("squares.png", os.O_WRONLY|os.O_CREATE, 0644)
if pngFile == nil || err != nil {
return
}
defer pngFile.Close()
frameSquare := func(x, y int) {
// closure effortlessly passes local variables to callback
if thickFrame {
// draw 3 x 3 pixel block for thicker rectangle
for x0 := x - 1; x0 <= x+1; x0++ {
for y0 := y - 1; y0 <= y+1; y0++ {
rgba.Set(x0, y0, redColor)
}
}
} else {
rgba.Set(x, y, blueColor)
}
}
thickFrame = true
DrawSquare(30, 20, 50, frameSquare)
thickFrame = false
DrawSquare(100, 120, 35, frameSquare)
png.Encode(pngFile, rgba)
}