Skip to content

Instantly share code, notes, and snippets.

@sameer
Last active January 18, 2024 19:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sameer/7c27ae1985deea0088c86cc13cc88bb1 to your computer and use it in GitHub Desktop.
Save sameer/7c27ae1985deea0088c86cc13cc88bb1 to your computer and use it in GitHub Desktop.
Creates a Plan 9 Palette as a 2D image using the gg library
import (
"github.com/fogleman/gg"
"image/color/palette"
"strconv"
"image/color"
)
func main() {
const numSquaresPerRow = 16.
const numSquaresPerColumn = numSquaresPerRow
const squareSideLength = 64.
dc := gg.NewContext(numSquaresPerRow*squareSideLength, numSquaresPerColumn*squareSideLength)
growingOffset := 0
for i := 0; i < numSquaresPerColumn; i++ {
if i%4 != 0 {
growingOffset++
}
for j := 0; j < numSquaresPerRow; j++ {
plan9Color := palette.Plan9[i*numSquaresPerColumn+j].(color.RGBA)
r, g, b := plan9Color.R, plan9Color.G, plan9Color.B
oppositeOfPlan9Color := color.RGBA{uint8(0xFF - r), uint8(0xFF - g), uint8(0xFF - b), 0xFF}
dc.SetColor(plan9Color)
drawI, drawJ := float64(i), float64(j-growingOffset)
if drawJ < 0 {
drawJ = float64(numSquaresPerColumn + drawJ)
}
dc.DrawRectangle(drawI*squareSideLength, drawJ*squareSideLength, squareSideLength, squareSideLength)
dc.Fill()
dc.SetColor(oppositeOfPlan9Color)
dc.DrawString(strconv.Itoa(i*numSquaresPerColumn+j), drawI*squareSideLength+squareSideLength/16, drawJ*squareSideLength + squareSideLength)
dc.Fill()
}
}
savePNG()
}
func savePNG() {
dc.SavePNG("Plan9_Palette.png")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment