UNPKG

counter-color

Version:

Helps to determine what color of text goes best for a given background, light or dark (and vice versa).

24 lines 933 B
import { toRGB } from "./rgb"; import { colorLuminance } from "./luminance"; export function colorsContrast(color1, color2) { const lum1 = colorLuminance(toRGB(color1)); const lum2 = colorLuminance(toRGB(color2)); return Math.abs(lum1 - lum2); } export function colorsContrastRatio(color1, color2) { const lum1 = colorLuminance(toRGB(color1)); const lum2 = colorLuminance(toRGB(color2)); const lighter = Math.min(lum1, lum2); const darker = Math.max(lum1, lum2); return (lighter + 0.05) / (darker + 0.05); } export function colorsHaveSufficientContrast(color1, color2) { return colorsContrastRatio(color1, color2) <= 1 / 7; } export function pickMostContrast(colors, target) { const contrasts = colors.map(color => colorsContrast(color, target)); const max = Math.max(...contrasts); const index = contrasts.indexOf(max); return colors[index]; } //# sourceMappingURL=contrast.js.map