Skip to content
This repository was archived by the owner on Jan 25, 2025. It is now read-only.

Commit e70b770

Browse files
authored
added opacity to mark squares (#148)
1 parent 281c4f3 commit e70b770

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

Diff for: image/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ image.SVG(file, pos.Board())
2424
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.
2525

2626
```go
27-
white := color.RGBA{255, 255, 255, 1}
28-
gray := color.RGBA{120, 120, 120, 1}
27+
white := color.RGBA{255, 255, 255, 255}
28+
gray := color.RGBA{120, 120, 120, 255}
2929
sqrs := image.SquareColors(white, gray)
3030
image.SVG(file, pos.Board(), sqrs)
3131
```
@@ -35,7 +35,7 @@ image.SVG(file, pos.Board(), sqrs)
3535
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.
3636

3737
```go
38-
yellow := color.RGBA{255, 255, 0, 1}
38+
yellow := color.RGBA{255, 255, 0, 51}
3939
mark := image.MarkSquares(yellow, chess.D2, chess.D4)
4040
image.SVG(file, pos.Board(), mark)
4141
```
@@ -82,7 +82,7 @@ func main() {
8282
}
8383

8484
// write board SVG to file
85-
yellow := color.RGBA{255, 255, 0, 1}
85+
yellow := color.RGBA{255, 255, 0, 51}
8686
mark := image.MarkSquares(yellow, chess.D2, chess.D4)
8787
arrows := image.MarkArrows(image.Arrow(chess.D2, chess.D4))
8888
if err := image.SVG(f, pos.Board(), mark, arrows); err != nil {

Diff for: image/image.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ func (e *Encoder) EncodeSVG(b *chess.Board) error {
152152
canvas.Rect(x, y, sqWidth, sqHeight, "fill: "+colorToHex(c))
153153
markColor, ok := e.marks[sq]
154154
if ok {
155-
canvas.Rect(x, y, sqWidth, sqHeight, "fill-opacity:0.2;fill: "+colorToHex(markColor))
155+
_, _, _, a := markColor.RGBA()
156+
opacity := float64(a) / 0xffff
157+
canvas.Rect(x, y, sqWidth, sqHeight, fmt.Sprintf("fill-opacity:%f;fill: %s", opacity, colorToHex(markColor)))
156158
}
157159
// draw piece
158160
p := boardMap[sq]

Diff for: image/image_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/notnil/chess/image"
1313
)
1414

15-
const expectedMD5 = "a2ee66ca19e4c347aec41371c1ca07f8"
16-
const expectedMD5Black = "ce4d4e033a50678898c62928b8e0a15c"
15+
const expectedMD5 = "625c63ef80796a3485004952a18c9b25"
16+
const expectedMD5Black = "1988104209a5401be2f645967fadec78"
1717
const expectedMD5KnightsAndDiagonalArrows = "9c95aa56cec67be2ceee141f259753f7"
1818

1919
func TestSVG(t *testing.T) {
@@ -24,7 +24,7 @@ func TestSVG(t *testing.T) {
2424
if err := pos.UnmarshalText([]byte(fenStr)); err != nil {
2525
t.Error(err)
2626
}
27-
mark := image.MarkSquares(color.RGBA{255, 255, 0, 1}, chess.D2, chess.D4)
27+
mark := image.MarkSquares(color.RGBA{255, 255, 0, 100}, chess.D2, chess.D4)
2828
arrows := image.MarkArrows(image.Arrow(chess.D2, chess.D4))
2929
if err := image.SVG(buf, pos.Board(), mark, arrows); err != nil {
3030
t.Error(err)
@@ -46,7 +46,7 @@ func TestSVGFromBlack(t *testing.T) {
4646
if err := pos.UnmarshalText([]byte(fenStr)); err != nil {
4747
t.Error(err)
4848
}
49-
mark := image.MarkSquares(color.RGBA{255, 255, 0, 1}, chess.D2, chess.D4)
49+
mark := image.MarkSquares(color.RGBA{255, 255, 0, 51}, chess.D2, chess.D4)
5050
arrows := image.MarkArrows(image.Arrow(chess.D2, chess.D4).WithColor(color.Black))
5151
per := image.Perspective(chess.Black)
5252
if err := image.SVG(buf, pos.Board(), mark, arrows, per); err != nil {

0 commit comments

Comments
 (0)