diff --git a/context.go b/context.go index f436878..dbfa7eb 100644 --- a/context.go +++ b/context.go @@ -496,6 +496,18 @@ func (dc *Context) AsMask() *image.Alpha { return mask } +// InvertMask inverts the alpha values in the current clipping mask such that +// a fully transparent region becomes fully opaque and vice versa. +func (dc *Context) InvertMask() { + if dc.mask == nil { + dc.mask = image.NewAlpha(dc.im.Bounds()) + } else { + for i, a := range dc.mask.Pix { + dc.mask.Pix[i] = 255 - a + } + } +} + // Clip updates the clipping region by intersecting the current // clipping region with the current path as it would be filled by dc.Fill(). // The path is cleared after this operation. diff --git a/examples/invert-mask.go b/examples/invert-mask.go new file mode 100644 index 0000000..b181503 --- /dev/null +++ b/examples/invert-mask.go @@ -0,0 +1,14 @@ +package main + +import "github.com/fogleman/gg" + +func main() { + dc := gg.NewContext(1024, 1024) + dc.DrawCircle(512, 512, 384) + dc.Clip() + dc.InvertMask() + dc.DrawRectangle(0, 0, 1024, 1024) + dc.SetRGB(0, 0, 0) + dc.Fill() + dc.SavePNG("out.png") +}