Generating Grid Papers with Golang

Generating Grid Papers with Golang

I was writing another article a while ago and I needed a blueprint image for the background. I could find some on the internet but they were limited in colors or sizes.

So I decided to write a Go program to generate Grid Papers images for me. I came across Go’s standard library png and gif implementations for the image package. I played along and realised how it is to generate images using Golang.

Unlike my other articles, this one is not a deep dive but just enough to get you started and interested in Golang image generation capabilities.

Let’s take a look at the noticeable building blocks of the code.

Structs

RGBA

// defines an RBGA color value I use pantone names 
// from https://www.schemecolor.com/green-neon-tube.php
color.RGBA{
    R: 206,
    G: 216,
    B: 247,
    A: 255,
}

BluePrint

// BluePrint struct exposes mutator functions for it's properties.
// SetBackgroundColor is used to specify the background color for the grid.
// SetLineColor specifies the grid line color.
// SetThickLineSpacing is used to set spacing among thick grid lines.
// SetThinLineSpacing is used to set spacing among think grid lines. 
type BluePrint struct {
    img              *image.RGBA
    size             image.Point
    backgroundColor  color.RGBA
    lineColor        color.RGBA
    thinLineSpacing  int
    thickLineSpacing int
}

func (b *BluePrint) SetBackgroundColor(c color.RGBA) *BluePrint {
    b.backgroundColor = c
    return b
}

func (b *BluePrint) SetLineColor(c color.RGBA) *BluePrint {
    b.lineColor = c
    return b
}

func (b *BluePrint) SetThickLineSpacing(i int) *BluePrint {
    b.thickLineSpacing = i
    return b
}

func (b *BluePrint) SetThinLineSpacing(i int) *BluePrint {
    b.thinLineSpacing = i
    return b
}

func (b *BluePrint) Render(file *os.File) error {
    // removed for brevity.
}

Interfaces

Image

// Image is a finite rectangular grid of [color.Color] values taken from a color
// model.
type Image interface {
    // ColorModel returns the Image's color model.
    ColorModel() color.Model
    // Bounds returns the domain for which At can return non-zero color.
    // The bounds do not necessarily contain the point (0, 0).
    Bounds() Rectangle
    // At returns the color of the pixel at (x, y).
    // At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid.
    // At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
    At(x, y int) color.Color
}

Image - Drawable

// Image is an image.Image with a Set method to change a single pixel.
type Image interface {
    image.Image
    Set(x, y int, c color.Color)
}

Functions

PNG - Encode

// Encode writes the Image m to w in PNG format. Any Image may be
// encoded, but images that are not [image.NRGBA] might be encoded lossily.
func Encode(w io.Writer, m image.Image) error {
    var e Encoder
    return e.Encode(w, m)
}

NewBluePrint

// takes an image point 
// to generate the rectangle far end points.
func NewBluePrint(size image.Point) *BluePrint {
    img := image.NewRGBA(image.Rect(0, 0, size.X, size.Y))
    return &BluePrint{img: img, size: size}
}

Please use the below code for your reference. Steps to run the go code are mentioned later.

  • create a golang project and initialize it with go mod init

  • copy blueprint.go files to your folder

  • change the directory to your go project and run go build

  • this should generate a go binary with the name of your module, and run it locally.

  • running this program will generate two files green.png and blue.png

Generated Images

This should get you started for the journey of Go’s standard library Image package, in the next articles will look deep dive into the details of these packages and generate gif images as well. Then we will explore advanced image generation involving fonts and other images. Followed by image manipulation such as applying Gaussian blur to the images, and much more. The possibilities are endless and so is the fun.