2019-04-15 04:37:21 -07:00
|
|
|
/**
|
2022-01-15 17:45:48 -08:00
|
|
|
* Identicon generator
|
|
|
|
* https://en.wikipedia.org/wiki/Identicon
|
2019-04-15 04:37:21 -07:00
|
|
|
*/
|
|
|
|
|
2021-08-19 02:33:41 -07:00
|
|
|
import * as p from 'pureimage';
|
2019-04-15 04:37:21 -07:00
|
|
|
import * as gen from 'random-seed';
|
2019-04-15 06:58:04 -07:00
|
|
|
import { WriteStream } from 'fs';
|
2019-04-15 04:37:21 -07:00
|
|
|
|
2019-04-15 06:58:04 -07:00
|
|
|
const size = 256; // px
|
2019-04-15 04:37:21 -07:00
|
|
|
const n = 5; // resolution
|
2021-04-01 18:00:05 -07:00
|
|
|
const margin = (size / n);
|
2019-04-15 04:37:21 -07:00
|
|
|
const colors = [
|
|
|
|
'#e57373',
|
|
|
|
'#F06292',
|
|
|
|
'#BA68C8',
|
|
|
|
'#9575CD',
|
|
|
|
'#7986CB',
|
|
|
|
'#64B5F6',
|
|
|
|
'#4FC3F7',
|
|
|
|
'#4DD0E1',
|
|
|
|
'#4DB6AC',
|
|
|
|
'#81C784',
|
|
|
|
'#8BC34A',
|
|
|
|
'#AFB42B',
|
|
|
|
'#F57F17',
|
|
|
|
'#FF5722',
|
|
|
|
'#795548',
|
|
|
|
'#455A64',
|
|
|
|
];
|
|
|
|
const bg = '#e9e9e9';
|
|
|
|
|
|
|
|
const actualSize = size - (margin * 2);
|
|
|
|
const cellSize = actualSize / n;
|
|
|
|
const sideN = Math.floor(n / 2);
|
|
|
|
|
|
|
|
/**
|
2022-01-15 17:45:48 -08:00
|
|
|
* Generate buffer of an identicon by seed
|
2019-04-15 04:37:21 -07:00
|
|
|
*/
|
2022-01-15 17:45:48 -08:00
|
|
|
export function genIdenticon(seed: string, stream: WriteStream): Promise<void> {
|
2019-04-15 04:37:21 -07:00
|
|
|
const rand = gen.create(seed);
|
2022-02-03 04:20:25 -08:00
|
|
|
const canvas = p.make(size, size, undefined);
|
2019-04-15 04:37:21 -07:00
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
|
|
|
|
ctx.fillStyle = bg;
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.fillRect(0, 0, size, size);
|
|
|
|
|
|
|
|
ctx.fillStyle = colors[rand(colors.length)];
|
|
|
|
|
|
|
|
// side bitmap (filled by false)
|
|
|
|
const side: boolean[][] = new Array(sideN);
|
|
|
|
for (let i = 0; i < side.length; i++) {
|
|
|
|
side[i] = new Array(n).fill(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1*n (filled by false)
|
|
|
|
const center: boolean[] = new Array(n).fill(false);
|
|
|
|
|
2021-11-11 17:52:10 -08:00
|
|
|
// eslint:disable-next-line:prefer-for-of
|
2019-04-15 04:37:21 -07:00
|
|
|
for (let x = 0; x < side.length; x++) {
|
|
|
|
for (let y = 0; y < side[x].length; y++) {
|
|
|
|
side[x][y] = rand(3) === 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < center.length; i++) {
|
|
|
|
center[i] = rand(3) === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
for (let x = 0; x < n; x++) {
|
|
|
|
for (let y = 0; y < n; y++) {
|
|
|
|
const isXCenter = x === ((n - 1) / 2);
|
|
|
|
if (isXCenter && !center[y]) continue;
|
|
|
|
|
|
|
|
const isLeftSide = x < ((n - 1) / 2);
|
|
|
|
if (isLeftSide && !side[x][y]) continue;
|
|
|
|
|
|
|
|
const isRightSide = x > ((n - 1) / 2);
|
|
|
|
if (isRightSide && !side[sideN - (x - sideN)][y]) continue;
|
|
|
|
|
|
|
|
const actualX = margin + (cellSize * x);
|
|
|
|
const actualY = margin + (cellSize * y);
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.fillRect(actualX, actualY, cellSize, cellSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 06:58:04 -07:00
|
|
|
return p.encodePNGToStream(canvas, stream);
|
2019-04-15 04:37:21 -07:00
|
|
|
}
|