Skip to content

Instantly share code, notes, and snippets.

@paulohp
Created November 21, 2016 15:48
Show Gist options
  • Save paulohp/d07e04b59c15c791ac0a8019034c68ef to your computer and use it in GitHub Desktop.
Save paulohp/d07e04b59c15c791ac0a8019034c68ef to your computer and use it in GitHub Desktop.
defmodule Identicon do
def main(input) do
input
|> hash_input
|> pick_color
|> build_grid
end
def build_grid(%Identicon.Image{hex: hex} = image) do
grid =
hex
|> Enum.chunk(3)
|> Enum.map(&mirror_row/1)
|> List.flatten
|> Enum.with_index
%Identicon.Image{image | grid: grid}
end
def mirror_row(row) do
[first, second | _tail] = row
row ++ [second, first]
end
def pick_color(%Identicon.Image{hex: [r, g, b | _tail]} = image) do
%Identicon.Image{image | color: {r, g, b}}
end
def hash_input(input) do
hex = :crypto.hash(:md5, input)
|> :binary.bin_to_list
%Identicon.Image{hex: hex}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment