fix: 背景色优先用 canvas 实际颜色,不靠文字颜色推断

优先级:styles.backgroundColor → canvas.backgroundColor → 文字颜色推断
This commit is contained in:
李进
2026-07-27 18:18:27 +08:00
parent 64c86bcd63
commit 55aa35837c
2 changed files with 14 additions and 19 deletions
+7 -13
View File
@@ -521,7 +521,12 @@ async function convert(input) {
if (hex) slideBg = { color: hex };
}
}
// fallback从 slide 容器的文字颜色反推背景
// fallbackcanvas 背景色(实际页面背景色)
if (!slideBg && canvas.backgroundColor && canvas.backgroundColor !== 'rgba(0, 0, 0, 0)') {
const canvasHex = rgbaToHex(canvas.backgroundColor);
if (canvasHex) slideBg = { color: canvasHex };
}
// fallback:从文字颜色推断
if (!slideBg && slideContainer && slideContainer.styles) {
const textColor = slideContainer.styles.color;
if (textColor) {
@@ -531,21 +536,10 @@ async function convert(input) {
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;
// 浅色文字 → 深色背景(用 canvas 背景色),深色文字 → 浅色背景(取反)
if (lum > 128) {
const canvasBg = canvas.backgroundColor ? rgbaToHex(canvas.backgroundColor) : null;
slideBg = { color: canvasBg || '1A1A1A' };
} else {
slideBg = { color: 'FAFAFA' };
}
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 + ' 个节点');