Files
2026-11-11 20:24:24 +01:00

121 lines
4.2 KiB
JavaScript

// Generates the Tab Park toolbar icons: a tab parked inside a parking bay.
// Flat shapes, no gradients, and a simplified 16/32px version so the bay
// outline and the tab still read in the toolbar.
const zlib = require('zlib');
const fs = require('fs');
const path = require('path');
function crc32(buf) {
let table = crc32.table;
if (!table) {
table = crc32.table = new Int32Array(256);
for (let n = 0; n < 256; n++) {
let c = n;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
table[n] = c;
}
}
let c = 0xffffffff;
for (let i = 0; i < buf.length; i++) c = table[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
}
function chunk(type, data) {
const len = Buffer.alloc(4);
len.writeUInt32BE(data.length);
const body = Buffer.concat([Buffer.from(type, 'ascii'), data]);
const crc = Buffer.alloc(4);
crc.writeUInt32BE(crc32(body));
return Buffer.concat([len, body, crc]);
}
function png(width, height, rgba) {
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(width, 0);
ihdr.writeUInt32BE(height, 4);
ihdr[8] = 8; ihdr[9] = 6; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0;
const raw = Buffer.alloc((width * 4 + 1) * height);
for (let y = 0; y < height; y++) {
raw[y * (width * 4 + 1)] = 0;
rgba.copy(raw, y * (width * 4 + 1) + 1, y * width * 4, (y + 1) * width * 4);
}
return Buffer.concat([
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
chunk('IHDR', ihdr),
chunk('IDAT', zlib.deflateSync(raw, { level: 9 })),
chunk('IEND', Buffer.alloc(0))
]);
}
function inRoundedRect(x, y, x0, y0, x1, y1, r) {
const cx = Math.max(x0 + r, Math.min(x1 - r, x));
const cy = Math.max(y0 + r, Math.min(y1 - r, y));
return Math.hypot(x - cx, y - cy) <= r;
}
function render(size) {
const rgba = Buffer.alloc(size * size * 4);
const bg = [24, 42, 68];
const bay = [242, 244, 248];
const tab = [201, 154, 78];
const tabTop = [169, 124, 54];
const fold = [138, 100, 40];
const radius = size * 0.22;
const bayOuter = { x0: size * 0.12, y0: size * 0.12, x1: size * 0.88, y1: size * 0.88, r: size * 0.10 };
const stroke = Math.max(size * 0.055, 1.1);
const bayInner = {
x0: bayOuter.x0 + stroke,
y0: bayOuter.y0 + stroke,
x1: bayOuter.x1 - stroke,
y1: bayOuter.y1 - stroke,
r: Math.max(bayOuter.r - stroke, 0)
};
const tabRect = { x0: size * 0.22, y0: size * 0.24, x1: size * 0.78, y1: size * 0.76, r: size * 0.055 };
const barBottom = tabRect.y0 + size * 0.10;
const foldSize = size * 0.12;
const showFold = size >= 32;
const samples = 4;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
let r = 0, g = 0, b = 0, covered = 0;
for (let sy = 0; sy < samples; sy++) {
for (let sx = 0; sx < samples; sx++) {
const px = x + (sx + 0.5) / samples;
const py = y + (sy + 0.5) / samples;
if (!inRoundedRect(px, py, 0, 0, size, size, radius)) continue;
let color = bg;
const inBayOuter = inRoundedRect(px, py, bayOuter.x0, bayOuter.y0, bayOuter.x1, bayOuter.y1, bayOuter.r);
const inBayInner = inRoundedRect(px, py, bayInner.x0, bayInner.y0, bayInner.x1, bayInner.y1, bayInner.r);
if (inBayOuter && !inBayInner) color = bay;
const inTab = inRoundedRect(px, py, tabRect.x0, tabRect.y0, tabRect.x1, tabRect.y1, tabRect.r);
if (inTab) {
color = py < barBottom ? tabTop : tab;
if (showFold && (px - tabRect.x0) + (py - tabRect.y0) < foldSize) {
color = fold;
}
}
r += color[0]; g += color[1]; b += color[2]; covered++;
}
}
const i = (y * size + x) * 4;
rgba[i] = covered ? Math.round(r / covered) : 0;
rgba[i + 1] = covered ? Math.round(g / covered) : 0;
rgba[i + 2] = covered ? Math.round(b / covered) : 0;
rgba[i + 3] = Math.round(255 * covered / (samples * samples));
}
}
return png(size, size, rgba);
}
const dir = path.join(__dirname, '..', 'icons');
fs.mkdirSync(dir, { recursive: true });
for (const s of [16, 32, 48, 96, 128]) {
fs.writeFileSync(path.join(dir, `icon-${s}.png`), render(s));
console.log(`wrote icon-${s}.png`);
}