#!/usr/bin/env node /** * 品牌 LOGO 生成器(后台登录页用) * ------------------------------------------------------------ * 输入:一张「圆形徽标 + 品牌文字」的方形品牌图(豆包 AI 生成的那种) * 输出:public/assets/img/brand-logo.png —— 只保留顶部圆形徽标, * 圆外像素置为完全透明(边缘按 alpha 平滑过渡),供登录页品牌位使用。 * * 算法(为什么这样做): * 1) 先取左上角小块均值作为「背景参考色」(源图是米白底、不是纯白,不能用 rgb>=250 判白), * 再逐行扫描「与背景色距 > 阈值」的内容像素,取**第一条连续内容行带** —— 它就是最上方的 * 圆形徽标,因此徽标下方的品牌文字(含旧站名)与右下角的 AI 水印天然被排除; * 2) 以该行带的包围盒求出圆心 / 半径,按圆生成 alpha 蒙版(1px 抗锯齿); * 3) 用「按 alpha 预乘的面积平均」重采样到目标尺寸,得到无底噪的透明 PNG。 * * 用法: * node tools/gen-brand-logo.mjs [源图] [--preview 输出图] * [源图] 默认 ~/Desktop/山间小仓.png(源图不入库,只提交生成结果) * --preview 路径 额外输出一张「深绿底 / 白底 × 52px / 140px」的合成校验图 * * 说明:脚本零依赖(仅 node 内置模块),PNG 编解码为 8bit / RGB / RGBA。 */ import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import url from 'node:url' import zlib from 'node:zlib' const ROOT = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..') /* ------------------------------------------------------------------ * * 最小 PNG 编解码(8bit:灰度 / 真彩 / 带 alpha) * ------------------------------------------------------------------ */ const SIG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) let CRC_TABLE = null function crcTable() { if (CRC_TABLE) return CRC_TABLE CRC_TABLE = new Int32Array(256) 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 CRC_TABLE[n] = c } return CRC_TABLE } function crc32(buf) { const t = crcTable() let c = 0xffffffff for (let i = 0; i < buf.length; i++) c = t[(c ^ buf[i]) & 0xff] ^ (c >>> 8) return (c ^ 0xffffffff) >>> 0 } function chunk(type, data) { const len = Buffer.alloc(4) len.writeUInt32BE(data.length, 0) const typeBuf = Buffer.from(type, 'ascii') const crc = Buffer.alloc(4) crc.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])), 0) return Buffer.concat([len, typeBuf, data, crc]) } /** 解码 PNG -> { width, height, rgba } */ function decodePng(buf) { if (!buf.slice(0, 8).equals(SIG)) throw new Error('不是有效的 PNG') let off = 8 let width = 0 let height = 0 let colorType = 0 let bitDepth = 0 const idat = [] while (off < buf.length) { const len = buf.readUInt32BE(off) const type = buf.toString('ascii', off + 4, off + 8) const data = buf.slice(off + 8, off + 8 + len) if (type === 'IHDR') { width = data.readUInt32BE(0) height = data.readUInt32BE(4) bitDepth = data[8] colorType = data[9] } else if (type === 'IDAT') idat.push(data) else if (type === 'IEND') break off += 12 + len } if (bitDepth !== 8) throw new Error('仅支持 8bit PNG') const ch = colorType === 6 ? 4 : colorType === 2 ? 3 : colorType === 4 ? 2 : 1 const raw = zlib.inflateSync(Buffer.concat(idat)) const stride = width * ch const rgba = Buffer.alloc(width * height * 4) let prev = Buffer.alloc(stride) for (let y = 0; y < height; y++) { const ft = raw[y * (stride + 1)] const line = Buffer.from(raw.slice(y * (stride + 1) + 1, y * (stride + 1) + 1 + stride)) for (let i = 0; i < stride; i++) { const a = i >= ch ? line[i - ch] : 0 const b = prev[i] const c = i >= ch ? prev[i - ch] : 0 if (ft === 1) line[i] = (line[i] + a) & 255 else if (ft === 2) line[i] = (line[i] + b) & 255 else if (ft === 3) line[i] = (line[i] + ((a + b) >> 1)) & 255 else if (ft === 4) { const p = a + b - c const pa = Math.abs(p - a) const pb = Math.abs(p - b) const pc = Math.abs(p - c) line[i] = (line[i] + (pa <= pb && pa <= pc ? a : pb <= pc ? b : c)) & 255 } } for (let x = 0; x < width; x++) { const si = x * ch const di = (y * width + x) * 4 if (ch === 4) { rgba[di] = line[si]; rgba[di + 1] = line[si + 1]; rgba[di + 2] = line[si + 2]; rgba[di + 3] = line[si + 3] } else if (ch === 3) { rgba[di] = line[si]; rgba[di + 1] = line[si + 1]; rgba[di + 2] = line[si + 2]; rgba[di + 3] = 255 } else { rgba[di] = rgba[di + 1] = rgba[di + 2] = line[si]; rgba[di + 3] = ch === 2 ? line[si + 1] : 255 } } prev = line } return { width, height, rgba } } /** 编码 RGBA -> PNG(filter 0) */ function encodePng({ width, height, rgba }) { const stride = width * 4 const raw = Buffer.alloc((stride + 1) * height) for (let y = 0; y < height; y++) { raw[y * (stride + 1)] = 0 rgba.copy(raw, y * (stride + 1) + 1, y * stride, y * stride + stride) } const ihdr = Buffer.alloc(13) ihdr.writeUInt32BE(width, 0) ihdr.writeUInt32BE(height, 4) ihdr[8] = 8 ihdr[9] = 6 return Buffer.concat([ SIG, chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw, { level: 9 })), chunk('IEND', Buffer.alloc(0)), ]) } /** * 任意尺寸重采样(按 alpha 预乘的面积平均) * 比整数倍降采样更通用:可以一次到位输出 256 / 140 / 52 等任意边长。 */ function resize(img, w, h) { const out = Buffer.alloc(w * h * 4) const sx = img.width / w const sy = img.height / h for (let y = 0; y < h; y++) { const y0 = Math.floor(y * sy) const y1 = Math.max(y0 + 1, Math.min(img.height, Math.ceil((y + 1) * sy))) for (let x = 0; x < w; x++) { const x0 = Math.floor(x * sx) const x1 = Math.max(x0 + 1, Math.min(img.width, Math.ceil((x + 1) * sx))) let r = 0 let g = 0 let b = 0 let a = 0 let n = 0 for (let yy = y0; yy < y1; yy++) { for (let xx = x0; xx < x1; xx++) { const d = (yy * img.width + xx) * 4 const al = img.rgba[d + 3] r += img.rgba[d] * al g += img.rgba[d + 1] * al b += img.rgba[d + 2] * al a += al n++ } } const o = (y * w + x) * 4 if (a === 0) { out[o] = out[o + 1] = out[o + 2] = out[o + 3] = 0 continue } out[o] = Math.round(r / a) out[o + 1] = Math.round(g / a) out[o + 2] = Math.round(b / a) out[o + 3] = Math.round(a / n) } } return { width: w, height: h, rgba: out } } /* ------------------------------------------------------------------ * * 主流程 * ------------------------------------------------------------------ */ const argv = process.argv.slice(2) const previewIdx = argv.indexOf('--preview') const previewPath = previewIdx >= 0 ? argv[previewIdx + 1] : '' const srcArg = argv.find((a) => !a.startsWith('--') && a !== previewPath) const SRC = srcArg ? path.resolve(srcArg) : path.join(os.homedir(), 'Desktop', '山间小仓.png') const OUT = path.join(ROOT, 'public', 'assets', 'img', 'brand-logo.png') const SIZE = 256 // 产物边长(登录页最大显示 140px,256 足够 2x 高清) const MARGIN = 10 // 圆形徽标与画布边缘的留白(源图分辨率下) if (!fs.existsSync(SRC)) { console.error(`❌ 源图不存在:${SRC}`) process.exit(1) } const src = decodePng(fs.readFileSync(SRC)) const W = src.width const H = src.height const rgba = src.rgba /** 背景参考色:取左上角小块均值(源图四周是米白底,水印在右下角,不会污染这里) */ const bgRgb = (() => { let r = 0 let g = 0 let b = 0 let n = 0 for (let y = 2; y < 8; y++) { for (let x = 2; x < 8; x++) { const d = (y * W + x) * 4 r += rgba[d] g += rgba[d + 1] b += rgba[d + 2] n++ } } return [r / n, g / n, b / n] })() /** * 该像素是否「有内容」 * 注意:源图不是纯白底而是米白底(如 250,246,234), * 因此不能用 rgb>=250 判定白底,必须与背景参考色比较色距。 */ const BG_TOL = 18 function isInk(x, y) { const d = (y * W + x) * 4 const dr = rgba[d] - bgRgb[0] const dg = rgba[d + 1] - bgRgb[1] const db = rgba[d + 2] - bgRgb[2] return Math.sqrt(dr * dr + dg * dg + db * db) > BG_TOL } // 1) 逐行找连续非白行带:第一条即顶部圆形徽标(天然排除下方文字与右下角水印) const GAP = 8 const bands = [] let cur = null let whiteRun = 0 for (let y = 0; y < H; y++) { let ink = false for (let x = 0; x < W; x++) { if (isInk(x, y)) { ink = true; break } } if (ink) { if (cur === null) cur = { y0: y, y1: y } else cur.y1 = y whiteRun = 0 } else if (cur !== null) { whiteRun++ if (whiteRun >= GAP) { bands.push(cur); cur = null; whiteRun = 0 } } } if (cur) bands.push(cur) if (!bands.length) { console.error('❌ 源图中未找到任何内容') process.exit(1) } const band = bands[0] let minX = W let maxX = -1 for (let y = band.y0; y <= band.y1; y++) { for (let x = 0; x < W; x++) { if (isInk(x, y)) { if (x < minX) minX = x if (x > maxX) maxX = x } } } if (maxX < 0) { console.error('❌ 第一条内容行带为空') process.exit(1) } const cx = (minX + maxX + 1) / 2 const cy = (band.y0 + band.y1 + 1) / 2 const R = Math.max(maxX - minX + 1, band.y1 - band.y0 + 1) / 2 const S = Math.ceil(R * 2) + MARGIN * 2 // 2) 按圆生成 alpha 蒙版(圆外全透明,边缘 1px 渐变) const disc = { width: S, height: S, rgba: Buffer.alloc(S * S * 4) } for (let y = 0; y < S; y++) { for (let x = 0; x < S; x++) { const dx = x + 0.5 - S / 2 const dy = y + 0.5 - S / 2 let a = R - Math.sqrt(dx * dx + dy * dy) + 0.5 if (a <= 0) continue if (a > 1) a = 1 const sx = Math.round(cx - S / 2 + x) const sy = Math.round(cy - S / 2 + y) if (sx < 0 || sy < 0 || sx >= W || sy >= H) continue const si = (sy * W + sx) * 4 const o = (y * S + x) * 4 disc.rgba[o] = rgba[si] disc.rgba[o + 1] = rgba[si + 1] disc.rgba[o + 2] = rgba[si + 2] disc.rgba[o + 3] = Math.round(a * 255) } } // 3) 重采样到目标尺寸并落盘 const out = resize(disc, SIZE, SIZE) fs.mkdirSync(path.dirname(OUT), { recursive: true }) fs.writeFileSync(OUT, encodePng(out)) let clear = 0 for (let i = 0; i < SIZE * SIZE; i++) if (out.rgba[i * 4 + 3] < 10) clear++ const ratio = clear / (SIZE * SIZE) const cornerOk = out.rgba[3] === 0 && out.rgba[(SIZE - 1) * 4 + 3] === 0 const centerOk = out.rgba[((SIZE / 2) * SIZE + SIZE / 2) * 4 + 3] > 200 console.log(`源图 :${SRC}(${W}×${H})`) console.log(`背景参考色:rgb(${bgRgb.map((v) => Math.round(v)).join(', ')})`) console.log(`内容行带:共 ${bands.length} 条 [${bands.map((b) => `y=${b.y0}~${b.y1}`).join(' / ')}],取第 1 条 x=${minX}~${maxX}`) console.log(`圆心/半径:(${cx.toFixed(1)}, ${cy.toFixed(1)}) / R=${R.toFixed(1)}`) console.log(`产物 :${path.relative(ROOT, OUT)}(${SIZE}×${SIZE},${(fs.statSync(OUT).size / 1024).toFixed(1)} KB)`) console.log(`自检 :圆外透明占比 ${(ratio * 100).toFixed(1)}% · 四角透明 ${cornerOk ? '✅' : '❌'} · 中心不透明 ${centerOk ? '✅' : '❌'}`) // 4) 可选:合成校验图(深绿底 / 白底 × 52px / 140px),人工确认缩到 52px 依然清晰 if (previewPath) { const PW = 760 const PH = 440 const canvas = { width: PW, height: PH, rgba: Buffer.alloc(PW * PH * 4) } const hex2rgb = (hex) => [ parseInt(hex.slice(1, 3), 16), parseInt(hex.slice(3, 5), 16), parseInt(hex.slice(5, 7), 16), ] const fillRect = (x0, y0, w, h, hex) => { const [r, g, b] = hex2rgb(hex) for (let y = y0; y < y0 + h; y++) { for (let x = x0; x < x0 + w; x++) { const o = (y * PW + x) * 4 canvas.rgba[o] = r canvas.rgba[o + 1] = g canvas.rgba[o + 2] = b canvas.rgba[o + 3] = 255 } } } /** source-over 合成(把带透明通道的 LOGO 叠到背景上) */ const over = (img, ox, oy) => { for (let y = 0; y < img.height; y++) { for (let x = 0; x < img.width; x++) { const si = (y * img.width + x) * 4 const a = img.rgba[si + 3] / 255 if (a === 0) continue const o = ((oy + y) * PW + ox + x) * 4 canvas.rgba[o] = Math.round(img.rgba[si] * a + canvas.rgba[o] * (1 - a)) canvas.rgba[o + 1] = Math.round(img.rgba[si + 1] * a + canvas.rgba[o + 1] * (1 - a)) canvas.rgba[o + 2] = Math.round(img.rgba[si + 2] * a + canvas.rgba[o + 2] * (1 - a)) canvas.rgba[o + 3] = 255 } } } fillRect(0, 0, PW / 2, PH, '#0a5f4d') // 左:登录页品牌栏底色 fillRect(PW / 2, 0, PW / 2, PH, '#ffffff') // 右:白色卡片底色 over(resize(out, 52, 52), 90, 90) over(resize(out, 140, 140), 46, 190) over(resize(out, 52, 52), PW / 2 + 90, 90) over(resize(out, 140, 140), PW / 2 + 46, 190) fs.writeFileSync(previewPath, encodePng(canvas)) console.log(`校验图 :${previewPath}(深绿底/白底 × 52px/140px)`) }