retricon
v0.0.3
Published
create indenticon-like visual hashes styled like Github and Gravatar (retro) avatars
Downloads
7
Readme
node-retricon
create indenticon-like image hashes styled like Github and Gravatar (retro) avatars
Example
var retricon = require('retricon');
var fmt = require('util').format;
console.log(fmt("<img alt='kibo' src='%s' />", retricon('kibo', 16, 0).toDataURL()));
API
retricon(str, pixelSize, border)
str
: string - username, email, or other string to hashpixelSize
: int - size in pixels to render each virtual pixelborder
: int (optional) - number of pixels padding about each rendered pixel. This is inside the bounds defined bypixelSize
.
Returns a Canvas
object containing the rendered image.
Algorithm
The input string is converted to a buffer of UTF-8 bytes. A one-byte iterator is appended to this, initialized at zero. The buffer is passed through SHA-512, and the output buffer is iteratively XORed to produce a final buffer of only the number of required bytes (currently eighteen). The first three bytes are taken as RGB values for the colour. The remaining fifteen bytes are quantized to boolean values at a threshold of 127. If at least 5 of the booleans are true, the hash is accepted. Otherwise, the iterator is incremented, and the process repeated.
The array of booleans is taken to be a 3x5 row-major array. It is reflected about the x=2 axis to for a 5x5 array, which is then rendered as the final visual hash.