// Generates public/favicon.svg and public/favicon.ico from the pixel avatar in // app/utils/avatar.js, so the sprite exists once. Run with `pnpm favicon`. // // No image library: the sprite is 32x32 cells, so the ICO is written as an // uncompressed 32bpp BMP inside a one-image ICO container (4.2 kB), and the SVG // merges each run of same-coloured cells per row into one rect. import { mkdirSync, writeFileSync } from 'node:fs' import { join } from 'node:path' import { AVATAR_PALETTE, AVATAR_SPRITE } from '../app/utils/avatar.js' const SIZE = 32 // Same chip colour the logo sits on in the header, so the portrait stays // readable on a dark browser tab bar. Set to null for a transparent icon. const BACKGROUND = '#1b2b3f' const OUT_DIR = join(import.meta.dirname, '..', 'public') const hex = (value) => { const n = Number.parseInt(value.slice(1), 16) return [n >> 16 & 0xff, n >> 8 & 0xff, n & 0xff] } const colorAt = (x, y) => { const cell = AVATAR_SPRITE[y]?.[x] ?? '.' return AVATAR_PALETTE[cell] ?? BACKGROUND } // --- SVG ------------------------------------------------------------------- const svg = () => { const rects = [] for (let y = 0; y < SIZE; y++) { let x = 0 while (x < SIZE) { const color = colorAt(x, y) let run = 1 while (x + run < SIZE && colorAt(x + run, y) === color) run++ if (color && color !== BACKGROUND) rects.push(``) x += run } } const background = BACKGROUND ? `` : '' return [ ``, ...(background ? [background] : []), ...rects, '', // Keep the trailing newline: the end-of-file-fixer pre-commit hook wants it. '', ].join('\n') } // --- ICO ------------------------------------------------------------------- const ico = () => { const xorSize = SIZE * SIZE * 4 const maskRow = Math.ceil(SIZE / 32) * 4 const maskSize = maskRow * SIZE const bitmap = Buffer.alloc(40 + xorSize + maskSize) bitmap.writeUInt32LE(40, 0) // BITMAPINFOHEADER size bitmap.writeInt32LE(SIZE, 4) // width bitmap.writeInt32LE(SIZE * 2, 8) // height: XOR image + AND mask bitmap.writeUInt16LE(1, 12) // planes bitmap.writeUInt16LE(32, 14) // bits per pixel bitmap.writeUInt32LE(xorSize + maskSize, 20) // image size // XOR bitmap: bottom-up rows of BGRA. let offset = 40 for (let y = SIZE - 1; y >= 0; y--) { for (let x = 0; x < SIZE; x++) { const [r, g, b] = hex(colorAt(x, y) ?? '#000000') bitmap[offset++] = b bitmap[offset++] = g bitmap[offset++] = r bitmap[offset++] = 0xff } } // AND mask stays zeroed: every pixel is opaque. const header = Buffer.alloc(6) header.writeUInt16LE(0, 0) // reserved header.writeUInt16LE(1, 2) // type: icon header.writeUInt16LE(1, 4) // image count const entry = Buffer.alloc(16) entry[0] = SIZE // width entry[1] = SIZE // height entry[2] = 0 // palette colours entry[3] = 0 // reserved entry.writeUInt16LE(1, 4) // planes entry.writeUInt16LE(32, 6) // bits per pixel entry.writeUInt32LE(bitmap.length, 8) entry.writeUInt32LE(header.length + entry.length, 12) return Buffer.concat([header, entry, bitmap]) } // --- write ----------------------------------------------------------------- mkdirSync(OUT_DIR, { recursive: true }) const svgFile = join(OUT_DIR, 'favicon.svg') const icoFile = join(OUT_DIR, 'favicon.ico') writeFileSync(svgFile, svg()) writeFileSync(icoFile, ico()) console.log(`wrote ${svgFile} and ${icoFile}`)