-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
111 lines (94 loc) · 2.67 KB
/
renderer.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
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"image/jpeg"
"os"
"strings"
)
type BoardRenderer interface {
DrawCompleteBoard(board [8][8]byte, filename string, cellsize int)
}
type RasterBoardRenderer struct {
icons map[byte]*image.Image
}
func NewRasterBoardRenderer() *RasterBoardRenderer {
p := new(RasterBoardRenderer)
p.icons = make(map[byte]*image.Image)
p.loadIcons()
return p
}
func (r RasterBoardRenderer) loadIcons() {
for _, s := range validPieces {
var iconFile = "icons/" + string(s) + "60.png"
r.icons[s] = r.loadIcon(iconFile)
}
}
func (r RasterBoardRenderer) loadIcon(inputFilename string) *image.Image {
infile, err := os.Open(inputFilename)
if err != nil {
// replace this with real error handling
panic(err)
}
defer infile.Close()
// Decode will figure out what type of image is in the file on its own.
// We just have to be sure all the image packages we want are imported.
src, _, err := image.Decode(infile)
if err != nil {
// replace this with real error handling
panic(err)
}
return &src
}
func (r RasterBoardRenderer) rect(x1 int, y1, x2, y2 int, col color.RGBA, img *image.RGBA) {
for x := x1; x <= x2; x++ {
for y := y1; y <= y2; y++ {
img.Set(x, y, col)
}
}
}
func (r RasterBoardRenderer) drawPieces(board [8][8]byte, cellsize int, reverseBoard bool) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, 8*cellsize, 8*cellsize))
// Lichess colors : 8ca2ad and dee3e6
var whiteColor = color.RGBA{222, 227, 230, 255}
var blackColor = color.RGBA{140, 162, 173, 255}
var s = cellsize;
for x := 0; x < 8; x++ {
for y := 0; y < 8; y++ {
// drawCell
cellColor := blackColor
if (x+y)%2 == 0 {
cellColor = whiteColor
}
r.rect(x*s, y*s, (x+1)*s, (y+1)*s, cellColor, img)
// drawPiece
var piece = board[y][x]
if reverseBoard {
piece = board[7-y][7-x]
}
var _, isLoaded = r.icons[piece]
if isChessPieceOrPawn(piece) && isLoaded {
var coords = image.Point{-x * cellsize, -y * cellsize}
draw.Draw(img, img.Bounds(), *(r.icons[piece]), coords, draw.Over)
}
}
}
return img
}
func (r RasterBoardRenderer) DrawCompleteBoard(board [8][8]byte, filename string, cellsize int, reverse bool) {
img := r.drawPieces(board, cellsize, reverse)
if strings.Contains(strings.ToLower(filename), ".png") {
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
png.Encode(f, img)
} else if strings.Contains(strings.ToLower(filename), ".jpg") {
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
jpegOptions := jpeg.Options{100}
jpeg.Encode(f, img, &jpegOptions)
} else {
panic("invalid output file format")
}
}