winzik/skeleton-chrome/scripts/icons.cjs

88 lines
3.7 KiB
JavaScript

// Draws the extension's icons (icons/icon-16/32/48/128.png): a rounded square, lines of text, one of them highlighted.
// Pure Node, no package: run by `bin/jumphp chrome:icons <folder> [#background] [#highlight]` (in Docker), or
// `node scripts/icons.cjs [#background] [#highlight]` where Node exists. Replace them with your own art when you have it:
// the Chrome Web Store wants a 128x128 PNG whose drawing is 96x96 with 16 px of transparent margin.
'use strict';
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const hex = (s, fallback) => {
const m = /^#?([0-9a-f]{6})$/i.exec(s || '') || /^#?([0-9a-f]{6})$/i.exec(fallback);
return [0, 2, 4].map((i) => parseInt(m[1].slice(i, i + 2), 16));
};
const background = hex(process.argv[2], '#5b4bdb');
const highlight = hex(process.argv[3], '#ffe066');
const white = [255, 255, 255];
const crcTable = Array.from({ length: 256 }, (_, n) => {
let c = n;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
return c >>> 0;
});
const crc32 = (buf) => {
let c = 0xffffffff;
for (const b of buf) c = crcTable[(c ^ b) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
};
const chunk = (type, data) => {
const len = Buffer.alloc(4);
len.writeUInt32BE(data.length);
const body = Buffer.concat([Buffer.from(type), data]);
const crc = Buffer.alloc(4);
crc.writeUInt32BE(crc32(body));
return Buffer.concat([len, body, crc]);
};
function png(size, rgba) {
const header = Buffer.alloc(13);
header.writeUInt32BE(size, 0);
header.writeUInt32BE(size, 4);
header.set([8, 6, 0, 0, 0], 8);
const raw = Buffer.alloc((size * 4 + 1) * size);
for (let y = 0; y < size; y++) rgba.copy(raw, y * (size * 4 + 1) + 1, y * size * 4, (y + 1) * size * 4);
return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', header),
chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]);
}
/** The colour at a point of the 0..1 square (null = transparent). */
function shade(x, y, margin) {
const inside = (x0, y0, x1, y1, r) => {
const cx = Math.min(Math.max(x, x0 + r), x1 - r);
const cy = Math.min(Math.max(y, y0 + r), y1 - r);
return (x - cx) ** 2 + (y - cy) ** 2 <= r * r && x >= x0 && x <= x1 && y >= y0 && y <= y1;
};
const a = margin, b = 1 - margin;
if (!inside(a, a, b, b, 0.2)) return null;
const w = b - a;
const line = (row) => a + w * row;
if (inside(a + w * 0.18, line(0.42), a + w * 0.82, line(0.6), 0.04)) {
return inside(a + w * 0.24, line(0.47), a + w * 0.7, line(0.55), 0.03) ? background : highlight;
}
if (inside(a + w * 0.2, line(0.24), a + w * 0.8, line(0.32), 0.035)) return white;
if (inside(a + w * 0.2, line(0.7), a + w * 0.62, line(0.78), 0.035)) return white;
return background;
}
const dir = path.join(process.argv[4] || path.join(__dirname, '..'), 'icons');
fs.mkdirSync(dir, { recursive: true });
for (const size of [16, 32, 48, 128]) {
const margin = size === 128 ? 16 / 128 : size >= 48 ? 2 / 48 : 0;
const rgba = Buffer.alloc(size * size * 4);
const ss = 4;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const sum = [0, 0, 0, 0];
for (let j = 0; j < ss; j++) {
for (let i = 0; i < ss; i++) {
const c = shade((x + (i + 0.5) / ss) / size, (y + (j + 0.5) / ss) / size, margin);
if (c) { sum[0] += c[0]; sum[1] += c[1]; sum[2] += c[2]; sum[3]++; }
}
}
const o = (y * size + x) * 4;
if (sum[3]) rgba.set([sum[0] / sum[3], sum[1] / sum[3], sum[2] / sum[3], (255 * sum[3]) / (ss * ss)].map(Math.round), o);
}
}
fs.writeFileSync(path.join(dir, `icon-${size}.png`), png(size, rgba));
console.log(`icons/icon-${size}.png`);
}