Documentation ¶
Overview ¶
Package chessboard provides functions to render a chess position as an image.
To use this package, you will probably also need the chess package (https://gitlab.com/eightsquared/chess) to describe games. For example, supposing you have the following board:
// White to mate in one move. g := chess.Game{ Board: chess.Board{ chess.E8: chess.BlackBishop, chess.F8: chess.BlackKing, chess.A7: chess.BlackPawn, chess.B7: chess.BlackPawn, chess.C7: chess.BlackPawn, chess.G7: chess.BlackPawn, chess.C6: chess.BlackKnight, chess.D6: chess.BlackPawn, chess.H6: chess.BlackPawn, chess.C5: chess.BlackBishop, chess.C4: chess.WhiteBishop, chess.F4: chess.WhiteBishop, chess.G4: chess.BlackKnight, chess.H4: chess.BlackQueen, chess.C3: chess.WhiteKnight, chess.A2: chess.WhitePawn, chess.B2: chess.WhitePawn, chess.G2: chess.WhitePawn, chess.H2: chess.WhitePawn, chess.E1: chess.WhiteRook, chess.F1: chess.WhiteRook, chess.H1: chess.WhiteKing, }, ToMove: chess.White, }
You can write an ASCII-art visualization of this position to standard output as follows:
err := chessboard.WriteSVG(os.Stdout, chessboard.Defaults) // Output: // // ╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗ // 8 ║ │ │ │ │ b │ k │ │ ║ // ╟───┼───┼───┼───┼───┼───┼───┼───╢ // 7 ║ p │ p │ p │ │ │ │ p │ ║ // ╟───┼───┼───┼───┼───┼───┼───┼───╢ // 6 ║ │ │ n │ p │ │ │ │ p ║ // ╟───┼───┼───┼───┼───┼───┼───┼───╢ // 5 ║ │ │ b │ │ │ │ │ ║ // ╟───┼───┼───┼───┼───┼───┼───┼───╢ // 4 ║ │ │ B │ │ │ B │ n │ q ║ // ╟───┼───┼───┼───┼───┼───┼───┼───╢ // 3 ║ │ │ N │ │ │ │ │ ║ // ╟───┼───┼───┼───┼───┼───┼───┼───╢ // 2 ║ P │ P │ │ │ │ │ P │ P ║ // ╟───┼───┼───┼───┼───┼───┼───┼───╢ // 1 ║ │ │ │ │ R │ R │ │ K ║ // ╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝ // w a b c d e f g h
In addition to ASCII output, this package supports rendering to SVG (fast) and PNG (much slower due to raster scaling operations).
Index ¶
Constants ¶
const ( MinImageSize = 8 * 16 MaxImageSize = 8 * 256 MaxFontSize = 20 )
These constants establish bounds on the rendered image dimensions and the maximum label size.
Variables ¶
var Defaults = Options{ Size: 256, Light: color.RGBA{255, 206, 158, 255}, Dark: color.RGBA{209, 139, 71, 255}, Rotate: None, Style: DefaultStyle, }
Defaults holds a set of reasonable default options.
Functions ¶
func WriteASCII ¶
WriteASCII renders a visual representation of the game g to w in ASCII art format.
This function does not yet support board rotation.
Types ¶
type Options ¶
type Options struct { // Size sets the image width and height in pixels. Size int // Light holds the color used to draw light-colored squares. Light color.RGBA // Dark holds the color used to draw dark-colored squares. Dark color.RGBA // Rotate describes how to transform (typically rotate) the board. (Piece // images and square labels are not affected by rotation.) Rotate Rotation // Style controls the look of the piece images. Style PieceStyle }
Options describes the options that may be applied to a rendered image.
type PieceStyle ¶
type PieceStyle uint8
PieceStyle enumerates the allowed piece styles.
const ( Lasker PieceStyle = iota Merida DefaultStyle PieceStyle = Lasker )
Lasker is an open-source style found at https://gitlab.com/eightsquared/lasker. Merida is derived from a free font of the same name.
func (PieceStyle) String ¶
func (s PieceStyle) String() string
type Rotation ¶
type Rotation uint8
Rotation enumerates the allowed rotational transforms that can be applied to a rendered board.
By default, no rotation is applied to the board. Clockwise and CounterCllockwise indicate that the board should be rotated one quarter-turn in the clockwise and counter-clockwise directions, respectively. Half indicates that the board should be rotated one half-turn, which effectively draws the board from Black's point of view.