| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- // 状态标识图标(地图图例行):
- // 从 Texture/Icon 取 I 系列,按各状态的语义色染色(--lv1/lv2/lv3/lv4/neutral),
- // 输出 public/cockpit/assets/icons/status/*.png。
- // 语义色来自 tokens.css:报警 #ff3b52 / 预警 #ffb020 / 工单 #01abea / 正常 #27d17a / 中性 #5680b0。
- 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/status'
- mkdirSync(OUT, { recursive: true })
- const STATUS = [
- { key: 'alarm', src: 'I图标报警产生.png', hex: '#ff3b52', meaning: '报警(铃铛警示)' },
- { key: 'warning', src: 'I图标报警等级.png', hex: '#ffb020', meaning: '预警(警灯+等级)' },
- { key: 'workOrder', src: 'I图标报警解除.png', hex: '#01abea', meaning: '工单处置(解除对勾)' },
- { key: 'normal', src: 'I图标在线.png', hex: '#27d17a', meaning: '正常(在线信号)' },
- { key: 'offline', src: 'I图标离线.png', hex: '#5680b0', meaning: '离线(断线信号)' },
- { key: 'empty', src: 'I图标设备.png', hex: '#5680b0', meaning: '无设备(设备机柜,中性色)' },
- { key: 'unknown', src: 'I图标报警误报.png', hex: '#5680b0', meaning: '状态未发布(问号铃,中性色)' },
- { key: 'unassociated', src: 'I图标设备开发.png', hex: '#5680b0', meaning: '无关联设备(中性色)' },
- ]
- const hexToRgb = (h) => [parseInt(h.slice(1, 3), 16), parseInt(h.slice(3, 5), 16), parseInt(h.slice(5, 7), 16)]
- function tintPng(buf, tint) {
- 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
- }
- 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
- }
- }
- 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, g, b, 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]
- }
- 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)
- }
- 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 compressed = deflateSync(Buffer.concat(outRows), { 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)),
- ])
- }
- for (const cfg of STATUS) {
- const srcPath = `${SRC}/${cfg.src}`
- if (!existsSync(srcPath)) {
- console.log(` ✗ 缺源图: ${cfg.src}`)
- continue
- }
- const out = tintPng(readFileSync(srcPath), hexToRgb(cfg.hex))
- writeFileSync(`${OUT}/status-${cfg.key}.png`, out)
- console.log(` ✓ status-${cfg.key}.png <- ${cfg.src} @ ${cfg.hex} (${out.length}B)`)
- }
|