petstore-frontend/gen_icons.js
2026-04-12 22:57:48 +08:00

55 lines
1.7 KiB
JavaScript

const fs = require('fs');
const zlib = require('zlib');
function crc32(buf) {
let crc = 0xffffffff;
const table = [];
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;
}
for (let i = 0; i < buf.length; i++) crc = table[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
return (crc ^ 0xffffffff) >>> 0;
}
function chunk(type, data) {
const typeB = Buffer.from(type);
const lenB = Buffer.alloc(4);
lenB.writeUInt32BE(data.length, 0);
const crcB = Buffer.alloc(4);
crcB.writeUInt32BE(crc32(Buffer.concat([typeB, data])), 0);
return Buffer.concat([lenB, typeB, data, crcB]);
}
function makePng(r, g, b) {
const w = 81, h = 81;
const sig = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
const ihdrData = Buffer.alloc(13);
ihdrData.writeUInt32BE(w, 0);
ihdrData.writeUInt32BE(h, 4);
ihdrData[8] = 8; ihdrData[9] = 2;
const ihdr = chunk('IHDR', ihdrData);
const raw = [];
for (let y = 0; y < h; y++) {
raw.push(0);
for (let x = 0; x < w; x++) {
raw.push(r, g, b);
}
}
const compressed = zlib.deflateSync(Buffer.from(raw), { level: 9 });
const idat = chunk('IDAT', compressed);
const iend = chunk('IEND', Buffer.alloc(0));
return Buffer.concat([sig, ihdr, idat, iend]);
}
const dir = __dirname + '/src/static/';
fs.writeFileSync(dir + 'tab-home.png', makePng(153, 153, 153));
fs.writeFileSync(dir + 'tab-home-active.png', makePng(7, 193, 96));
fs.writeFileSync(dir + 'tab-report.png', makePng(153, 153, 153));
fs.writeFileSync(dir + 'tab-report-active.png', makePng(7, 193, 96));
fs.writeFileSync(dir + 'tab-mine.png', makePng(153, 153, 153));
fs.writeFileSync(dir + 'tab-mine-active.png', makePng(7, 193, 96));
console.log('done');