set root fill as transparent on pdf exports

This commit is contained in:
Bernard Xie 2023-02-27 16:05:41 -08:00
parent a14871c81f
commit 66083e2d98
No known key found for this signature in database
GPG key ID: 3C3E0036CE0F892C
3 changed files with 9 additions and 5 deletions

View file

@ -6,7 +6,6 @@ import (
"regexp"
"strconv"
"github.com/davecgh/go-spew/spew"
"github.com/lucasb-eyer/go-colorful"
"github.com/mazznoer/csscolorparser"
)
@ -161,7 +160,6 @@ func (c *RGB) IsLight() bool {
hsp := math.Sqrt(0.299*math.Pow(r, 2) + 0.587*math.Pow(g, 2) + 0.114*math.Pow(b, 2))
spew.Dump(hsp)
return hsp > 130
}
@ -170,9 +168,10 @@ func Hex2RGB(hex string) (RGB, error) {
var rgb RGB
if hex[0:1] == "#" {
hex = hex[1:]
} else {
return RGB{}, nil
}
values, err := strconv.ParseUint(hex, 16, 32)
if err != nil {
return RGB{}, err
}

View file

@ -34,7 +34,7 @@ func Init() *GoFPDF {
}
func (g *GoFPDF) GetFillRGB(fill string) (color.RGB, error) {
if fill == "" {
if fill == "" || strings.ToLower(fill) == "transparent" {
return color.RGB{
Red: 255,
Green: 255,

View file

@ -458,6 +458,11 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, ske
}
if !diagram.IsFolderOnly {
rootFill := diagram.Root.Fill
// gofpdf will print the png img with a slight filter
// strip out the background fill within the png so that the background is uniform in the exported pdf
diagram.Root.Fill = "transparent"
svg, err = d2svg.Render(diagram, &d2svg.RenderOpts{
Pad: int(pad),
Sketch: sketch,
@ -484,7 +489,7 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, ske
return svg, err
}
err = pdf.AddPDFPage(pngImg, currBoardPath, diagram.Root.Fill)
err = pdf.AddPDFPage(pngImg, currBoardPath, rootFill)
if err != nil {
return svg, err
}