fix: 背景色优先用 canvas 实际颜色,不靠文字颜色推断
优先级:styles.backgroundColor → canvas.backgroundColor → 文字颜色推断
This commit is contained in:
+7
-13
@@ -521,7 +521,12 @@ async function convert(input) {
|
||||
if (hex) slideBg = { color: hex };
|
||||
}
|
||||
}
|
||||
// fallback:从 slide 容器的文字颜色反推背景
|
||||
// fallback:canvas 背景色(实际页面背景色)
|
||||
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 + ' 个节点');
|
||||
|
||||
|
||||
@@ -220,6 +220,12 @@ async function convertToPptx(input, onProgress) {
|
||||
if (hex) slideBg = { color: hex };
|
||||
}
|
||||
}
|
||||
// fallback:canvas 背景色(实际页面背景色)
|
||||
if (!slideBg && canvas.backgroundColor && canvas.backgroundColor !== 'rgba(0, 0, 0, 0)') {
|
||||
const canvasHex = rgbaToHex(canvas.backgroundColor);
|
||||
if (canvasHex) slideBg = { color: canvasHex };
|
||||
}
|
||||
// fallback:从文字颜色推断
|
||||
if (!slideBg && slideNode?.styles) {
|
||||
const textColor = slideNode.styles.color;
|
||||
if (textColor) {
|
||||
@@ -229,12 +235,7 @@ async function convertToPptx(input, onProgress) {
|
||||
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;
|
||||
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' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user