From 3c3b3e6710fd64d93e323c0054365fefa92422bd Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 28 Jul 2021 19:10:26 +0100 Subject: [PATCH] Fixed infinite loop when lightening/darkening colors refs https://github.com/TryGhost/Team/issues/928 refs https://github.com/TryGhost/Admin/commit/eed299d1f6edd97019c3647dd7623c289896262b - 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 --- ghost/admin/app/utils/color.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ghost/admin/app/utils/color.js b/ghost/admin/app/utils/color.js index 84a8e7f72d..bf85f9db7d 100644 --- a/ghost/admin/app/utils/color.js +++ b/ghost/admin/app/utils/color.js @@ -4,14 +4,16 @@ export function lightenToContrastThreshold(foreground, background, contrastThres const foregroundColor = Color(foreground); const backgroundColor = Color(background); + const {h,s} = foregroundColor.hsl().object(); + let newColor = foregroundColor; while (newColor.contrast(backgroundColor) < contrastThreshold) { - if (newColor.l() >= 100) { + if (newColor.lightness() >= 100) { break; } - newColor = newColor.lightness(newColor.l() + 5); + newColor = Color({h, s, l: newColor.lightness() + 5}); } return newColor; @@ -21,14 +23,16 @@ export function darkenToContrastThreshold(foreground, background, contrastThresh const foregroundColor = Color(foreground); const backgroundColor = Color(background); + const {h,s} = foregroundColor.hsl().object(); + let newColor = foregroundColor; while (newColor.contrast(backgroundColor) < contrastThreshold) { - if (newColor.l() <= 0) { + if (newColor.lightness() <= 0) { break; } - newColor = newColor.lightness(newColor.l() - 5); + newColor = Color({h, s, l: newColor.lightness() - 5}); } return newColor;