diff --git a/lib/commons/color/get-foreground-color.js b/lib/commons/color/get-foreground-color.js index dca800421..d4ed670b2 100644 --- a/lib/commons/color/get-foreground-color.js +++ b/lib/commons/color/get-foreground-color.js @@ -39,26 +39,42 @@ export default function getForegroundColor(node, _, bgColor, options = {}) { } fgColors = fgColors.concat(color); - - if (color.alpha === 1) { + // If any color in the array is fully opaque, break + if (Array.isArray(color)) { + if (color.some(c => c && c.alpha === 1)) { + break; + } + } else if (color.alpha === 1) { break; } } + if (!fgColors.length) { + // Could not determine foreground color + incompleteData.set('fgColor', 'No foreground color found'); + return null; + } + const fgColor = fgColors.reduce((source, backdrop) => { return flattenColors(source, backdrop); }); // Lastly blend the background bgColor ??= getBackgroundColor(node, []); + if (bgColor === null) { - const reason = incompleteData.get('bgColor'); - incompleteData.set('fgColor', reason); - return null; + // Return the foreground color as-is if background color is not found + return fgColor; } const stackingContexts = getStackingContext(node); const context = findNodeInContexts(stackingContexts, node); + + // If context is not found, fallback to blending with bgColor only + if (!context) { + return flattenColors(fgColor, bgColor); + } + return flattenColors( calculateBlendedForegroundColor(fgColor, context, stackingContexts), // default page background