My Wiki
Search…
Core Concepts
Graphics deals with images, which are, in their simplest form, nothing more than two-dimensional arrays of pixels, containing color information for each pixel:

Image

At the core, there are two main interfaces: image.Image and image/draw.Image. One serves as a readable image source, the other is used for drawing:
1
// image.Image
2
type Image interface {
3
ColorModel() color.Model
4
Bounds() Rectangle
5
At(x, y int) color.Color
6
}
Copied!
1
// draw.Image is an image.Image with a Set method to change a single pixel.
2
type Image interface {
3
image.Image
4
Set(x, y int, c color.Color)
5
}
Copied!

Color

TBA

Example: Creating a Circular Mask

This example has been taken from The Go image/draw package | The Go Blog​
1
type circle struct {
2
p image.Point
3
r int
4
}
5
​
6
func (c *circle) ColorModel() color.Model {
7
return color.AlphaModel
8
}
9
​
10
func (c *circle) Bounds() image.Rectangle {
11
return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)
12
}
13
​
14
func (c *circle) At(x, y int) color.Color {
15
xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
16
if xx*xx+yy*yy < rr*rr {
17
return color.Alpha{255}
18
}
19
return color.Alpha{0}
20
}
Copied!

Resources

Libraries

Last modified 1yr ago