Files
simplescan-solo/scripts/make-icons.cjs
T

66 lines
1.9 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const iconsDir = path.join(__dirname, 'icons');
fs.mkdirSync(iconsDir, { recursive: true });
function crc32(buf) {
const table = [];
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 (const byte of buf) {
crc = table[(crc ^ byte) & 0xFF] ^ (crc >>> 8);
}
return (crc ^ -1) >>> 0;
}
function makeChunk(typeStr, data) {
const typeBuf = Buffer.from(typeStr, 'ascii');
const c = crc32(Buffer.concat([typeBuf, data]));
const crcBuf = Buffer.alloc(4);
crcBuf.writeUInt32BE(c, 0);
const lenBuf = Buffer.alloc(4);
lenBuf.writeUInt32BE(data.length, 0);
return Buffer.concat([lenBuf, typeBuf, data, crcBuf]);
}
function makePng(size, r, g, b) {
const sig = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
const ihdrData = Buffer.alloc(13);
ihdrData.writeUInt32BE(size, 0);
ihdrData.writeUInt32BE(size, 4);
ihdrData.writeUInt8(8, 8);
ihdrData.writeUInt8(2, 9);
ihdrData.writeUInt8(0, 10);
ihdrData.writeUInt8(0, 11);
ihdrData.writeUInt8(0, 12);
const rawData = [];
for (let y = 0; y < size; y++) {
rawData.push(0);
for (let x = 0; x < size; x++) {
rawData.push(r, g, b);
}
}
const imageData = zlib.deflateSync(Buffer.from(rawData));
const ihdr = makeChunk('IHDR', ihdrData);
const idat = makeChunk('IDAT', imageData);
const iend = makeChunk('IEND', Buffer.alloc(0));
return Buffer.concat([sig, ihdr, idat, iend]);
}
const r = 15, g = 52, b = 96;
fs.writeFileSync(path.join(iconsDir, 'icon16.png'), makePng(16, r, g, b));
fs.writeFileSync(path.join(iconsDir, 'icon48.png'), makePng(48, r, g, b));
fs.writeFileSync(path.join(iconsDir, 'icon128.png'), makePng(128, r, g, b));
console.log('Icons created');