Skip to content

Instantly share code, notes, and snippets.

@sameer sameer/plan9palette.go
Last active May 11, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.