-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path08_gif_animation_blink.go
72 lines (60 loc) · 1.64 KB
/
08_gif_animation_blink.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
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Patnáctá část
// Programovací jazyk Go a grafika: tvorba animovaných GIFů, grafická knihovna GG
// https://www.root.cz/clanky/programovaci-jazyk-go-a-grafika-tvorba-animovanych-gifu-graficka-knihovna-gg/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z patnácté části:
// https://github.com/tisnik/go-root/blob/master/article_15/README.md
//
// Demonstrační příklad číslo 8:
// Jednoduchá animace se dvěma snímky o velikosti 32×32 pixelů
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_15/08_gif_animation_blink.html
package main
import (
"image"
"image/color"
"image/draw"
"image/gif"
"os"
)
// BoardSize represents size of chessboard being rendered
const BoardSize = 8
func CreateImage(width int, height int, paletteIndex int) *image.Paletted {
var palette []color.Color
if paletteIndex == 0 {
palette = []color.Color{
color.RGBA{150, 150, 150, 255},
}
} else {
palette = []color.Color{
color.RGBA{250, 0, 0, 255},
}
}
img := image.NewPaletted(image.Rect(0, 0, width, height), palette)
r := image.Rect(0, 0, width, height)
draw.Draw(img, r, &image.Uniform{palette[0]}, image.ZP, draw.Src)
return img
}
func main() {
images := []*image.Paletted{
CreateImage(32, 32, 0),
CreateImage(32, 32, 1),
}
delays := []int{100, 100}
outfile, err := os.Create("08.gif")
if err != nil {
panic(err)
}
defer outfile.Close()
gif.EncodeAll(outfile, &gif.GIF{
Image: images,
Delay: delays,
})
}