From 0e60f7525b054b863d41253345488de140f83e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=9B?= Date: Fri, 24 Jul 2026 16:40:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=80=9A=E7=94=A8=E5=8C=96?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E8=89=B2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 去掉 dark/light 关键词硬编码 - 优先级:styles.backgroundColor → 文字颜色反推 → canvas.backgroundColor - 文字颜色反推:亮度>128用深色背景,否则用浅色背景 - 两个 PPT 都正确识别背景色 --- convert-w2p.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/convert-w2p.js b/convert-w2p.js index 411e204..bf3261f 100644 --- a/convert-w2p.js +++ b/convert-w2p.js @@ -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 + ' 个节点');