Skip to content

Instantly share code, notes, and snippets.

@tomowarkar
Last active December 11, 2019 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomowarkar/8877ae34df9e1ae627b51c55ce6d2710 to your computer and use it in GitHub Desktop.
Save tomowarkar/8877ae34df9e1ae627b51c55ce6d2710 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"math/rand"
"os"
"time"
)
var (
khaki = color.RGBA{240, 230, 140, 255}
darkgoldenrod = color.RGBA{184, 134, 11, 255}
royalblue = color.RGBA{65, 105, 225, 255}
seagreen = color.RGBA{46, 139, 87, 255}
width = 16
height = 10
scale = 40
)
func setField() []int {
rand.Seed(time.Now().UnixNano())
field := make([]int, width*height)
for i := 0; i < len(field); i++ {
field[i] = rand.Intn(4)
}
return field
}
func timestamp() string {
t := time.Now()
return fmt.Sprintf("%04d%02d%02d%02d%02d%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second(),
)
}
func main() {
field := setField()
img := image.NewRGBA(image.Rect(0, 0, width*scale, height*scale))
for x := 0; x < width*scale; x++ {
for y := 0; y < height*scale; y++ {
switch field[y/scale*height+x/scale] {
case 1:
img.Set(x, y, khaki)
case 2:
img.Set(x, y, royalblue)
case 3:
img.Set(x, y, seagreen)
default:
img.Set(x, y, darkgoldenrod)
}
}
}
fpath := fmt.Sprintf("./img/image%s.png", timestamp())
f, _ := os.Create(fpath)
defer f.Close()
png.Encode(f, img)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment