blockies-base64-svg
Version:
SVG Blockies base64 generation in browser or nodejs.
107 lines (103 loc) • 4.1 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;
}
const store = Object.create(null);
const _btoa = btoa || (window === null || window === void 0 ? void 0 : window.btoa) || ((str) => Buffer.from(str, 'binary').toString('base64'));
function makeBlockiesUrl(address, size = 8, caseSensitive = false, scale = 10) {
if (!address)
throw new Error('Address is required');
if (!caseSensitive)
address = address.toLowerCase();
if (store[`${size}:${address}`]) {
return store[`${size}:${address}`];
}
const opts = buildOpts({ seed: address, size, scale });
const imageData = createImageData(opts.size);
const width = size * scale;
const svgMarkup = `
<svg width="${width}" height="${width}" viewBox="0 0 ${width} ${width}" xmlns="http://www.w3.org/2000/svg">
<rect width="${width}" height="${width}" fill="${opts.bgcolor}" />
<g fill="${opts.color}">
${imageData === null || imageData === void 0 ? void 0 : imageData.map((value, i) => {
if (value === 1) {
const row = (i % size) * scale;
const col = Math.floor(i / size) * scale;
return `<rect width="${scale}" height="${scale}" x="${row}" y="${col}" />`;
}
}).join('')}
</g>
<g fill="${opts.spotcolor}">
${imageData === null || imageData === void 0 ? void 0 : imageData.map((value, i) => {
if (value === 2) {
const row = (i % size) * scale;
const col = Math.floor(i / size) * scale;
return `<rect width="${scale}" height="${scale}" x="${row}" y="${col}" />`;
}
}).join('')}
</g>
</svg>`;
const base64Url = `data:image/svg+xml;base64,${_btoa(svgMarkup)}`;
store[`${size}:${address}`] = base64Url;
return base64Url;
}
module.exports = makeBlockiesUrl;
//# sourceMappingURL=makeBlockiesUrl.js.map