Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions draw2dsvg/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
)

func toSvgRGBA(c color.Color) string {
r, g, b, a := c.RGBA()
r, g, b, a = r>>8, g>>8, b>>8, a>>8
if a == 255 {
return optiSprintf("#%02X%02X%02X", r, g, b)
// SVG expects straight RGB channels even when the paint is translucent.
nrgba := color.NRGBAModel.Convert(c).(color.NRGBA)
if nrgba.A == 255 {
return optiSprintf("#%02X%02X%02X", nrgba.R, nrgba.G, nrgba.B)
}
return optiSprintf("rgba(%v,%v,%v,%f)", r, g, b, float64(a)/255)
return optiSprintf("rgba(%v,%v,%v,%f)", nrgba.R, nrgba.G, nrgba.B, float64(nrgba.A)/255)
}

func toSvgLength(l float64) string {
Expand Down
19 changes: 19 additions & 0 deletions draw2dsvg/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package draw2dsvg

import (
"encoding/xml"
"image/color"
"testing"
)

Expand Down Expand Up @@ -106,3 +107,21 @@ func TestXml_ViewBox(t *testing.T) {
svg.ViewBox = "0 100 -10 -100"
t.Run("with viewBox", subtest)
}

func TestTranslucentFillAndStroke(t *testing.T) {
svg := NewSvg()
gc := NewGraphicContext(svg)
gc.SetFillColor(color.NRGBA{R: 255, A: 128})
gc.SetStrokeColor(color.RGBA{B: 128, A: 128})
gc.MoveTo(1, 1)
gc.LineTo(4, 4)
gc.FillStroke()

group := svg.Groups[0]
if got, want := group.Fill, "rgba(255,0,0,0.502)"; got != want {
t.Errorf("fill = %s, want %s", got, want)
}
if got, want := group.Stroke, "rgba(0,0,255,0.502)"; got != want {
t.Errorf("stroke = %s, want %s", got, want)
}
}