UNPKG

@compositive/foundation

Version:

Compositive framework foundation package.

44 lines (41 loc) 1.33 kB
import chroma from 'chroma-js'; import { ContrastLevel } from './types.js'; const getWCAGContrast = (background, foreground) => { const value = chroma.contrast(background.hex, foreground.hex); return { get value() { return value; }, toString() { const roundedValue = Math.round(value * 100) / 100; const level = this.level; let levelDescription = ""; switch (level) { case ContrastLevel.HIGH: { levelDescription = " AAA"; break; } case ContrastLevel.GOOD: { levelDescription = " AA"; break; } case ContrastLevel.MINIMUM: { levelDescription = " A"; break; } } return `${roundedValue}${levelDescription}`; }, get level() { if (value >= 7) return ContrastLevel.HIGH; if (value >= 4.5) return ContrastLevel.GOOD; if (value >= 3) return ContrastLevel.MINIMUM; return ContrastLevel.INSUFFICIENT; }, }; }; export { getWCAGContrast }; //# sourceMappingURL=getWCAGContrast.js.map