Files
invoicechaser/scripts/gen-icons.cjs
T

78 lines
2.2 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
function crc32(buf) {
const table = new Int32Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let k = 0; k < 8; k++) {
c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1);
}
table[i] = c;
}
let crc = -1;
for (let i = 0; i < buf.length; i++) {
crc = table[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
}
return (crc ^ -1) >>> 0;
}
function makeChunk(type, data) {
const typeBuf = Buffer.from(type, "ascii");
const lenBuf = Buffer.alloc(4);
lenBuf.writeUInt32BE(data.length, 0);
const crcData = Buffer.concat([typeBuf, data]);
const crcBuf = Buffer.alloc(4);
crcBuf.writeUInt32BE(crc32(crcData), 0);
return Buffer.concat([lenBuf, typeBuf, data, crcBuf]);
}
function createSolidPng(size, r, g, b) {
const width = size;
const height = size;
const bitDepth = 8;
const colorType = 2; // RGB
const signature = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
const ihdrData = Buffer.alloc(13);
ihdrData.writeUInt32BE(width, 0);
ihdrData.writeUInt32BE(height, 4);
ihdrData.writeUInt8(bitDepth, 8);
ihdrData.writeUInt8(colorType, 9);
ihdrData.writeUInt8(0, 10);
ihdrData.writeUInt8(0, 11);
ihdrData.writeUInt8(0, 12);
const ihdrChunk = makeChunk("IHDR", ihdrData);
const rowSize = 1 + width * 3;
const imageData = Buffer.alloc(rowSize * height);
for (let y = 0; y < height; y++) {
imageData[y * rowSize] = 0;
for (let x = 0; x < width; x++) {
const offset = y * rowSize + 1 + x * 3;
imageData[offset] = r;
imageData[offset + 1] = g;
imageData[offset + 2] = b;
}
}
const compressed = zlib.deflateRawSync(imageData);
const idatChunk = makeChunk("IDAT", compressed);
const iendChunk = makeChunk("IEND", Buffer.alloc(0));
return Buffer.concat([signature, ihdrChunk, idatChunk, iendChunk]);
}
const iconsDir = path.join(__dirname, "..", "src", "icons");
fs.mkdirSync(iconsDir, { recursive: true });
const sizes = [16, 48, 128];
for (const size of sizes) {
const png = createSolidPng(size, 34, 197, 94);
fs.writeFileSync(path.join(iconsDir, `icon${size}.png`), png);
}
console.log("Icons generated successfully");