Skip to content

Instantly share code, notes, and snippets.

@Deleplace
Last active January 25, 2019 09:20
Show Gist options
  • Save Deleplace/419e7f4b54a8cae184452b7945945aac to your computer and use it in GitHub Desktop.
Save Deleplace/419e7f4b54a8cae184452b7945945aac to your computer and use it in GitHub Desktop.
A command-line program to generate a dancing gopher GIF
# Copyright 2019 Google LLC
# SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"os"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
const sourceGifFile = "./gopher-dance-long-3x-sign.gif"
const destGifFile = "./result.gif"
func main() {
fin, err := os.Open(sourceGifFile)
check(err)
defer fin.Close()
g, err := gif.DecodeAll(fin)
check(err)
fmt.Println(len(g.Image), "frames")
newLightColor := color.RGBA{255, 200, 200, 255}
newDimmedColor := color.RGBA{230, 175, 175, 255}
textColor := color.RGBA{240, 240, 240, 255}
for i, img := range g.Image {
modify(i, img, newLightColor, newDimmedColor, "foobar", textColor)
}
fout, err := os.OpenFile(destGifFile, os.O_RDWR|os.O_CREATE, 0777)
err = gif.EncodeAll(fout, g)
fout.Close()
check(err)
fmt.Println("Done.")
}
func modify(i int, img *image.Paletted, newLightColor, newDimmedColor color.Color, message string, textColor color.Color) {
dx, dy := 25, 35
for j, c := range img.Palette {
if c == originalLightColor {
// Main light blue
img.Palette[j] = newLightColor
} else if c == originalDimmedColor {
// Secondary dimmed blue
img.Palette[j] = newDimmedColor
}
}
addLabel(img, x[23-i]+dx, y[23-i]+dy, message, textColor)
}
func addLabel(img draw.Image, x, y int, label string, col color.Color) {
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
face := basicfont.Face7x13
face.Width = 14
face.Height = 26
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: face,
Dot: point,
}
d.DrawString(label)
}
// Hardcoded positions of the sign
var x = []int{45, 72, 76, 70, 43, 14, 18, 27, 48, 76, 78, 73, 44, 14, 11, 25, 46, 76, 82, 74, 42, 14, 11, 27}
var y = []int{101, 116, 117, 90, 99, 100, 114, 100, 96, 114, 117, 91, 97, 99, 105, 93, 96, 110, 117, 90, 97, 97, 109, 98}
var originalLightColor = color.RGBA{0x96, 0xd6, 0xff, 0xff}
var originalDimmedColor = color.RGBA{0x8d, 0xc9, 0xf0, 0xff}
func check(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment