/** * Identicon.js v1.0 * http://github.com/stewartlord/identicon.js * * Requires PNGLib * http://www.xarg.org/download/pnglib.js * * Copyright 2013, Stewart Lord * Released under the BSD license * http://www.opensource.org/licenses/bsd-license.php */ (function() { Identicon = function(hash, size, margin){ this.hash = hash; this.size = size || 64; this.margin = margin || .08; } Identicon.prototype = { hash: null, size: null, margin: null, render: function(){ var hash = this.hash, size = this.size, margin = Math.floor(size * this.margin), cell = Math.floor((size - (margin * 2)) / 5), image = new PNGlib(size, size, 256); // light-grey background var bg = image.color(240, 240, 240); // foreground is last 7 chars as hue at 50% saturation, 70% brightness var rgb = this.hsl2rgb(parseInt(hash.substr(-7), 16) / 0xfffffff, .5, .7), fg = image.color(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255); // the first 15 characters of the hash control the pixels (even/odd) // they are drawn down the middle first, then mirrored outwards var i, color; for (i = 0; i < 15; i++) { color = parseInt(hash.charAt(i), 16) % 2 ? bg : fg; if (i < 5) { this.rectangle(2 * cell + margin, i * cell + margin, cell, cell, color, image); } else if (i < 10) { this.rectangle(1 * cell + margin, (i - 5) * cell + margin, cell, cell, color, image); this.rectangle(3 * cell + margin, (i - 5) * cell + margin, cell, cell, color, image); } else if (i < 15) { this.rectangle(0 * cell + margin, (i - 10) * cell + margin, cell, cell, color, image); this.rectangle(4 * cell + margin, (i - 10) * cell + margin, cell, cell, color, image); } } return image; }, rectangle: function(x, y, w, h, color, image) { var i, j; for (i = x; i < x + w; i++) { for (j = y; j < y + h; j++) { image.buffer[image.index(i, j)] = color; } } }, // adapted from: https://gist.github.com/aemkei/1325937 hsl2rgb: function(h, s, b){ h *= 6; s = [ b += s *= b < .5 ? b : 1 - b, b - h % 1 * s * 2, b -= s *= 2, b, b + h % 1 * s, b + s ]; return[ s[ ~~h % 6 ], // red s[ (h|16) % 6 ], // green s[ (h|8) % 6 ] // blue ]; }, toString: function(){ return this.render().getBase64(); } } window.Identicon = Identicon; })();