chessimage

package module
v0.0.0-...-e8cc60f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2023 License: MIT Imports: 11 Imported by: 1

README

chessimage

chessimage is a golang library for rendering a chess board PNG in specific state.

GoDoc

go run examples/starting_board.go | open -f -a /Applications/Preview.app/

Basic Usage

Include in your go path.

go get github.com/cjsaylor/chessimage

Initialize the renderer with a FEN notation.

board, _ := chessimage.NewRendererFromFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")

Render the chess board to a png image.Image interface.

f, _ := os.Create("board.png")
defer f.Close()
image, _ := board.Render(chessimage.Options{AssetPath: "./assets/")})
png.Encode(f, image)

Highlighting LastMove

You can highlight tiles of where a move started and ended.

board.SetLastMove(chessimage.LastMove{
	From: chessimage.E4,
	To: chessimage.E2,
})

Example

Mark Checked

You can highlight a tile as "checked".

board.SetCheckTile(chessimage.G1)

Example

Options

You can define rendering options at render time:

options := chessimage.Options{
	AssetPath: "./assets/"
}
renderer.Render(options)
AssetPath (Required)

Specify the path of the image assets for the individual pieces. Feel free to use the assets packaged in this repo, but be aware they are under CC license.

Inverted (false)

Invert the board so that it displays correctly for the black player. By default the white player view is rendered. This is a boolean option.

Resizer (draw.CatmullRom)

Change the algorhythm for asset resizing. Depending on your performance requirements, you may need to use a faster (but more lossy) resizing method (like draw.NearestNeighbor).

BoardSize (512)

Square board size in pixels

PieceRatio (0.8)

Size of the pieces relative as a percentage to the game board tile size. If the game board size is 800, each board tile would be 100 pixels wide, and the pieces would render at 80 pixels with the default ratio.

Todo

  • Add support for PGN notation for rendering a board (similar to the FEN notation setup now)
  • Add configuration support for changing board and tile highlight colors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LastMove

type LastMove struct {
	From Tile
	To   Tile
}

LastMove represents two tiles that indicate a piece was moved

type Options

type Options struct {
	FileSystem fs.FS
	AssetPath  string
	Resizer    draw.Scaler
	BoardSize  int
	PieceRatio float64
	Inverted   bool
}

Options holds all possible rendering options for customization

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer is responsible for rendering the board, pieces, rank/file, and tile highlights

func NewRendererFromFEN

func NewRendererFromFEN(fen string) (*Renderer, error)

NewRendererFromFEN prepares a renderer for use with given FEN string

func (*Renderer) Render

func (r *Renderer) Render(options Options) (image.Image, error)

Render the chess board with given items

func (*Renderer) SetCheckTile

func (r *Renderer) SetCheckTile(tile Tile)

SetCheckTile - Sets the check tile

func (*Renderer) SetLastMove

func (r *Renderer) SetLastMove(lastMove LastMove)

SetLastMove - Sets the last move

type Tile

type Tile int8

Tile represents a specific position of a tile on a chess board

const (
	NoTile Tile = iota - 1
	A8
	B8
	C8
	D8
	E8
	F8
	G8
	H8
	A7
	B7
	C7
	D7
	E7
	F7
	G7
	H7
	A6
	B6
	C6
	D6
	E6
	F6
	G6
	H6
	A5
	B5
	C5
	D5
	E5
	F5
	G5
	H5
	A4
	B4
	C4
	D4
	E4
	F4
	G4
	H4
	A3
	B3
	C3
	D3
	E3
	F3
	G3
	H3
	A2
	B2
	C2
	D2
	E2
	F2
	G2
	H2
	A1
	B1
	C1
	D1
	E1
	F1
	G1
	H1
)

func TileFromAN

func TileFromAN(an string) (Tile, error)

TileFromAN will attempt to get a tile by its algebraic notation (ie: "e5")

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL