Fixed infinite loop when lightening/darkening colors

refs https://github.com/TryGhost/Team/issues/928
refs eed299d1f6

- usage of `color` was incorrect resulting in an infinite loop because the color was not being changed on each iteration
  - `Color().lightness()` adjusts via percentage not exact number
  - `Color().l()` does not return lightness
This commit is contained in:
Kevin Ansfield 2021-07-28 19:10:26 +01:00
parent 2cf55cb307
commit 3c3b3e6710

View File

@ -4,14 +4,16 @@ export function lightenToContrastThreshold(foreground, background, contrastThres
const foregroundColor = Color(foreground); const foregroundColor = Color(foreground);
const backgroundColor = Color(background); const backgroundColor = Color(background);
const {h,s} = foregroundColor.hsl().object();
let newColor = foregroundColor; let newColor = foregroundColor;
while (newColor.contrast(backgroundColor) < contrastThreshold) { while (newColor.contrast(backgroundColor) < contrastThreshold) {
if (newColor.l() >= 100) { if (newColor.lightness() >= 100) {
break; break;
} }
newColor = newColor.lightness(newColor.l() + 5); newColor = Color({h, s, l: newColor.lightness() + 5});
} }
return newColor; return newColor;
@ -21,14 +23,16 @@ export function darkenToContrastThreshold(foreground, background, contrastThresh
const foregroundColor = Color(foreground); const foregroundColor = Color(foreground);
const backgroundColor = Color(background); const backgroundColor = Color(background);
const {h,s} = foregroundColor.hsl().object();
let newColor = foregroundColor; let newColor = foregroundColor;
while (newColor.contrast(backgroundColor) < contrastThreshold) { while (newColor.contrast(backgroundColor) < contrastThreshold) {
if (newColor.l() <= 0) { if (newColor.lightness() <= 0) {
break; break;
} }
newColor = newColor.lightness(newColor.l() - 5); newColor = Color({h, s, l: newColor.lightness() - 5});
} }
return newColor; return newColor;