refactor: 通用化背景色逻辑

- 去掉 dark/light 关键词硬编码
- 优先级:styles.backgroundColor → 文字颜色反推 → canvas.backgroundColor
- 文字颜色反推:亮度>128用深色背景,否则用浅色背景
- 两个 PPT 都正确识别背景色
This commit is contained in:
李进
2026-07-24 16:40:50 +08:00
parent a2c7f23107
commit 0e60f7525b
+17 -7
View File
@@ -458,15 +458,25 @@ function convert(input) {
if (hex) slideBg = { color: hex }; if (hex) slideBg = { color: hex };
} }
} }
// fallback:从 slide name 推断背景(CSS class 设置的背景 web-to-pixso 没提取) // fallback:从 slide 容器的文字颜色反推背景
if (!slideBg) { if (!slideBg && slideContainer && slideContainer.styles) {
const name = sg.name.toLowerCase(); const textColor = slideContainer.styles.color;
if (name.includes('dark')) { if (textColor) {
slideBg = { color: '1A1A1A' }; const tc = rgbaToHex(textColor);
} else if (name.includes('light')) { if (tc) {
slideBg = { color: 'FAFAFA' }; const r = parseInt(tc.slice(0,2), 16);
const g = parseInt(tc.slice(2,4), 16);
const b = parseInt(tc.slice(4,6), 16);
const lum = (r * 299 + g * 587 + b * 114) / 1000;
slideBg = lum > 128 ? { color: '1A1A1A' } : { color: 'FAFAFA' };
}
} }
} }
// fallback:从 canvas 背景色兜底(最后手段)
if (!slideBg && canvas.backgroundColor && canvas.backgroundColor !== 'rgba(0, 0, 0, 0)') {
const hex = rgbaToHex(canvas.backgroundColor);
if (hex) slideBg = { color: hex };
}
console.log('处理 ' + sg.name + ': ' + sg.children.length + ' 个节点'); console.log('处理 ' + sg.name + ': ' + sg.children.length + ' 个节点');