Files
FitOS/app/scripts/gen-icons.mjs
2026-09-04 19:06:19 +02:00

63 lines
2.1 KiB
JavaScript

// Generates simple PNG icons (solid background + circle) without dependencies.
import { deflateSync } from 'node:zlib';
import { writeFileSync, mkdirSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const outDir = join(root, 'public', 'icons');
mkdirSync(outDir, { recursive: true });
let crcTable = null;
function crc32(buf) {
if (!crcTable) {
crcTable = 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;
crcTable[n] = c;
}
}
let crc = 0xffffffff;
for (let i = 0; i < buf.length; i++) crc = crcTable[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
return (crc ^ 0xffffffff) >>> 0;
}
function chunk(type, data) {
const len = Buffer.alloc(4);
len.writeUInt32BE(data.length);
const typeBuf = Buffer.from(type, 'ascii');
const crc = Buffer.alloc(4);
crc.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
return Buffer.concat([len, typeBuf, data, crc]);
}
function makePng(size) {
const raw = Buffer.alloc(size * (1 + size * 3));
const cx = size / 2;
for (let y = 0; y < size; y++) {
raw[y * (1 + size * 3)] = 0; // filter: none
for (let x = 0; x < size; x++) {
const off = y * (1 + size * 3) + 1 + x * 3;
const dist = Math.sqrt((x - cx) ** 2 + (y - cx) ** 2);
if (dist < size * 0.32) {
raw[off] = 16; raw[off + 1] = 185; raw[off + 2] = 129; // green
} else {
raw[off] = 15; raw[off + 1] = 23; raw[off + 2] = 42; // dark
}
}
}
const sig = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(size, 0);
ihdr.writeUInt32BE(size, 4);
ihdr[8] = 8; // bit depth
ihdr[9] = 2; // color type: RGB
const idat = deflateSync(raw);
return Buffer.concat([sig, chunk('IHDR', ihdr), chunk('IDAT', idat), chunk('IEND', Buffer.alloc(0))]);
}
writeFileSync(join(outDir, 'icon-192.png'), makePng(192));
writeFileSync(join(outDir, 'icon-512.png'), makePng(512));
console.log('Icons generated:', outDir);