@@ -188,6 +188,28 @@ window.__contrastAuditPrepare = function () {
188188 return [ p [ 0 ] , p [ 1 ] , p [ 2 ] , p [ 3 ] != null ? p [ 3 ] : 1 ] ;
189189 }
190190
191+ // The colours a url(#id) fill paints (gradient stops, through href); [] when it names no gradient.
192+ function gradientStops ( fill ) {
193+ var ref = / ^ u r l \( " ? [ ^ " # ) ] * # ( [ ^ " ) ] + ) " ? \) / . exec ( fill || "" ) ;
194+ if ( ! ref ) return null ;
195+ var grad = document . getElementById ( ref [ 1 ] ) ;
196+ for ( var hops = 0 ; grad && hops < 8 ; hops ++ ) {
197+ if ( ! / ^ ( l i n e a r | r a d i a l ) G r a d i e n t $ / . test ( grad . localName ) ) return [ ] ;
198+ var stops = grad . querySelectorAll ( ":scope > stop" ) ;
199+ if ( stops . length ) return Array . prototype . map . call ( stops , stopColor ) . filter ( Boolean ) ;
200+ var href = grad . getAttribute ( "href" ) || grad . getAttribute ( "xlink:href" ) || "" ;
201+ grad = href . charAt ( 0 ) === "#" ? document . getElementById ( href . slice ( 1 ) ) : null ;
202+ }
203+ return [ ] ;
204+ }
205+
206+ function stopColor ( stop ) {
207+ var stopStyle = getComputedStyle ( stop ) ;
208+ var color = parseColor ( stopStyle . stopColor ) ;
209+ if ( color ) color [ 3 ] *= parseFloat ( stopStyle . stopOpacity ) ;
210+ return color && color [ 3 ] > 0.01 ? color : null ;
211+ }
212+
191213 function selectorOf ( el ) {
192214 if ( el . id ) return "#" + el . id ;
193215 var cls = Array . from ( el . classList ) . slice ( 0 , 2 ) . join ( "." ) ;
@@ -369,14 +391,16 @@ window.__contrastAuditPrepare = function () {
369391 // is actually covered; the same copy is audited normally when visible.
370392 if ( isIntentionallyOccluded ( el , rect ) ) continue ;
371393
372- // For SVG text, `fill` is the paint that's actually rendered; `color` is
373- // frequently just the inherited/initial value and unrelated to what's on
374- // screen. Only trust `fill` when it resolves to a solid color — "none",
375- // "context-fill", and gradient/pattern refs (url(#...)) fall back to
376- // `color` rather than crashing parseColor or reporting a fabricated
377- // black.
394+ // SVG text paints `fill`, not `color`. A gradient fill is judged by its weakest stop; a pattern
395+ // or a missing reference has no colour to judge, so the text is skipped.
378396 var isSvgText = isSvgTextElement ( el ) ;
379- var fg = isSvgText ? parseColor ( cs . fill ) || parseColor ( cs . color ) : parseColor ( cs . color ) ;
397+ var fgStops = isSvgText ? gradientStops ( cs . fill ) : null ;
398+ if ( fgStops && ! fgStops . length ) continue ;
399+ var fg = fgStops
400+ ? fgStops [ 0 ]
401+ : isSvgText
402+ ? parseColor ( cs . fill ) || parseColor ( cs . color )
403+ : parseColor ( cs . color ) ;
380404 if ( ! fg ) continue ;
381405 if ( fg [ 3 ] <= 0.01 ) continue ;
382406 var strokeWidth = parseFloat ( cs . webkitTextStrokeWidth || "0" ) ;
@@ -448,6 +472,7 @@ window.__contrastAuditPrepare = function () {
448472 selector : selectorOf ( el ) ,
449473 text : ( el . textContent || "" ) . trim ( ) . slice ( 0 , 50 ) ,
450474 fg : fg ,
475+ fgStops : fgStops ,
451476 stroke : stroke ,
452477 fontSize : fontSize ,
453478 fontWeight : fontWeight ,
@@ -603,13 +628,24 @@ window.__contrastAuditFinish = async function (imgBase64, time, candidates, regi
603628 bgB = median ( bb ) ;
604629 }
605630
606- // Composite foreground alpha over the measured background
607- var fg = c . fg ;
608- var compR = Math . round ( fg [ 0 ] * fg [ 3 ] + bgR * ( 1 - fg [ 3 ] ) ) ;
609- var compG = Math . round ( fg [ 1 ] * fg [ 3 ] + bgG * ( 1 - fg [ 3 ] ) ) ;
610- var compB = Math . round ( fg [ 2 ] * fg [ 3 ] + bgB * ( 1 - fg [ 3 ] ) ) ;
611-
612- var ratio = + wcagRatio ( compR , compG , compB , bgR , bgG , bgB ) . toFixed ( 2 ) ;
631+ // Composite foreground alpha over the measured background; a gradient's weakest stop sets the ratio.
632+ var compR = 0 ,
633+ compG = 0 ,
634+ compB = 0 ,
635+ ratio = Infinity ;
636+ var fills = c . fgStops || [ c . fg ] ;
637+ for ( var pi = 0 ; pi < fills . length ; pi ++ ) {
638+ var fg = fills [ pi ] ;
639+ var paintR = Math . round ( fg [ 0 ] * fg [ 3 ] + bgR * ( 1 - fg [ 3 ] ) ) ;
640+ var paintG = Math . round ( fg [ 1 ] * fg [ 3 ] + bgG * ( 1 - fg [ 3 ] ) ) ;
641+ var paintB = Math . round ( fg [ 2 ] * fg [ 3 ] + bgB * ( 1 - fg [ 3 ] ) ) ;
642+ var paintRatio = + wcagRatio ( paintR , paintG , paintB , bgR , bgG , bgB ) . toFixed ( 2 ) ;
643+ if ( paintRatio >= ratio ) continue ;
644+ compR = paintR ;
645+ compG = paintG ;
646+ compB = paintB ;
647+ ratio = paintRatio ;
648+ }
613649
614650 // A solid text stroke is part of the visible glyph paint. Use whichever
615651 // of fill or stroke provides the stronger edge contrast, rather than
0 commit comments