Skip to content

Instantly share code, notes, and snippets.

@skybrian
Last active August 29, 2015 13:57
Show Gist options
  • Save skybrian/9719296 to your computer and use it in GitHub Desktop.
Save skybrian/9719296 to your computer and use it in GitHub Desktop.
import Dict
type Painting = {palette: [Color], grid: [[Int]]}
model: Painting
model = {
palette = [red, orange, yellow, green, blue, purple],
grid = [[0,1,2,3,4,5], [1,2,3,4,5,0], [2,3,4,5,0,1]] }
indexed: [a] -> Dict.Dict Int a
indexed list = Dict.fromList (zip [0..(length list)] list)
type Palette = Dict.Dict Int Color
makePalette: [Color] -> Palette
makePalette colors = indexed colors
colorAt: Palette -> Int -> Color
colorAt p i = case Dict.lookup i p of
Just c -> c
Nothing -> black
cellAt: Palette -> Int -> Int -> Form
cellAt p x i = moveX (toFloat (5 + x * 10)) (filled (colorAt p i) (rect 10 10))
rowAt: Palette -> Int -> [Int] -> Form
rowAt p y pixels = moveY (toFloat (-5 - y * 10)) (group (zipWith (cellAt p) [0..(length pixels)] pixels))
render: Painting -> Element
render m =
let width = 10 * (length (head m.grid))
height = 10 * (length m.grid)
palette = makePalette m.palette
image = (group (zipWith (rowAt palette) [0..(length m.grid)] m.grid))
in
collage width height [move (toFloat(-width)/2, toFloat(height)/2) image]
rotate: [a] -> [a]
rotate l = concat [(tail l), [head l]]
step: Float -> Painting -> Painting
step _ prev = { palette = rotate prev.palette, grid = prev.grid }
frames: Signal Painting
frames = foldp step model (every second)
main: Signal Element
main = lift render frames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment