Packages

It creates a default profile picture for users in the case where they don't want to upload real picture.

Current section

Files

Jump to
tinpo_identicon lib identicon.ex
Raw

lib/identicon.ex

defmodule Identicon do
@moduledoc """
This application generates a visual representation of a hash value,
usually of an IP address, that serves to identify a user of a computer
while protecting the user's privacy or due to inability of user to
upload real identity.
"""
@doc """
The pipe here helps to run the entire program per input by passing from hash_input
down to the last one which is save_image(input) and return the input as "ok" if it is correct.
## Examples
iex> Identicon.main("olatinpo")
:ok
"""
def main(input) do
input
|> hash_input
|> select_colour
|> build_grid
|> remove_odd_squares
|> build_pixel_map
|> present_image
|> save_image(input)
end
@doc """
Ultimately, this function alllows each image created to be saved in a particular format.
# for example Identicon.main("olatinpo") will be saved as olatinpo.png.
"""
def save_image(image, input) do
File.write("#{input}.png", image)
end
@doc """
Erlang function is invoked here to convert our refined data to the image we set out to do.
"""
def present_image(%Identicon.Image{colour: colour, pixel_map: pixel_map}) do
image = :egd.create(250, 250)
fill = :egd.color(colour)
Enum.each pixel_map, fn({start, stop}) -> :egd.filledRectangle(image, start, stop, fill) end
:egd.render(image)
end
@doc """
This task helps to determine the pixel size of each grid in the entire space.
"""
def build_pixel_map(%Identicon.Image{grid: grid} = image) do
pixel_map = Enum.map(grid, fn({_code, index}) ->
horizontal = rem(index, 5) * 50
vertical = div(index, 5) * 50
top_left = {horizontal, vertical}
bottom_right = {horizontal + 50, vertical + 50}
{top_left, bottom_right}
end)
%Identicon.Image{image | pixel_map: pixel_map}
end
@doc """
In a bid to colour some portions of grid, the selection of which grid to be coloured
is based on the type of number value. Only grid with non-odd numbers is coloured, hence
why this function task is to remove odd grids and make them blank.
"""
def remove_odd_squares(%Identicon.Image{grid: grid} = image) do
grid = Enum.filter(grid, fn({code, _index}) -> rem(code, 2) == 0 end)
%Identicon.Image{image | grid: grid}
end
@doc """
This function with the help of pipe operator allow us to form our grid comprising
5 rows and 5 columns. The pipe works in following order:
divide the hash values in threes --> produce mirrow values for each three i.e, turning 3 numbers to 5 numbers --> join each set together --> create a numbering system where first value is one, second is 2 and so on
"""
def build_grid(%Identicon.Image{hex: hex_list} = image) do
grid = hex_list
|> Enum.chunk(3)
|> Enum.map(&mirror_row/1)
|> List.flatten
|> Enum.with_index
%Identicon.Image{image | grid: grid}
end
@doc """
This function helps the application to use 3 values to produce 5 values in row.
"""
def mirror_row (row) do
# [99, 49, 209]
[first, second | _tail] = row
# [99, 49, 209, 49, 99]
row ++ [second, first]
end
@doc """
This function selects the first three elements in the hash list and use
as RGB colour value in HTML.
"""
def select_colour(image) do
%Identicon.Image{hex: hex_list} = image
[r, g, b | _tail] = hex_list
%Identicon.Image{image | colour: {r, g, b}}
end
@doc """
The function here transforms the input to md5 format which in turn converts the binary value
to list.
## Example
iex(1)> Identicon.hash_input("olatinpo")
%Identicon.Image{
colour: nil,
grid: nil,
hex: [31, 201, 231, 9, 224, 13, 55, 164, 102, 113, 188, 89, 195, 193, 18, 130],
pixel_map: nil
}
"""
def hash_input(input) do
hex = :crypto.hash(:md5, input)
|> :binary.bin_to_list
%Identicon.Image{hex: hex}
end
end