This commit is contained in:
Josh at WLTechBlog
2025-12-09 14:06:19 -07:00
parent 6b26c13add
commit 8d7566cad6
2 changed files with 153 additions and 9 deletions

View File

@@ -9150,15 +9150,24 @@ func (d *Daemon) checkContrast(tabID string, selector string, timeout int) (*Con
}
// Tier 3: Gradient analysis - walks up DOM tree to find gradient backgrounds
// Only returns gradient info if no solid background color is found first
function analyzeGradientContrast(element, textColor) {
try {
// Walk up the DOM tree to find a gradient background
// Stop if we encounter a solid background color first
let current = element;
while (current && current !== document.documentElement.parentElement) {
const style = window.getComputedStyle(current);
const bgImage = style.backgroundImage;
// Check for solid background color first - if found, gradient doesn't apply
const bgColor = style.backgroundColor;
if (!isTransparent(bgColor)) {
// Solid background found - gradient doesn't apply to this element
return null;
}
// Check if background is a gradient
const bgImage = style.backgroundImage;
if (bgImage && bgImage.includes('gradient')) {
// Extract gradient colors using regex
const colorRegex = /rgba?\([^)]+\)|#[0-9a-f]{3,6}/gi;
@@ -9312,14 +9321,16 @@ func (d *Daemon) checkContrast(tabID string, selector string, timeout int) (*Con
}
}
// Tier 3: Check for gradient backgrounds (always check, even if solid color found)
// Gradients take precedence over solid colors when present
gradientInfo = analyzeGradientContrast(element, fgColor);
if (gradientInfo) {
detectionMethod = 'gradient-analysis';
// Use a representative color from the gradient for the background_color field
// We'll use the worst-case color for reporting purposes
bgColor = gradientInfo.colors[0] || bgColor;
// Tier 3: Check for gradient backgrounds (only if no solid color found)
// Gradient analysis will return null if a solid background is found before the gradient
if (!bgColor) {
gradientInfo = analyzeGradientContrast(element, fgColor);
if (gradientInfo) {
detectionMethod = 'gradient-analysis';
// Use a representative color from the gradient for the background_color field
// We'll use the first color for reporting purposes
bgColor = gradientInfo.colors[0];
}
}
// Final fallback to white if no background detected