Skip to content

Instantly share code, notes, and snippets.

@avary
Forked from sadysnaat/blueprint.go
Created January 9, 2025 11:15
Show Gist options
  • Save avary/553745cfedc05ef66a4bbcfba7720817 to your computer and use it in GitHub Desktop.
Save avary/553745cfedc05ef66a4bbcfba7720817 to your computer and use it in GitHub Desktop.
Blue Print generating Go Program
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
var (
Pantone2706C = color.RGBA{
R: 206,
G: 216,
B: 247,
A: 255,
}
Pantone2726C = color.RGBA{
R: 48,
G: 87,
B: 225,
A: 255,
}
PantoneGreen0921C = color.RGBA{
R: 162,
G: 252,
B: 206,
A: 255,
}
Pantone7479C = color.RGBA{
R: 49,
G: 222,
B: 124,
A: 255,
}
)
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 {
for i := 0; i < b.size.X; i++ {
for j := 0; j < b.size.Y; j++ {
b.img.Set(i, j, b.backgroundColor)
b.handleLines(i, j)
}
}
err := png.Encode(file, b.img)
if err != nil {
fmt.Println(err)
}
err = file.Close()
if err != nil {
fmt.Println(err)
}
return nil
}
func (b *BluePrint) handleLines(i, j int) {
if i%b.thinLineSpacing == 0 {
b.img.Set(i, j, b.lineColor)
}
if b.isThickLinePixel(i, j) {
b.img.Set(i, j, b.lineColor)
}
}
func (b *BluePrint) isThickLinePixel(i, j int) bool {
switch {
case i%b.thickLineSpacing == 0 || j%b.thinLineSpacing == 0:
return true
case i+1 < b.size.X && (i+1)%b.thickLineSpacing == 0:
return true
case j+1 < b.size.Y && (j+1)%b.thickLineSpacing == 0:
return true
case i-1 > 0 && (i-1)%b.thickLineSpacing == 0:
return true
case j-1 > 0 && (j-1)%b.thickLineSpacing == 0:
return true
default:
return false
}
}
func NewBluePrint(size image.Point) *BluePrint {
img := image.NewRGBA(image.Rect(0, 0, size.X, size.Y))
return &BluePrint{img: img, size: size}
}
func main() {
bp := NewBluePrint(image.Pt(1000, 1000)).
SetBackgroundColor(Pantone2726C).
SetLineColor(Pantone2706C).
SetThickLineSpacing(100).
SetThinLineSpacing(20)
f, err := os.Create("blue.png")
if err != nil {
fmt.Println(err)
}
err = bp.Render(f)
if err != nil {
fmt.Println(err)
}
bp.SetBackgroundColor(PantoneGreen0921C).
SetLineColor(Pantone7479C)
f, err = os.Create("green.png")
if err != nil {
fmt.Println(err)
}
err = bp.Render(f)
if err != nil {
fmt.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment