Browse Source

initial commit! ヽ(^o^)丿

master v1.0.0
katherine 3 years ago
commit
0797b9df8f
  1. BIN
      Elixir.png
  2. 24
      LICENSE
  3. 27
      README.md
  4. BIN
      example.png
  5. 76
      lib/identicon.ex
  6. 19
      mix.exs
  7. 4
      test/identicon_test.exs
  8. 1
      test/test_helper.exs

BIN
Elixir.png

After

Width: 420  |  Height: 420  |  Size: 3.5 KiB

24
LICENSE

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>

27
README.md

@ -0,0 +1,27 @@
Identicon
=========
[![example output](example.png)](example.png)
a cute little idea stolen from
[rbishop](https://github.com/rbishop/identicon/blob/master/lib/identicon.ex).
provides a function for generating an "identicon" (an icon produced from the
hash of a given string). please feel free to steal from me as well! ^_^
this version is a little cleaner and uses softer, "web-safe" foreground colours
on a customisable background.
compile in the usual manner:
mix deps.get
mix compile
and then if you just want to test it out (with your name or whatever XD), try
something like:
iex -S mix
iex(1)> File.write "Elixir.png", Identicon.gen("Elixir", {36, 32, 36})
which produces:
[![generated icon](Elixir.png)](Elixir.png)

BIN
example.png

After

Width: 916  |  Height: 568  |  Size: 4.3 KiB

76
lib/identicon.ex

@ -0,0 +1,76 @@
defmodule Identicon do
@moduledoc """
Deterministically generate a simple "github-style" user icon using the hash
of a given string
"""
# use the first three bytes of the hash to determine foreground colour
defp colour(hash) do
hash
|> Stream.take(3)
|> Enum.map(&( rem(&1, 110) + 100 ))
|> List.to_tuple
end
# use the next 15 bytes to create a 5x5 symmetric pixel map
defp pixels(hash) do
hash
|> Stream.drop(3)
|> Stream.take(15)
|> Stream.map(&(rem &1, 2))
|> Stream.chunk_every(3)
|> Stream.flat_map(fn [a, b, c] -> [a, b, c, b, a] end)
|> Stream.with_index
end
# use the egd erlang library to draw the image
defp img(fg, bg, pixels) do
i = :egd.create 420, 420
fg = :egd.color fg
bg = :egd.color bg
:egd.filledRectangle(i, {0, 0}, {419, 419}, bg)
pixels
|> Stream.filter(fn {v, _} -> v == 1 end)
|> Stream.map(fn {_, i} -> {rem(i, 5) * 70, div(i, 5) * 70} end)
|> Enum.each(fn {x, y} ->
:egd.filledRectangle(i, {x+35, y+35}, {x+105, y+105}, fg) end)
:egd.render i, :png
end
@doc """
Takes the hash of `string` and returns a binary representing a
deterministically generated icon.
This icon will be an "identicon" à la github, a coloured, symmetric symbol in
a 5x5 grid of logical pixels. This symbol will be drawn on a 420x420px canvas
of colour `bg_colour`, where `bg_colour` is an rgb value, represented by a
3-tuple of single byte integers.
The returned icon will be a binary in png format, meaning it can be written
directly to disk. If doing so, consider optimising it with the `optipng`
command line tool or similar.
## Example
iex> r1 = Identicon.gen "elixir"
iex> r2 = Identicon.gen "elixir"
iex> r1 === r2
true
"""
@spec gen(binary, {byte, byte, byte}) :: binary
def gen(string, bg_colour \\ {0xf0, 0xf0, 0xf0}) do
h = :crypto.hash(:ripemd160, string)
|> :binary.bin_to_list
# get draw components
fg_colour = colour h
p = pixels h
# generate image
img fg_colour, bg_colour, p
end
end

19
mix.exs

@ -0,0 +1,19 @@
defmodule Identicon.MixProject do
use Mix.Project
def project do
[
app: :identicon,
version: "1.0.0",
elixir: "~> 1.7",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
defp deps do
[
{:egd, github: "erlang/egd"},
]
end
end

4
test/identicon_test.exs

@ -0,0 +1,4 @@
defmodule IdenticonTest do
use ExUnit.Case
doctest Identicon
end

1
test/test_helper.exs

@ -0,0 +1 @@
ExUnit.start()
Loading…
Cancel
Save