Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Open vs Merged GIF now uses disposals for better transitions (#40)
Browse files Browse the repository at this point in the history
* white background works

* support gif disposals + background
  • Loading branch information
prince-chrismc authored Feb 10, 2022
1 parent 45f1a21 commit 2f638ee
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 49 deletions.
65 changes: 65 additions & 0 deletions cmd/ovm/animate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"image"
"image/color"
"image/draw"
"image/gif"
)

func MakeGif(images []image.Image, delay int) gif.GIF {
// Alloc slice with 0 elems but capacity of all images
frames := make([]*image.Paletted, 0, len(images)+1)
delays := make([]int, 0, len(images)+1)
disposals := make([]byte, 0, len(images)+1)

for _, png := range images {
// Images are in reverse order so we need to prepend them
frames = append([]*image.Paletted{renderToPalette(png)}, frames...)
delays = append(delays, delay)
disposals = append(disposals, gif.DisposalBackground)
}

// Add out background to revert to when changing frames
frames = append([]*image.Paletted{makeBlank()}, frames...)
delays = append([]int{0}, delays...)
disposals = append([]byte{gif.DisposalNone}, disposals...)

return gif.GIF{
Image: frames,
Delay: delays,
LoopCount: 10,
Disposal: disposals,
BackgroundIndex: 0,
}
}

func makeBlank() *image.Paletted {
var palette color.Palette = color.Palette{
image.Transparent,
}
img := image.NewPaletted(image.Rect(0, 0, 4025, 2048), palette)
draw.Draw(img, img.Bounds(), &image.Uniform{color.RGBA{0, 0, 0, 0}}, img.Bounds().Min, draw.Src)
return img
}

func renderToPalette(img image.Image) *image.Paletted {
var palette color.Palette = color.Palette{
image.Transparent,
// color.RGBA{255, 255, 255, 255},
color.RGBA{88, 166, 255, 255},
color.RGBA{63, 185, 80, 255},
color.RGBA{248, 81, 73, 255},
color.RGBA{163, 113, 247, 255},
color.RGBA{134, 94, 201, 255},
}
// Some days the images are off by a column so we are just hard coding the fix for now
// TODO(prince-chrismc) Make this more generic
sp := img.Bounds().Min
width := img.Bounds().Dx()
sp.X = width - 4025

paletted := image.NewPaletted(image.Rect(0, 0, 4025, 2048), palette)
draw.Draw(paletted, image.Rect(0, 0, 4025, 2048), img, sp, draw.Src)
return paletted
}
3 changes: 1 addition & 2 deletions cmd/ovm/dry_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"image/png"
"os"

"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/charts"
"github.com/wcharczuk/go-chart/v2"
)

Expand Down Expand Up @@ -37,7 +36,7 @@ func SaveToDisk(barGraph chart.StackedBarChart, images []image.Image) error {
}

images = append([]image.Image{img}, images...)
jif := charts.MakeGif(images, delay)
jif := MakeGif(images, delay)

g, _ := os.Create("ovm.gif")
defer g.Close()
Expand Down
2 changes: 1 addition & 1 deletion cmd/ovm/opened_versus_merged.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func OpenVersusMerged(token string, dryRun bool) error {
}

images = append([]image.Image{img}, images...)
jif := charts.MakeGif(images, delay)
jif := MakeGif(images, delay)

var b3 bytes.Buffer
err = gif.EncodeAll(&b3, &jif)
Expand Down
46 changes: 0 additions & 46 deletions internal/charts/animate.go

This file was deleted.

0 comments on commit 2f638ee

Please sign in to comment.