Created
February 13, 2018 21:15
-
-
Save peterhellberg/15c3ceff128d215f789bf5225b6ea161 to your computer and use it in GitHub Desktop.
Atkinson dithered plasma rendered by Pixel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"image" | |
"image/color" | |
"image/draw" | |
"time" | |
"github.com/faiface/pixel" | |
"github.com/faiface/pixel/pixelgl" | |
"github.com/peterhellberg/plasma" | |
"github.com/peterhellberg/plasma/palette" | |
) | |
func run() { | |
const w, h, d = 512, 256, 64 * time.Millisecond | |
win, err := pixelgl.NewWindow(pixelgl.WindowConfig{ | |
Bounds: pixel.R(0, 0, float64(w), float64(h)), | |
VSync: true, | |
Undecorated: true, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
win.SetSmooth(false) | |
c := win.Bounds().Center() | |
t := time.NewTicker(d) | |
var i int | |
for !win.Closed() { | |
win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ)) | |
select { | |
case <-t.C: | |
i++ | |
p := atkinsonPlasmaPicture(w/4, h/4, 12, i) | |
s := pixel.NewSprite(p, p.Bounds()) | |
s.Draw(win, pixel.IM.Moved(c).Scaled(c, 4)) | |
default: | |
} | |
win.Update() | |
} | |
} | |
func main() { | |
pixelgl.Run(run) | |
} | |
func atkinsonPlasmaPicture(w, h int, s float64, i int) *pixel.PictureData { | |
return pixel.PictureDataFromImage(atkinsonDither( | |
plasma.New(w, h, s).Image(w, h, i, palette.DefaultGradient), | |
)) | |
} | |
func atkinsonDither(src image.Image) image.Image { | |
bounds, dst := src.Bounds(), image.NewGray(src.Bounds()) | |
draw.Draw(dst, bounds, src, image.ZP, draw.Src) | |
s := 1 | |
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { | |
for x := bounds.Min.X; x < bounds.Max.X; x++ { | |
gray := dst.At(x, y).(color.Gray) | |
var m = mono(gray.Y) | |
quant_err := int((float64(gray.Y) - float64(m)) / 8) | |
dst.SetGray(x, y, color.Gray{m}) | |
for _, p := range []image.Point{ | |
{x + s, y}, | |
{x - s, y + s}, | |
{x, y + s}, | |
{x + s, y + s}, | |
{x + 2*s, y}, | |
{x, y + 2*s}, | |
} { | |
gray := dst.At(p.X, p.Y).(color.Gray) | |
dst.SetGray(p.X, p.Y, color.Gray{uint8(int16(gray.Y) + int16(quant_err))}) | |
} | |
} | |
} | |
return dst | |
} | |
func mono(l uint8) uint8 { | |
if l < 128 { | |
return 0 | |
} else { | |
return 255 | |
} | |
} |
Author
peterhellberg
commented
Feb 13, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment