@@ -100,6 +100,7 @@ export function makeHighContrastColor(foreground: Color, background: Color, rati
100100 // determine the lightness factor of the background color to determine whether to lighten or darken the foreground
101101 const lightness = chroma ( background ) . get ( 'hsl.l' ) ;
102102 let highContrastTextColor = foreground ;
103+ const originalhighContrastTextColor = foreground ;
103104 const isBackgroundDark = colorIsDark ( background ) ;
104105 // determine whether white or black text is ideal contrast vs a grey that just passes the ratio
105106 if ( isBackgroundDark && chroma . deltaE ( 'black' , foreground ) === 0 ) {
@@ -117,9 +118,14 @@ export function makeHighContrastColor(foreground: Color, background: Color, rati
117118 const scaledOldContrast = Math . round ( contrast * precision ) / precision ;
118119 contrast = getContrast ( highContrastTextColor , background ) ;
119120 const scaledContrast = Math . round ( contrast * precision ) / precision ;
120- // catch if the ideal contrast may not be possible
121+ // catch if the ideal contrast may not be possible, switch to the other extreme color contrast
121122 if ( scaledOldContrast === scaledContrast ) {
122- break ;
123+ const contrastColor =
124+ originalhighContrastTextColor === 'rgba(255, 255, 255, 1)' || originalhighContrastTextColor === '#fff'
125+ ? '#000'
126+ : '#fff' ;
127+ // make sure the new text color hits the ratio, if not, then return the scaledContrast since we tried earlier
128+ return getContrast ( contrastColor , background ) > ratio ? contrastColor : scaledContrast . toString ( ) ;
123129 }
124130 }
125131 return highContrastTextColor . toString ( ) ;
0 commit comments