hash_color_avatar v0.1.0 HashColorAvatar
This is a small library to generate SVG initial avatar with unique-ish color based on string hash.
The primary feature is to generate on the fly SVG for default avatar. The user can get unique avatar (to certain degree) based on his name innitial and the unique color generated by hashing its name. Please be noted that of couse same name will resulting to the same image. And eventhough there are thousands of color exist, we choose to make the saturation fix so there can only be 359 possible color.
Another function will be quite useful as well such as random_color/1, will give you nice pastel random color which you can use as background or anything.
set_color/2 can be used to make color by specifying the hue value.
Function to Generate Initial from name, gen_initial/1 can also be used independently.
Link to this section Summary
Functions
Given some string, this function will generate SVG avatar. The main feature is the micro hasing function. Which means th ecolor given for "Frank Abraham" will be different for "Foreman Abdul" even though they both have same initials
This function generate initial from any given name. If more than 2 word given it will take initial first and last word. It will try to ignore other character
This function will convert HSV map to RGB map
Will generate random color in hex
This function will convert RGB to hex
This function will convert RGB to hex
Link to this section Functions
gen_avatar(rawtext, options \\ [])
Given some string, this function will generate SVG avatar. The main feature is the micro hasing function. Which means th ecolor given for "Frank Abraham" will be different for "Foreman Abdul" even though they both have same initials.
Examples
iex> HashColorAvatar.gen_avatar("")
'<svg width="100" height="100"><circle cx="50.0" cy="50.0" r="50.0" stroke="white" stroke-width="4" fill="pastel" /><text fill="white" x="50%" y="67%" text-anchor="middle" style="font: bold 41.66666666666667px sans-serif;" >VK</text></circle></svg>'
Option
:color can be used to specify background color. By default it will generate hash based on the text given. It will be unique-ish since there are only 359 possible color and there's a chance it looks similar one amongst the other. For the value you can choose "random", any color code recognized by CSS such as "teal", "tomato", also it accept hex code.
:shape by default is circle. You can also choose "rect" for rectangle avatar.
:size You can define how many pixel height and width. Default is 100
Examples
iex> HashColorAvatar.gen_avatar("Samantha Johnson Abigail", [color: "tomato", shape: "rect", size: 200])
'<svg width="200" height="200"><rect width="200" height="200" fill="tomato" /><text fill="white" x="50%" y="65%" text-anchor="middle" style="font: bold 83.33333333333334px sans-serif;" >SA</text></circle></svg>'
get_initial(name)
This function generate initial from any given name. If more than 2 word given it will take initial first and last word. It will try to ignore other character.
Examples
iex> HashColorAvatar.get_initial("sujiwo tedjo")
"ST"
iex> HashColorAvatar.get_initial("guruh soekarno putra")
"GP"
hsv_to_rgb(hsv)
This function will convert HSV map to RGB map.
Examples
iex> HashColorAvatar.hsv_to_rgb(%{hue: 17, saturation: 50, value: 90})
%{blue: 114, green: 147, red: 229}
random_color(options \\ [])
Will generate random color in hex.
Options
This color is generated by randomizing HSV (Hue Saturation Value) with default Saturation is set to 50 and Value set to 90. ``` iex> HashColorAvatar.random_color([saturation: 70, value: 100])
rgb_to_hex(rgb)
This function will convert RGB to hex.
Examples
iex> HashColorAvatar.rgb_to_hex(%{red: 12, green: 23, blue: 43})
"#0C172B"
iex> HashColorAvatar.rgb_to_hex(%{red: 121, green: 13, blue: 203})
"#790DCB"
set_color(hue_value, options \\ [])
This function will convert RGB to hex.
Examples
iex> HashColorAvatar.set_color(12)
"#E58972"
Option also applied
iex> HashColorAvatar.set_color(12, [saturation: 70, value: 80])
"#CC593D"