Skip to content

Instantly share code, notes, and snippets.

@cymruu
Created February 18, 2018 16:38
Show Gist options
  • Save cymruu/bfbd486fe1e0f7c5a8d95f56fc0d5d45 to your computer and use it in GitHub Desktop.
Save cymruu/bfbd486fe1e0f7c5a8d95f56fc0d5d45 to your computer and use it in GitHub Desktop.
A program that draws github-like contribution graph chart
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"math/rand"
"os"
"time"
)
var margin = 3
var width, height = 1240, 480
var blockSize = 10
var withMarginSize = blockSize + margin
var img = image.NewRGBA(image.Rect(0, 0, width, height))
var light_gray = color.RGBA{0xee, 0xee, 0xee, 0xff}
var light_green = color.RGBA{0xc6, 0xe4, 0x8b, 0xff} //c6e48b
var green = color.RGBA{0x7b, 0xc9, 0x6f, 0xff} //7bc96f
var greener = color.RGBA{0x23, 0x9a, 0x3b, 0xff} //239a3b
var the_greenest = color.RGBA{0x19, 0x61, 0x27, 0xff} //196127
var gitColors = []color.RGBA{light_green, green, greener, the_greenest}
var random = rand.New(rand.NewSource(time.Now().Unix()))
func getRandomColor() *color.RGBA {
return &gitColors[random.Intn(len(gitColors))]
}
func main() {
draw.Draw(img, img.Bounds(), &image.Uniform{light_gray}, image.ZP, draw.Src)
for x := 0; x < width/withMarginSize; x++ {
posx := x*blockSize + (x+1)*margin
for y := 0; y < height/withMarginSize; y++ {
fmt.Printf("Drawing rect %d", x+y)
posy := y*blockSize + (y+1)*margin
git_rect := image.Rect(posx, posy, posx+blockSize, posy+blockSize) // geometry of 2nd rectangle
draw.Draw(img, git_rect, &image.Uniform{getRandomColor()}, image.ZP, draw.Src)
}
}
f, err := os.Create("draw.png")
if err != nil {
panic(err)
}
defer f.Close()
png.Encode(f, img)
}
@cymruu
Copy link
Author

cymruu commented Feb 18, 2018

draw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment