/** * 配图资源生成器 v3 * ------------------------------------------------------------ * 1. 每个图标用同一份 SVG 渲染「白底」「黑底」两次 * 2. 通过两式联立解出真实 alpha(见 tools/png-util.mjs)→ 得到精确抗锯齿的透明 PNG * 3. 徽章类图形使用「垂直渐变圆角底」,提升质感与统一性 * 运行: node tools/gen-icons.mjs */ import fs from 'node:fs' import path from 'node:path' import os from 'node:os' import { execFileSync } from 'node:child_process' import url from 'node:url' import { ART, OUTPUTS } from './icon-svg.mjs' import { decodePng, encodePng, extractAlpha, transparentRatio, contentBox, crop, downscale } from './png-util.mjs' const ROOT = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..') const OUT_DIR = path.join(ROOT, 'static', 'icons') const SIZE = 384 // 渲染分辨率 const TARGET = 192 // 最终输出边长(2 倍降采样,锐度与体积兼顾) const BADGE_SCALE = 0.66 const BADGE_OFFSET = (96 - 96 * BADGE_SCALE) / 2 const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'fc-illus-')) const SRC = path.join(TMP, 'src') const RST = path.join(TMP, 'out') fs.mkdirSync(SRC, { recursive: true }) fs.mkdirSync(RST, { recursive: true }) fs.mkdirSync(OUT_DIR, { recursive: true }) /** 调色板替换 */ function paint(art, item) { const p = { c1: item.c1, c2: item.c2 || item.c1, c3: item.c3 || '#FFFFFF' } return art.replace(/\{(c1|c2|c3)\}/g, (_, k) => p[k]) } /** 生成一版 SVG:backdrop 用于两次渲染(白 / 黑) */ function buildSvg(item, backdrop) { const body = paint(ART[item.art], item) const head = `` // 底色层:徽章用渐变圆角块,其余用纯色 backdrop 便于抠出 alpha let layer if (item.bg) { layer = `` + `` + `` + `` } else { layer = '' } const base = `` // 徽章类:插画缩到 66% 居中,四周留出圆角底 // 透明类:插画铺满画布,保证在 CSS 给定尺寸下的视觉重量 const inner = item.bg ? `${body}` : body return head + base + layer + inner + '' } /** 品牌 LOGO */ const LOGO_SVG = ` ` // 1. 生成两版 SVG const names = [] for (const item of OUTPUTS) { const base = item.file.replace(/\.png$/, '') fs.writeFileSync(path.join(SRC, `${base}__w.svg`), buildSvg(item, '#FFFFFF'), 'utf8') fs.writeFileSync(path.join(SRC, `${base}__b.svg`), buildSvg(item, '#000000'), 'utf8') names.push(base) } fs.writeFileSync(path.join(SRC, 'logo__w.svg'), LOGO_SVG.replace(' path.join(SRC, n)) try { execFileSync('qlmanage', ['-t', '-s', String(size), '-o', RST, ...chunk], { stdio: 'ignore' }) } catch (e) { /* 忽略,后续按产物校验 */ } } } render(names.map((n) => `${n}__w.svg`).concat(names.map((n) => `${n}__b.svg`)), SIZE) render(['logo__w.svg'], 512) // 3. 两式联立 -> 透明 PNG let ok = 0 const missing = [] const lowAlpha = [] const sizes = new Set() for (const n of names) { const fw = path.join(RST, `${n}__w.svg.png`) const fb = path.join(RST, `${n}__b.svg.png`) if (!fs.existsSync(fw) || !fs.existsSync(fb)) { missing.push(n); continue } const black = decodePng(fs.readFileSync(fb)) // qlmanage 会把内容渲染到子矩形并补白衬,先用黑底图定位真实内容区 const box = contentBox(black) let img = extractAlpha(crop(decodePng(fs.readFileSync(fw)), box), crop(black, box)) // 整数倍降采样到目标尺寸 const factor = Math.max(1, Math.floor(img.width / TARGET)) img = downscale(img, factor) const ratio = transparentRatio(img) sizes.add(`${img.width}x${img.height}`) // 非徽章类图标应当有明显透明区域(徽章类自带圆角底,透明仅出现在四角) const item = OUTPUTS.find((o) => o.file === `${n}.png`) if (!item.bg && ratio < 0.1) lowAlpha.push(`${n}(${ratio.toFixed(2)})`) fs.writeFileSync(path.join(OUT_DIR, `${n}.png`), encodePng(img)) ok++ } // 4. LOGO(白底渲染 -> 裁剪 -> 直接使用,自身不透明) const logoW = path.join(RST, 'logo__w.svg.png') if (fs.existsSync(logoW)) { const limg = decodePng(fs.readFileSync(logoW)) fs.writeFileSync(path.join(ROOT, 'static', 'logo.png'), encodePng(crop(limg, contentBox(limg)))) } const legacy = path.join(ROOT, 'static', 'logo-512.png') if (fs.existsSync(legacy)) fs.rmSync(legacy, { force: true }) fs.rmSync(TMP, { recursive: true, force: true }) console.log(`✅ 配图生成完成:${ok}/${names.length} 个 → static/icons/(${[...sizes].join(', ')},真实透明)`) if (missing.length) console.log(`⚠️ 未生成:${missing.join(', ')}`) if (lowAlpha.length) console.log(`⚠️ 透明区域异常:${lowAlpha.join(', ')}`) console.log('✅ LOGO 生成:static/logo.png')