Add InvertMask and an example

This commit is contained in:
Michael Fogleman 2019-01-14 11:04:49 -05:00
parent 0e0ff3ade7
commit cdabe43353
2 changed files with 26 additions and 0 deletions

View File

@ -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.

14
examples/invert-mask.go Normal file
View File

@ -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")
}