blockies-react-svg
Version:
Blockies react SVG function component, blocky identicons, address-unique ethereum avatar, SVG base64 generation in browser or nodejs.
65 lines (63 loc) • 2.35 kB
JavaScript
// Forked from https://github.com/download13/blockies
// The random number is a js implementation of the Xorshift PRNG
const randseed = new Array(4); // Xorshift: [x, y, z, w] 32 bit values
function seedrand(seed) {
randseed.fill(0);
for (let i = 0; i < seed.length; i++) {
randseed[i % 4] = (randseed[i % 4] << 5) - randseed[i % 4] + seed.charCodeAt(i);
}
}
function rand() {
// based on Java's String.hashCode(), expanded to 4 32bit values
const t = randseed[0] ^ (randseed[0] << 11);
randseed[0] = randseed[1];
randseed[1] = randseed[2];
randseed[2] = randseed[3];
randseed[3] = randseed[3] ^ (randseed[3] >> 19) ^ t ^ (t >> 8);
return (randseed[3] >>> 0) / ((1 << 31) >>> 0);
}
function createColor() {
//saturation is the whole color spectrum
const h = Math.floor(rand() * 360);
//saturation goes from 40 to 100, it avoids greyish colors
const s = (rand() * 60 + 40).toFixed(1) + '%';
//lightness can be anything from 0 to 100, but probabilities are a bell curve around 50%
const l = ((rand() + rand() + rand() + rand()) * 25).toFixed(1) + '%';
return 'hsl(' + h + ',' + s + ',' + l + ')';
}
function createImageData(size) {
const width = size; // Only support square icons for now
const height = size;
const dataWidth = Math.ceil(width / 2);
const mirrorWidth = width - dataWidth;
const data = [];
for (let y = 0; y < height; y++) {
let row = [];
for (let x = 0; x < dataWidth; x++) {
// this makes foreground and background color to have a 43% (1/2.3) probability
// spot color has 13% chance
row[x] = Math.floor(rand() * 2.3);
}
const r = row.slice(0, mirrorWidth);
r.reverse();
row = row.concat(r);
for (let i = 0; i < row.length; i++) {
data.push(row[i]);
}
}
return data;
}
function buildOpts(opts) {
seedrand(opts.seed);
const newOpts = {
seed: opts.seed,
size: opts.size || 8,
scale: opts.scale || 10,
color: createColor(),
bgcolor: createColor(),
spotcolor: createColor()
};
return newOpts;
}
export { buildOpts as b, createImageData as c };
//# sourceMappingURL=blockies-a884f18e.mjs.map