Skip to content

Commit

Permalink
vgimg: Add option to specify image background color.
Browse files Browse the repository at this point in the history
Previously, the background color was always white, with no 
straightforward way to specify a transparent backgroun.
  • Loading branch information
ctessum committed Dec 6, 2018
1 parent f41a315 commit 32f6a97
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
26 changes: 21 additions & 5 deletions vg/vgimg/vgimg.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type Canvas struct {

// width is the current line width.
width vg.Length

// backgroundColor is the background color, set by
// UseBackgroundColor.
backgroundColor color.Color
}

const (
Expand All @@ -54,7 +58,7 @@ const (

// New returns a new image canvas.
func New(w, h vg.Length) *Canvas {
return NewWith(UseWH(w, h))
return NewWith(UseWH(w, h), UseBackgroundColor(color.White))
}

// NewWith returns a new image canvas created according to the specified
Expand All @@ -67,6 +71,7 @@ func New(w, h vg.Length) *Canvas {
// passed).
func NewWith(o ...option) *Canvas {
c := new(Canvas)
c.backgroundColor = color.White
var g uint32
for _, opt := range o {
f := opt(c)
Expand Down Expand Up @@ -101,7 +106,7 @@ func NewWith(o ...option) *Canvas {
c.gc.Scale(1, -1)
c.gc.Translate(0, -h)
}
draw.Draw(c.img, c.img.Bounds(), image.White, image.ZP, draw.Src)
draw.Draw(c.img, c.img.Bounds(), &image.Uniform{c.backgroundColor}, image.ZP, draw.Src)
c.color = []color.Color{color.Black}
vg.Initialize(c)
return c
Expand All @@ -111,8 +116,9 @@ func NewWith(o ...option) *Canvas {
// used when initializing a canvas are compatible with
// each other.
const (
setsDPI = 1 << iota
setsDPI uint32 = 1 << iota
setsSize
setsBackground
)

type option func(*Canvas) uint32
Expand Down Expand Up @@ -148,7 +154,7 @@ func UseDPI(dpi int) option {
func UseImage(img draw.Image) option {
return func(c *Canvas) uint32 {
c.img = img
return setsSize
return setsSize | setsBackground
}
}

Expand All @@ -161,7 +167,16 @@ func UseImageWithContext(img draw.Image, gc draw2d.GraphicContext) option {
c.img = img
c.gc = gc
c.dpi = gc.GetDPI()
return setsDPI | setsSize
return setsDPI | setsSize | setsBackground
}
}

// UseBackgroundColor specifies the image background color.
// Without UseBackgroundColor, the default color is white.
func UseBackgroundColor(c color.Color) option {
return func(canvas *Canvas) uint32 {
canvas.backgroundColor = c
return setsBackground
}
}

Expand Down Expand Up @@ -257,6 +272,7 @@ func (c *Canvas) outline(p vg.Path) {
}
}

// DPI returns the resolution of the receiver in pixels per inch.
func (c *Canvas) DPI() float64 {
return float64(c.gc.GetDPI())
}
Expand Down
18 changes: 18 additions & 0 deletions vg/vgimg/vgimg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ package vgimg_test

import (
"bytes"
"fmt"
"image/color"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"sync"
"testing"

Expand Down Expand Up @@ -70,3 +73,18 @@ func TestConcurrentInit(t *testing.T) {
}()
wg.Wait()
}

func TestUseBackgroundColor(t *testing.T) {
colors := []color.Color{color.Transparent, color.NRGBA{R: 255, A: 255}}
for i, col := range colors {
t.Run(fmt.Sprint(i), func(t *testing.T) {
c := vgimg.NewWith(vgimg.UseWH(1, 1), vgimg.UseBackgroundColor(col))
img := c.Image()
wantCol := color.RGBAModel.Convert(col)
haveCol := img.At(0, 0)
if !reflect.DeepEqual(haveCol, wantCol) {
t.Fatalf("color should be %#v but is %#v", wantCol, haveCol)
}
})
}
}

0 comments on commit 32f6a97

Please sign in to comment.