import { mkdirSync, writeFileSync } from "node:fs"; import { deflateSync } from "node:zlib"; const spells = [ { id: "spell-light", rgb: [255, 248, 192] }, { id: "spell-spark", rgb: [255, 144, 64] } ]; mkdirSync(new URL("../assets/fx/projectile/", import.meta.url), { recursive: true }); for (const spell of spells) { for (let frame = 0; frame < 3; frame++) { const png = buildPng5(spell.rgb); writeFileSync( new URL(`../assets/fx/projectile/${spell.id}-${frame}.png`, import.meta.url), png ); } } function buildPng5 ([r, g, b]) { const row = Buffer.alloc(1 + 5 * 4); row[0] = 0; for (let x = 0; x < 5; x++) { const i = 1 + x * 4; row[i] = r; row[i + 1] = g; row[i + 2] = b; row[i + 3] = x === 0 || x === 4 ? 0 : 255; } const raw = Buffer.concat(Array.from({ length: 5 }, () => row)); return encodePng(5, 5, deflateSync(raw)); } function encodePng (w, h, idat) { const signature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]); const ihdr = chunk("IHDR", (() => { const buf = Buffer.alloc(13); buf.writeUInt32BE(w, 0); buf.writeUInt32BE(h, 4); buf[8] = 8; buf[9] = 6; return buf; })()); return Buffer.concat([signature, ihdr, chunk("IDAT", idat), chunk("IEND", Buffer.alloc(0))]); } function chunk (type, data) { const len = Buffer.alloc(4); len.writeUInt32BE(data.length, 0); const t = Buffer.from(type); const crc = crc32(Buffer.concat([t, data])); const c = Buffer.alloc(4); c.writeUInt32BE(crc >>> 0, 0); return Buffer.concat([len, t, data, c]); } function crc32 (buf) { let c = ~0; for (let i = 0; i < buf.length; i++) { c ^= buf[i]; for (let k = 0; k < 8; k++) { c = (c >>> 1) ^ (c & 1 ? 0xedb88320 : 0); } } return ~c; }