refactor: 通用化背景色逻辑
- 去掉 dark/light 关键词硬编码 - 优先级:styles.backgroundColor → 文字颜色反推 → canvas.backgroundColor - 文字颜色反推:亮度>128用深色背景,否则用浅色背景 - 两个 PPT 都正确识别背景色
This commit is contained in:
+17
-7
@@ -458,15 +458,25 @@ function convert(input) {
|
||||
if (hex) slideBg = { color: hex };
|
||||
}
|
||||
}
|
||||
// fallback:从 slide name 推断背景(CSS class 设置的背景 web-to-pixso 没提取)
|
||||
if (!slideBg) {
|
||||
const name = sg.name.toLowerCase();
|
||||
if (name.includes('dark')) {
|
||||
slideBg = { color: '1A1A1A' };
|
||||
} else if (name.includes('light')) {
|
||||
slideBg = { color: 'FAFAFA' };
|
||||
// fallback:从 slide 容器的文字颜色反推背景
|
||||
if (!slideBg && slideContainer && slideContainer.styles) {
|
||||
const textColor = slideContainer.styles.color;
|
||||
if (textColor) {
|
||||
const tc = rgbaToHex(textColor);
|
||||
if (tc) {
|
||||
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 + ' 个节点');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user