| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // 面板小标题图标统一化:
- // 1. 从 E:\&项目模型文件等\UI图片\Texture\Icon 取维护者指定的 I/Pipe/Q 系列切图;
- // 2. 去色(转灰度,保 alpha);
- // 3. 用项目调色盘主蓝 #01abea(--cyan)整体染色——灰度值作为亮度系数,
- // 保留原图明暗层次(渐变高光仍在),只替换色相;
- // 4. 输出到 public/cockpit/assets/icons/panel/*.png(256×256,1x)。
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs'
- import { inflateSync, deflateSync } from 'node:zlib'
- const SRC = 'E:/&项目模型文件等/UI图片/Texture/Icon'
- const OUT = 'public/cockpit/assets/icons/panel'
- mkdirSync(OUT, { recursive: true })
- // 面板语义 → 源图(维护者规定:I=次要、Pipe=主要、Q=补充)
- const MAP = {
- 'icon.panel.situation': { src: 'I图标设备.png', name: 'panel-situation.png', meaning: '态势总览(设备机柜)' },
- 'icon.panel.infrastructure': { src: 'I图标设备.png', name: 'panel-infrastructure.png', meaning: '基础设施(设备机柜)' },
- 'icon.panel.monitoring': { src: 'I图标在线.png', name: 'panel-monitoring.png', meaning: '运行监测(在线)' },
- 'icon.panel.alert': { src: 'I图标报警等级.png', name: 'panel-alert.png', meaning: '预警设置(报警等级)' },
- 'icon.panel.archive': { src: 'I图标设备开发.png', name: 'panel-archive.png', meaning: '基础档案(设备开发)' },
- 'icon.panel.operation': { src: 'I图标在线.png', name: 'panel-operation.png', meaning: '运行信息(在线)' },
- 'icon.panel.management': { src: 'I图标设备开发.png', name: 'panel-management.png', meaning: '管理信息(设备开发)' },
- 'icon.panel.emergency': { src: 'I图标报警产生.png', name: 'panel-emergency.png', meaning: '应急保障(报警产生)' },
- 'icon.panel.overview': { src: 'I图标在线.png', name: 'panel-overview.png', meaning: '监测概览(在线)' },
- 'icon.panel.distribution': { src: 'I图标资源整合.png', name: 'panel-distribution.png', meaning: '监测分布(资源整合)' },
- 'icon.panel.ledger': { src: 'I图标设备.png', name: 'panel-ledger.png', meaning: '监测点台账(设备)' },
- 'icon.panel.current': { src: 'I图标报警产生.png', name: 'panel-current.png', meaning: '当前预警(报警产生)' },
- 'icon.panel.history': { src: 'Q图标时间.png', name: 'panel-history.png', meaning: '历史分析(时间)' },
- 'icon.panel.efficiency': { src: 'I图标协议实施.png', name: 'panel-efficiency.png', meaning: '预警效率(协议实施)' },
- 'icon.panel.analysis': { src: 'I图标规划设计.png', name: 'panel-analysis.png', meaning: '预警分析(规划设计)' },
- }
- // 主蓝 #01abea 的 RGB(tokens.css --cyan)
- const TINT = [0x01, 0xab, 0xea]
- function tintPng(buf) {
- // PNG 解析:只处理 8-bit RGBA/RGB(源图均为该格式)
- if (buf.readUInt32BE(0) !== 0x89504e47) throw new Error('非 PNG')
- let pos = 8
- const idat = []
- let width = 0
- let height = 0
- let bitDepth = 0
- let colorType = 0
- let palette = null
- let trns = null
- while (pos < buf.length) {
- const len = buf.readUInt32BE(pos)
- const type = buf.toString('ascii', pos + 4, pos + 8)
- const data = buf.subarray(pos + 8, pos + 8 + len)
- if (type === 'IHDR') {
- width = data.readUInt32BE(0)
- height = data.readUInt32BE(4)
- bitDepth = data[8]
- colorType = data[9]
- if (bitDepth !== 8 || ![2, 3, 6].includes(colorType)) throw new Error(`不支持的 PNG 格式: bit=${bitDepth} color=${colorType}`)
- } else if (type === 'PLTE') {
- palette = Buffer.from(data)
- } else if (type === 'tRNS') {
- trns = Buffer.from(data)
- } else if (type === 'IDAT') {
- idat.push(data)
- } else if (type === 'IEND') {
- break
- }
- pos += 12 + len
- }
- if (!width) throw new Error('缺 IHDR')
- // 解压
- const raw = inflateSync(Buffer.concat(idat))
- const channels = colorType === 6 ? 4 : colorType === 2 ? 3 : 1
- const stride = width * channels
- const bpp = channels
- // 还原滤波
- const img = Buffer.alloc(height * stride)
- for (let y = 0; y < height; y++) {
- const filter = raw[y * (stride + 1)]
- const row = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1))
- const prev = y > 0 ? img.subarray((y - 1) * stride, y * stride) : null
- const out = img.subarray(y * stride, (y + 1) * stride)
- for (let x = 0; x < stride; x++) {
- const a = x >= bpp ? out[x - bpp] : 0
- const b = prev ? prev[x] : 0
- const c = prev && x >= bpp ? prev[x - bpp] : 0
- let v = row[x]
- if (filter === 1) v += a
- else if (filter === 2) v += b
- else if (filter === 3) v += (a + b) >> 1
- else if (filter === 4) {
- const p = a + b - c
- const pa = Math.abs(p - a)
- const pb = Math.abs(p - b)
- const pc = Math.abs(p - c)
- v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c
- }
- out[x] = v & 0xff
- }
- }
- // 去色 + 染色,重建 PNG(无压缩 filter:filter=0 逐行)
- const outRows = []
- for (let y = 0; y < height; y++) {
- const rowOut = Buffer.alloc(1 + stride)
- rowOut[0] = 0
- for (let x = 0; x < width; x++) {
- let r
- let g
- let b
- let alpha = 255
- if (colorType === 6) {
- const o = x * 4
- r = img[y * stride + o]
- g = img[y * stride + o + 1]
- b = img[y * stride + o + 2]
- alpha = img[y * stride + o + 3]
- } else if (colorType === 2) {
- const o = x * 3
- r = img[y * stride + o]
- g = img[y * stride + o + 1]
- b = img[y * stride + o + 2]
- } else {
- const idx = img[y * stride + x]
- const p = palette ? idx * 3 : 0
- r = palette ? palette[p] : 0
- g = palette ? palette[p + 1] : 0
- b = palette ? palette[p + 2] : 0
- if (trns && idx < trns.length) alpha = trns[idx]
- }
- // 灰度(BT.601)作为亮度系数,乘目标色
- const lum = (r * 0.299 + g * 0.587 + b * 0.114) / 255
- const o = x * channels
- rowOut[1 + o] = Math.round(TINT[0] * lum)
- rowOut[1 + o + 1] = Math.round(TINT[1] * lum)
- rowOut[1 + o + 2] = Math.round(TINT[2] * lum)
- if (channels === 4) rowOut[1 + o + 3] = alpha
- }
- outRows.push(rowOut)
- }
- // 编码 PNG(无滤波,zlib 默认压缩)
- const crc32Table = (() => {
- const t = 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
- t[n] = c
- }
- return t
- })()
- const crc32 = (b) => {
- let c = -1
- for (const byte of b) c = crc32Table[(c ^ byte) & 0xff] ^ (c >>> 8)
- return (c ^ -1) >>> 0
- }
- const chunk = (type, data) => {
- const head = Buffer.alloc(4)
- head.writeUInt32BE(data.length)
- const body = Buffer.concat([Buffer.from(type, 'ascii'), data])
- const crc = Buffer.alloc(4)
- crc.writeUInt32BE(crc32(body))
- return Buffer.concat([head, body, crc])
- }
- const ihdr = Buffer.alloc(13)
- ihdr.writeUInt32BE(width, 0)
- ihdr.writeUInt32BE(height, 4)
- ihdr[8] = 8
- ihdr[9] = colorType === 6 ? 6 : 2
- const rawOut = Buffer.concat(outRows)
- const compressed = deflateSync(rawOut, { level: 9 })
- return Buffer.concat([
- Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
- chunk('IHDR', ihdr),
- chunk('IDAT', compressed),
- chunk('IEND', Buffer.alloc(0)),
- ])
- }
- const top = () => {
- for (const [key, cfg] of Object.entries(MAP)) {
- const srcPath = `${SRC}/${cfg.src}`
- if (!existsSync(srcPath)) {
- console.log(` ✗ 缺源图: ${cfg.src}`)
- continue
- }
- const out = tintPng(readFileSync(srcPath))
- writeFileSync(`${OUT}/${cfg.name}`, out)
- console.log(` ✓ ${key} <- ${cfg.src} -> panel/${cfg.name} (${out.length}B)`)
- }
- }
- top()
|