Files
CV_Site_Nuxt/scripts/generate-favicon.mjs
Noel Sabia 2f9f236f13 fix(favicon): keep the ICO transparent when BACKGROUND is null
The documented null option fell through to opaque black: the BGRA writer
defaulted to #000000 and hardcoded alpha 0xff.
2026-10-23 21:50:17 +02:00

111 lines
3.8 KiB
JavaScript

// 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(`<rect x="${x}" y="${y}" width="${run}" height="1" fill="${color}"/>`)
x += run
}
}
const background = BACKGROUND
? `<rect width="${SIZE}" height="${SIZE}" fill="${BACKGROUND}"/>`
: ''
return [
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${SIZE} ${SIZE}" width="${SIZE}" height="${SIZE}" shape-rendering="crispEdges">`,
...(background ? [background] : []),
...rects,
'</svg>',
// 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. With a null BACKGROUND the pixel stays
// zeroed and alpha 0, which is how a 32bpp ICO carries transparency.
let offset = 40
for (let y = SIZE - 1; y >= 0; y--) {
for (let x = 0; x < SIZE; x++) {
const color = colorAt(x, y)
const [r, g, b] = color ? hex(color) : [0, 0, 0]
bitmap[offset++] = b
bitmap[offset++] = g
bitmap[offset++] = r
bitmap[offset++] = color ? 0xff : 0
}
}
// AND mask stays zeroed: 32bpp readers use the alpha channel instead.
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}`)