-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
55 lines (48 loc) · 1.33 KB
/
draw.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
package pic
import (
"image"
"image/draw"
"github.com/xopoww/chess2pic/pkg/chess"
)
// DrawPosition creates a draw.Image from Position using Collection.
// If Collection is a CanvasCollection, its Canvas() method is used to create resulting image,
// otherwise image.NewRGBA() is used.
func DrawPosition(col Collection, pos chess.Position, fromPerspective chess.PieceColor) draw.Image {
var dst draw.Image
if ccol, ok := col.(CanvasCollection); ok {
dst = ccol.Canvas()
} else {
dst = image.NewRGBA(col.Board(fromPerspective).Bounds())
}
if dst.Bounds() != col.Board(fromPerspective).Bounds() {
panic("invalid dst bounds")
}
draw.Draw(dst, dst.Bounds(), col.Board(fromPerspective), image.Pt(0, 0), draw.Over)
bs := dst.Bounds().Dx()
ss := bs / 8
ps := col.Piece(chess.Piece{Color: chess.White, Kind: chess.Pawn}).Bounds().Dx()
off := (ss - ps) / 2
for file := 0; file < 8; file++ {
for rank := 0; rank < 8; rank++ {
sq := chess.MustNewSquare(file, rank)
p := pos.Get(sq)
if p.Kind == chess.None {
continue
}
img := col.Piece(p)
var (
x int
y int
)
if fromPerspective == chess.White {
x = file*ss + off
y = (7-rank)*ss + off
} else {
x = (7-file)*ss + off
y = rank*ss + off
}
draw.Draw(dst, image.Rect(x, y, x+ps, y+ps), img, image.Pt(0, 0), draw.Over)
}
}
return dst
}