仰望星辰工作室

starmusic-mobile

starmusic-mobile/ scripts/gen-theme-tabbar.js 4.9 KB · 155 行 原始文件
1// 一次性脚本:把 static/tabbar 的选中态图标重着色,生成各主题版本
2// 运行:node scripts/gen-theme-tabbar.js
3// 输出:static/tabbar/{search,my,setting}-active-{themeId}.png
4// 默认主题(green)继续使用原始图标,不生成文件。
5const fs = require('fs')
6const path = require('path')
7const zlib = require('zlib')
8
9// ---------- 主题色:从 common/theme.js 提取,保证与前端一致 ----------
10const themeSrc = fs.readFileSync(path.join(__dirname, '..', 'common', 'theme.js'), 'utf8')
11const THEMES = [
12 ...themeSrc.matchAll(/id:\s*'([\w-]+)',\s*name:\s*'[^']*',\s*color:\s*'(#[0-9a-fA-F]{6})'/g),
13].map((m) => ({ id: m[1], color: m[2] }))
14if (!THEMES.length) throw new Error('未从 common/theme.js 解析到主题色')
15
16const DEFAULT_THEME_ID = 'green'
17const ICON_NAMES = ['search-active', 'my-active', 'setting-active']
18
19// ---------- PNG 编解码(仅支持 8bit RGBA,即本项目 tabbar 图标的格式) ----------
20const crcTable = (() => {
21 const table = new Int32Array(256)
22 for (let n = 0; n < 256; n++) {
23 let c = n
24 for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
25 table[n] = c
26 }
27 return table
28})()
29
30const crc32 = (buf) => {
31 let c = 0xffffffff
32 for (let i = 0; i < buf.length; i++) c = crcTable[(c ^ buf[i]) & 0xff] ^ (c >>> 8)
33 return (c ^ 0xffffffff) >>> 0
34}
35
36const chunk = (type, data) => {
37 const len = Buffer.alloc(4)
38 len.writeUInt32BE(data.length)
39 const typeBuf = Buffer.from(type, 'ascii')
40 const crcBuf = Buffer.alloc(4)
41 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])))
42 return Buffer.concat([len, typeBuf, data, crcBuf])
43}
44
45const decodePng = (buf) => {
46 let pos = 8
47 let width = 0
48 let height = 0
49 let colorType = 6
50 const idat = []
51 while (pos < buf.length) {
52 const len = buf.readUInt32BE(pos)
53 const type = buf.toString('ascii', pos + 4, pos + 8)
54 const data = buf.subarray(pos + 8, pos + 8 + len)
55 if (type === 'IHDR') {
56 width = data.readUInt32BE(0)
57 height = data.readUInt32BE(4)
58 colorType = data[9]
59 } else if (type === 'IDAT') {
60 idat.push(data)
61 } else if (type === 'IEND') {
62 break
63 }
64 pos += 12 + len
65 }
66 if (colorType !== 6) throw new Error('仅支持 8bit RGBA 的 PNG(colorType=6),当前为 ' + colorType)
67
68 const raw = zlib.inflateSync(Buffer.concat(idat))
69 const bpp = 4
70 const stride = width * bpp
71 const out = Buffer.alloc(stride * height)
72 let rp = 0
73 for (let y = 0; y < height; y++) {
74 const filter = raw[rp++]
75 const line = raw.subarray(rp, rp + stride)
76 rp += stride
77 const cur = out.subarray(y * stride, (y + 1) * stride)
78 const prev = y > 0 ? out.subarray((y - 1) * stride, y * stride) : null
79 for (let x = 0; x < stride; x++) {
80 const a = x >= bpp ? cur[x - bpp] : 0
81 const b = prev ? prev[x] : 0
82 const c = prev && x >= bpp ? prev[x - bpp] : 0
83 let v = line[x]
84 switch (filter) {
85 case 1:
86 v += a
87 break
88 case 2:
89 v += b
90 break
91 case 3:
92 v += (a + b) >> 1
93 break
94 case 4: {
95 const p = a + b - c
96 const pa = Math.abs(p - a)
97 const pb = Math.abs(p - b)
98 const pc = Math.abs(p - c)
99 v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c
100 break
101 }
102 }
103 cur[x] = v & 0xff
104 }
105 }
106 return { width, height, pixels: out }
107}
108
109const encodePng = (width, height, pixels) => {
110 const stride = width * 4
111 const raw = Buffer.alloc((stride + 1) * height)
112 for (let y = 0; y < height; y++) {
113 raw[y * (stride + 1)] = 0 // filter: None
114 pixels.copy(raw, y * (stride + 1) + 1, y * stride, (y + 1) * stride)
115 }
116 const ihdr = Buffer.alloc(13)
117 ihdr.writeUInt32BE(width, 0)
118 ihdr.writeUInt32BE(height, 4)
119 ihdr[8] = 8 // bit depth
120 ihdr[9] = 6 // color type: RGBA
121 return Buffer.concat([
122 Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
123 chunk('IHDR', ihdr),
124 chunk('IDAT', zlib.deflateSync(raw, { level: 9 })),
125 chunk('IEND', Buffer.alloc(0)),
126 ])
127}
128
129const hexToRgb = (hex) => [
130 parseInt(hex.slice(1, 3), 16),
131 parseInt(hex.slice(3, 5), 16),
132 parseInt(hex.slice(5, 7), 16),
133]
134
135// ---------- 生成 ----------
136const dir = path.join(__dirname, '..', 'static', 'tabbar')
137let count = 0
138for (const theme of THEMES) {
139 if (theme.id === DEFAULT_THEME_ID) continue
140 const [r, g, b] = hexToRgb(theme.color)
141 for (const name of ICON_NAMES) {
142 const src = path.join(dir, `${name}.png`)
143 if (!fs.existsSync(src)) continue
144 const { width, height, pixels } = decodePng(fs.readFileSync(src))
145 for (let i = 0; i < pixels.length; i += 4) {
146 if (pixels[i + 3] === 0) continue // 全透明像素保持原样,仅给可见像素换色
147 pixels[i] = r
148 pixels[i + 1] = g
149 pixels[i + 2] = b
150 }
151 fs.writeFileSync(path.join(dir, `${name}-${theme.id}.png`), encodePng(width, height, pixels))
152 count++
153 }
154}
155console.log(`generated ${count} theme tabbar icons`)