(()=>{
const size = 50,
margin = 0.08;
window.identiconSvg = (hash, txt, font) => {
// color defaults to last 7 chars as hue at 70% saturation, 50% brightness
// hsl2rgb adapted from: https://gist.github.com/aemkei/1325937
let h = (parseInt(hash.substr(-7), 16) / 0xfffffff) * 6, s = 0.7, l = 0.5,
v = [
l += s *= l < .5 ? l : 1 - l,
l - h % 1 * s * 2,
l -= s *= 2,
l,
l + h % 1 * s,
l + s
],
m = txt ? 128 : 255,
color = 'rgb(' + [
v[ ~~h % 6 ] * m, // red
v[ (h | 16) % 6 ] * m, // green
v[ (h | 8) % 6 ] * m // blue
].map(Math.round).join(',') + ')';
if (txt) {
txt = ``;
} else {
txt = ``;
const cell = Math.floor((size - ((Math.floor(size * margin)) * 2)) / 5),
imargin = Math.floor((size - cell * 5) / 2),
rectangles = [],
add = (x, y) => rectangles.push("");
// the first 15 characters of the hash control the pixels (even/odd)
// they are drawn down the middle first, then mirrored outwards
for (let i = 0; i < 15; ++i) {
if (!(parseInt(hash.charAt(i), 16) % 2)) {
if (i < 5) {
add(2, i);
} else if (i < 10) {
add(1, (i - 5));
add(3, (i - 5));
} else {
add(0, (i - 10));
add(4, (i - 10));
}
}
}
txt = ""
+ rectangles.join('')
+ "";
}
return "";
};
})();