-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path17_chessboard.go
More file actions
69 lines (60 loc) · 1.63 KB
/
17_chessboard.go
File metadata and controls
69 lines (60 loc) · 1.63 KB
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
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Čtrnáctá část
// Programovací jazyk Go a počítačová grafika (úvod)
// https://www.root.cz/clanky/programovaci-jazyk-go-a-pocitacova-grafika-uvod/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů ze čtrnácté části:
// https://github.com/tisnik/go-root/blob/master/article_14/README.md
//
// Demonstrační příklad číslo 17:
// Využití balíčku draw pro vykreslení šachovnice (rastrové operace)
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_14/17_chessboard.html
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const width = 256
const height = 256
func main() {
img := image.NewRGBA(image.Rect(0, 0, width, height))
outfile, err := os.Create("17.png")
if err != nil {
panic(err)
}
defer outfile.Close()
palette := make(map[int]color.RGBA, 2)
palette[0] = color.RGBA{150, 205, 50, 255}
palette[1] = color.RGBA{0, 100, 0, 255}
indexColor := 0
boardSize := 8
horizontalBlock := int(width / boardSize)
verticalBlock := int(height / boardSize)
xFrom := 0
xTo := horizontalBlock
for x := 0; x < boardSize; x++ {
yFrom := 0
yTo := verticalBlock
for y := 0; y < boardSize; y++ {
r := image.Rect(xFrom, yFrom, xTo, yTo)
draw.Draw(img, r, &image.Uniform{palette[indexColor]}, image.ZP, draw.Src)
yFrom = yTo
yTo += verticalBlock
indexColor = 1 - indexColor
}
xFrom = xTo
xTo += horizontalBlock
indexColor = 1 - indexColor
}
png.Encode(outfile, img)
}