import Color from 'color'; export {Color}; export function lightenToContrastThreshold(foreground: string | Color, background: string | Color, contrastThreshold: number): Color { const foregroundColor = Color(foreground); const backgroundColor = Color(background); const {h,s} = foregroundColor.hsl().object(); let newColor = foregroundColor; while (newColor.contrast(backgroundColor) < contrastThreshold) { if (newColor.lightness() >= 100) { break; } newColor = Color({h, s, l: newColor.lightness() + 5}); } return newColor; } export function darkenToContrastThreshold(foreground: string | Color, background: string | Color, contrastThreshold: number): Color { const foregroundColor = Color(foreground); const backgroundColor = Color(background); const {h,s} = foregroundColor.hsl().object(); let newColor = foregroundColor; while (newColor.contrast(backgroundColor) < contrastThreshold) { if (newColor.lightness() <= 0) { break; } newColor = Color({h, s, l: newColor.lightness() - 5}); } return newColor; } export function textColorForBackgroundColor(background: string | Color): Color { const backgroundColor = Color(background); const white = Color({r: 255, g: 255, b: 255}); const black = Color({r: 0, g: 0, b: 0}); // shared with Portal https://github.com/TryGhost/Portal/blob/317876f20d22431df15e655ea6cc197fe636615e/src/utils/contrast-color.js#L26-L29 const yiq = ( backgroundColor.red() * 0.299 + backgroundColor.green() * 0.587 + backgroundColor.blue() * 0.114 ); return (yiq >= 186) ? black : white; }