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:

source: ResearchGate


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:

// image.Image
type Image interface {
    ColorModel() color.Model
    Bounds() Rectangle
    At(x, y int) color.Color
// draw.Image is an image.Image with a Set method to change a single pixel.
type Image interface {
    Set(x, y int, c color.Color)



Example: Creating a Circular Mask

This example has been taken from The Go image/draw package | The Go Blog

type circle struct {
    p image.Point
    r int

func (c *circle) ColorModel() color.Model {
    return color.AlphaModel

func (c *circle) Bounds() image.Rectangle {
    return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)

func (c *circle) At(x, y int) color.Color {
    xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
    if xx*xx+yy*yy < rr*rr {
        return color.Alpha{255}
    return color.Alpha{0}



Last updated