frontend/generate-icons.mjs aktualisiert
This commit is contained in:
@@ -1,106 +1,101 @@
|
|||||||
// Generiert icon-192.png und icon-512.png ohne externe Abhängigkeiten
|
import { writeFileSync, mkdirSync } from 'fs';
|
||||||
// Wird automatisch vor dem Build ausgeführt (prebuild script)
|
|
||||||
import { createWriteStream } from 'fs';
|
|
||||||
import { mkdir } from 'fs/promises';
|
|
||||||
|
|
||||||
await mkdir('./public', { recursive: true });
|
mkdirSync('./public', { recursive: true });
|
||||||
|
|
||||||
function createPNG(size) {
|
function crc32(buf) {
|
||||||
// Minimales PNG: dunkel (#0d0d0f) mit DD in teal (#4ecdc4)
|
const table = new Uint32Array(256);
|
||||||
// Wir nutzen den Canvas aus dem Vite/Node Kontext nicht –
|
for (let n = 0; n < 256; n++) {
|
||||||
// stattdessen schreiben wir ein gültiges einfarbiges PNG per Hand
|
let c = n;
|
||||||
const width = size, height = size;
|
for (let k = 0; k < 8; k++) c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1);
|
||||||
|
table[n] = c;
|
||||||
function crc32(buf) {
|
|
||||||
let c, crcTable = [];
|
|
||||||
for (let n = 0; n < 256; n++) {
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
let crc = 0xffffffff;
|
||||||
function chunk(type, data) {
|
for (const b of buf) crc = (table[(crc ^ b) & 0xff] ^ (crc >>> 8));
|
||||||
const t = Buffer.from(type);
|
return ((crc ^ 0xffffffff) >>> 0);
|
||||||
const len = Buffer.alloc(4); len.writeUInt32BE(data.length);
|
|
||||||
const crcBuf = Buffer.concat([t, data]);
|
|
||||||
const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(crcBuf));
|
|
||||||
return Buffer.concat([len, t, data, crc]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// IHDR
|
|
||||||
const ihdr = Buffer.alloc(13);
|
|
||||||
ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4);
|
|
||||||
ihdr[8] = 8; ihdr[9] = 2; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0;
|
|
||||||
|
|
||||||
// Pixel data: dark background with a teal circle
|
|
||||||
const rows = [];
|
|
||||||
const cx = width / 2, cy = height / 2, r = width * 0.35;
|
|
||||||
const r2 = width * 0.28;
|
|
||||||
for (let y = 0; y < height; y++) {
|
|
||||||
const row = [0]; // filter byte
|
|
||||||
for (let x = 0; x < width; x++) {
|
|
||||||
const dx = x - cx, dy = y - cy;
|
|
||||||
const dist = Math.sqrt(dx*dx + dy*dy);
|
|
||||||
if (dist < r2) {
|
|
||||||
// teal inner circle
|
|
||||||
row.push(0x4e, 0xcd, 0xc4);
|
|
||||||
} else if (dist < r) {
|
|
||||||
// slightly lighter ring
|
|
||||||
row.push(0x1a, 0x8a, 0x84);
|
|
||||||
} else {
|
|
||||||
// dark background
|
|
||||||
row.push(0x0d, 0x0d, 0x0f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rows.push(Buffer.from(row));
|
|
||||||
}
|
|
||||||
|
|
||||||
// zlib compress (deflate) – minimal uncompressed blocks
|
|
||||||
const raw = Buffer.concat(rows);
|
|
||||||
// Split into 65535-byte chunks for uncompressed deflate
|
|
||||||
const chunks = [];
|
|
||||||
const CHUNK = 65535;
|
|
||||||
for (let i = 0; i < raw.length; i += CHUNK) {
|
|
||||||
const slice = raw.slice(i, i + CHUNK);
|
|
||||||
const last = i + CHUNK >= raw.length ? 1 : 0;
|
|
||||||
const hdr = Buffer.alloc(5);
|
|
||||||
hdr[0] = last; hdr.writeUInt16LE(slice.length, 1); hdr.writeUInt16LE(~slice.length & 0xffff, 3);
|
|
||||||
chunks.push(hdr, slice);
|
|
||||||
}
|
|
||||||
const deflated = Buffer.concat(chunks);
|
|
||||||
|
|
||||||
// Adler32
|
|
||||||
let s1 = 1, s2 = 0;
|
|
||||||
for (const b of raw) { s1 = (s1 + b) % 65521; s2 = (s2 + s1) % 65521; }
|
|
||||||
const adler = Buffer.alloc(4); adler.writeUInt32BE((s2 << 16) | s1);
|
|
||||||
|
|
||||||
const zlib = Buffer.concat([
|
|
||||||
Buffer.from([0x78, 0x01]), // zlib header (no compression)
|
|
||||||
deflated,
|
|
||||||
adler
|
|
||||||
]);
|
|
||||||
|
|
||||||
const png = Buffer.concat([
|
|
||||||
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), // PNG signature
|
|
||||||
chunk('IHDR', ihdr),
|
|
||||||
chunk('IDAT', zlib),
|
|
||||||
chunk('IEND', Buffer.alloc(0))
|
|
||||||
]);
|
|
||||||
|
|
||||||
return png;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const png192 = createPNG(192);
|
function chunk(type, data) {
|
||||||
const png512 = createPNG(512);
|
const t = Buffer.from(type, 'ascii');
|
||||||
|
const len = Buffer.allocUnsafe(4);
|
||||||
|
len.writeUInt32BE(data.length, 0);
|
||||||
|
const crcInput = Buffer.concat([t, data]);
|
||||||
|
const crcBuf = Buffer.allocUnsafe(4);
|
||||||
|
crcBuf.writeUInt32BE(crc32(crcInput), 0);
|
||||||
|
return Buffer.concat([len, t, data, crcBuf]);
|
||||||
|
}
|
||||||
|
|
||||||
const ws192 = createWriteStream('./public/icon-192.png');
|
function deflateStore(raw) {
|
||||||
ws192.write(png192); ws192.end();
|
// zlib header (deflate, no compression)
|
||||||
|
const header = Buffer.from([0x78, 0x01]);
|
||||||
|
const blocks = [];
|
||||||
|
const SIZE = 32768;
|
||||||
|
for (let i = 0; i < raw.length; i += SIZE) {
|
||||||
|
const slice = raw.slice(i, i + SIZE);
|
||||||
|
const last = (i + SIZE >= raw.length) ? 1 : 0;
|
||||||
|
const h = Buffer.allocUnsafe(5);
|
||||||
|
h[0] = last;
|
||||||
|
h.writeUInt16LE(slice.length, 1);
|
||||||
|
h.writeUInt16LE((~slice.length) & 0xffff, 3);
|
||||||
|
blocks.push(h, slice);
|
||||||
|
}
|
||||||
|
// Adler-32
|
||||||
|
let s1 = 1, s2 = 0;
|
||||||
|
for (const b of raw) {
|
||||||
|
s1 = (s1 + b) % 65521;
|
||||||
|
s2 = (s2 + s1) % 65521;
|
||||||
|
}
|
||||||
|
const adler = Buffer.allocUnsafe(4);
|
||||||
|
adler.writeUInt32BE(((s2 << 16) | s1) >>> 0, 0);
|
||||||
|
return Buffer.concat([header, ...blocks, adler]);
|
||||||
|
}
|
||||||
|
|
||||||
const ws512 = createWriteStream('./public/icon-512.png');
|
function makePNG(size) {
|
||||||
ws512.write(png512); ws512.end();
|
const cx = size / 2, cy = size / 2;
|
||||||
|
const outerR = size * 0.42, innerR = size * 0.30;
|
||||||
|
|
||||||
console.log('✅ Icons generiert: icon-192.png, icon-512.png');
|
// IHDR
|
||||||
|
const ihdr = Buffer.allocUnsafe(13);
|
||||||
|
ihdr.writeUInt32BE(size, 0);
|
||||||
|
ihdr.writeUInt32BE(size, 4);
|
||||||
|
ihdr[8] = 8; // bit depth
|
||||||
|
ihdr[9] = 2; // RGB
|
||||||
|
ihdr[10] = ihdr[11] = ihdr[12] = 0;
|
||||||
|
|
||||||
|
// Raw pixel data (filter byte 0 per row)
|
||||||
|
const rows = [];
|
||||||
|
for (let y = 0; y < size; y++) {
|
||||||
|
const row = Buffer.allocUnsafe(1 + size * 3);
|
||||||
|
row[0] = 0; // filter none
|
||||||
|
for (let x = 0; x < size; x++) {
|
||||||
|
const dx = x - cx, dy = y - cy;
|
||||||
|
const d = Math.sqrt(dx * dx + dy * dy);
|
||||||
|
let r, g, b;
|
||||||
|
if (d < innerR) {
|
||||||
|
r = 0x4e; g = 0xcd; b = 0xc4; // teal
|
||||||
|
} else if (d < outerR) {
|
||||||
|
r = 0x1e; g = 0x6a; b = 0x65; // dark teal ring
|
||||||
|
} else {
|
||||||
|
r = 0x0d; g = 0x0d; b = 0x0f; // background
|
||||||
|
}
|
||||||
|
row[1 + x * 3] = r;
|
||||||
|
row[2 + x * 3] = g;
|
||||||
|
row[3 + x * 3] = b;
|
||||||
|
}
|
||||||
|
rows.push(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
const raw = Buffer.concat(rows);
|
||||||
|
const idat = deflateStore(raw);
|
||||||
|
|
||||||
|
const sig = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
||||||
|
return Buffer.concat([
|
||||||
|
sig,
|
||||||
|
chunk('IHDR', ihdr),
|
||||||
|
chunk('IDAT', idat),
|
||||||
|
chunk('IEND', Buffer.alloc(0)),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
writeFileSync('./public/icon-192.png', makePNG(192));
|
||||||
|
writeFileSync('./public/icon-512.png', makePNG(512));
|
||||||
|
console.log('✅ Icons generiert: icon-192.png + icon-512.png');
|
||||||
|
|||||||
Reference in New Issue
Block a user