Skip to content

Commit

Permalink
added opacity to mark squares
Browse files Browse the repository at this point in the history
  • Loading branch information
notnil committed Nov 25, 2024
1 parent b30c702 commit f036e3b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ image.SVG(file, pos.Board())
The default colors, shown in the example SVG below, are (235, 209, 166) for light squares and (165, 117, 81) for dark squares. The light and dark squares can be customized using the SquareColors() option.

```go
white := color.RGBA{255, 255, 255, 1}
gray := color.RGBA{120, 120, 120, 1}
white := color.RGBA{255, 255, 255, 255}
gray := color.RGBA{120, 120, 120, 255}
sqrs := image.SquareColors(white, gray)
image.SVG(file, pos.Board(), sqrs)
```
Expand All @@ -35,7 +35,7 @@ image.SVG(file, pos.Board(), sqrs)
MarkSquares is designed to be used as an optional argument to the SVG function. It marks the given squares with the color. A possible usage includes marking squares of the previous move.

```go
yellow := color.RGBA{255, 255, 0, 1}
yellow := color.RGBA{255, 255, 0, 51}
mark := image.MarkSquares(yellow, chess.D2, chess.D4)
image.SVG(file, pos.Board(), mark)
```
Expand Down Expand Up @@ -82,7 +82,7 @@ func main() {
}

// write board SVG to file
yellow := color.RGBA{255, 255, 0, 1}
yellow := color.RGBA{255, 255, 0, 51}
mark := image.MarkSquares(yellow, chess.D2, chess.D4)
arrows := image.MarkArrows(image.Arrow(chess.D2, chess.D4))
if err := image.SVG(f, pos.Board(), mark, arrows); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (e *Encoder) EncodeSVG(b *chess.Board) error {
canvas.Rect(x, y, sqWidth, sqHeight, "fill: "+colorToHex(c))
markColor, ok := e.marks[sq]
if ok {
canvas.Rect(x, y, sqWidth, sqHeight, "fill-opacity:0.2;fill: "+colorToHex(markColor))
_, _, _, a := markColor.RGBA()
opacity := float64(a) / 0xffff
canvas.Rect(x, y, sqWidth, sqHeight, fmt.Sprintf("fill-opacity:%f;fill: %s", opacity, colorToHex(markColor)))
}
// draw piece
p := boardMap[sq]
Expand Down
8 changes: 4 additions & 4 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/notnil/chess/image"
)

const expectedMD5 = "a2ee66ca19e4c347aec41371c1ca07f8"
const expectedMD5Black = "ce4d4e033a50678898c62928b8e0a15c"
const expectedMD5 = "625c63ef80796a3485004952a18c9b25"
const expectedMD5Black = "1988104209a5401be2f645967fadec78"
const expectedMD5KnightsAndDiagonalArrows = "9c95aa56cec67be2ceee141f259753f7"

func TestSVG(t *testing.T) {
Expand All @@ -24,7 +24,7 @@ func TestSVG(t *testing.T) {
if err := pos.UnmarshalText([]byte(fenStr)); err != nil {
t.Error(err)
}
mark := image.MarkSquares(color.RGBA{255, 255, 0, 1}, chess.D2, chess.D4)
mark := image.MarkSquares(color.RGBA{255, 255, 0, 100}, chess.D2, chess.D4)
arrows := image.MarkArrows(image.Arrow(chess.D2, chess.D4))
if err := image.SVG(buf, pos.Board(), mark, arrows); err != nil {
t.Error(err)
Expand All @@ -46,7 +46,7 @@ func TestSVGFromBlack(t *testing.T) {
if err := pos.UnmarshalText([]byte(fenStr)); err != nil {
t.Error(err)
}
mark := image.MarkSquares(color.RGBA{255, 255, 0, 1}, chess.D2, chess.D4)
mark := image.MarkSquares(color.RGBA{255, 255, 0, 51}, chess.D2, chess.D4)
arrows := image.MarkArrows(image.Arrow(chess.D2, chess.D4).WithColor(color.Black))
per := image.Perspective(chess.Black)
if err := image.SVG(buf, pos.Board(), mark, arrows, per); err != nil {
Expand Down

0 comments on commit f036e3b

Please sign in to comment.