Skip to content

Commit

Permalink
export encoder (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
theletterjeff authored Nov 25, 2024
1 parent 6d754fa commit c26788f
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
// SVG writes the board SVG representation into the writer.
// An error is returned if there is there is an error writing data.
// SVG also takes options which can customize the image output.
func SVG(w io.Writer, b *chess.Board, opts ...func(*encoder)) error {
func SVG(w io.Writer, b *chess.Board, opts ...func(*Encoder)) error {
e := new(w, opts)
return e.EncodeSVG(b)
}

// SquareColors is designed to be used as an optional argument
// to the SVG function. It changes the default light and
// dark square colors to the colors given.
func SquareColors(light, dark color.Color) func(*encoder) {
return func(e *encoder) {
func SquareColors(light, dark color.Color) func(*Encoder) {
return func(e *Encoder) {
e.light = light
e.dark = dark
}
Expand All @@ -34,8 +34,8 @@ func SquareColors(light, dark color.Color) func(*encoder) {
// to the SVG function. It marks the given squares with the
// color. A possible usage includes marking squares of the
// previous move.
func MarkSquares(c color.Color, sqs ...chess.Square) func(*encoder) {
return func(e *encoder) {
func MarkSquares(c color.Color, sqs ...chess.Square) func(*Encoder) {
return func(e *Encoder) {
for _, sq := range sqs {
e.marks[sq] = c
}
Expand All @@ -45,14 +45,14 @@ func MarkSquares(c color.Color, sqs ...chess.Square) func(*encoder) {
// Perspective is designed to be used as an optional argument
// to the SVG function. It draws the board from the perspective
// of the given color. White is the default.
func Perspective(c chess.Color) func(*encoder) {
return func(e *encoder) {
func Perspective(c chess.Color) func(*Encoder) {
return func(e *Encoder) {
e.perspective = c
}
}

// A Encoder encodes chess boards into images.
type encoder struct {
type Encoder struct {
w io.Writer
light color.Color
dark color.Color
Expand All @@ -63,8 +63,8 @@ type encoder struct {
// New returns an encoder that writes to the given writer.
// New also takes options which can customize the image
// output.
func new(w io.Writer, options []func(*encoder)) *encoder {
e := &encoder{
func new(w io.Writer, options []func(*Encoder)) *Encoder {
e := &Encoder{
w: w,
light: color.RGBA{235, 209, 166, 1},
dark: color.RGBA{165, 117, 81, 1},
Expand Down Expand Up @@ -94,7 +94,7 @@ var (
// EncodeSVG writes the board SVG representation into
// the Encoder's writer. An error is returned if there
// is there is an error writing data.
func (e *encoder) EncodeSVG(b *chess.Board) error {
func (e *Encoder) EncodeSVG(b *chess.Board) error {
boardMap := b.SquareMap()
canvas := svg.New(e.w)
canvas.Start(boardWidth, boardHeight)
Expand Down Expand Up @@ -142,15 +142,15 @@ func (e *encoder) EncodeSVG(b *chess.Board) error {
return nil
}

func (e *encoder) colorForSquare(sq chess.Square) color.Color {
func (e *Encoder) colorForSquare(sq chess.Square) color.Color {
sqSum := int(sq.File()) + int(sq.Rank())
if sqSum%2 == 0 {
return e.dark
}
return e.light
}

func (e *encoder) colorForText(sq chess.Square) color.Color {
func (e *Encoder) colorForText(sq chess.Square) color.Color {
sqSum := int(sq.File()) + int(sq.Rank())
if sqSum%2 == 0 {
return e.light
Expand Down

0 comments on commit c26788f

Please sign in to comment.