@atlaskit/tokens
Version:
Design tokens are the single source of truth to name and store design decisions.
16 lines • 834 B
JavaScript
import { hexToRgb } from './hex-to-rgb';
import { isValidHex } from './is-valid-hex';
import { relativeLuminanceW3C } from './relative-luminance-w3-c';
export function getContrastRatio(foreground, background) {
if (!isValidHex(foreground) || !isValidHex(background)) {
throw new Error('Invalid HEX');
}
var foregroundRgb = hexToRgb(foreground);
var backgroundRgb = hexToRgb(background);
var foregroundLuminance = relativeLuminanceW3C(foregroundRgb[0], foregroundRgb[1], foregroundRgb[2]);
var backgroundLuminance = relativeLuminanceW3C(backgroundRgb[0], backgroundRgb[1], backgroundRgb[2]);
// calculate the color contrast ratio
var brightest = Math.max(foregroundLuminance, backgroundLuminance);
var darkest = Math.min(foregroundLuminance, backgroundLuminance);
return (brightest + 0.05) / (darkest + 0.05);
}