Skip to content

Commit 2eaa8e6

Browse files
committed
fix: color contrast fix
1 parent c99ae8f commit 2eaa8e6

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

Loading

packages/charts/src/common/color_calcs.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)