UNPKG

contrastrast

Version:

A lightweight tool that parses color strings and recommends text contrast based on WCAG Standards

61 lines (60 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HSL_CONVERSION = exports.RGB_BOUNDS = exports.WCAG_LEVELS = exports.CONTRAST_RATIO = exports.GAMMA_CORRECTION = exports.LUMINANCE_COEFFICIENTS = exports.BRIGHTNESS_COEFFICIENTS = exports.CONTRAST_THRESHOLD = void 0; // Source: https://www.w3.org/TR/AERT/#color-contrast exports.CONTRAST_THRESHOLD = 124; // WC3 AERT brightness calculation coefficients -- more efficient for quick calculations // Source: https://www.w3.org/TR/AERT/#color-contrast exports.BRIGHTNESS_COEFFICIENTS = { RED: 299, GREEN: 587, BLUE: 114, DIVISOR: 1000, }; // WCAG 2.1 relative luminance calculation coefficients // Source: https://www.w3.org/TR/WCAG21/#dfn-relative-luminance exports.LUMINANCE_COEFFICIENTS = { RED: 0.2126, GREEN: 0.7152, BLUE: 0.0722, }; // Gamma correction constants for sRGB color space // Source: https://www.w3.org/TR/WCAG21/#dfn-relative-luminance exports.GAMMA_CORRECTION = { THRESHOLD: 0.04045, LINEAR_DIVISOR: 12.92, GAMMA_OFFSET: 0.055, GAMMA_DIVISOR: 1.055, GAMMA_EXPONENT: 2.4, }; // WCAG 2.1 contrast ratio calculation constants // Source: https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio exports.CONTRAST_RATIO = { LUMINANCE_OFFSET: 0.05, }; // WCAG 2.1 minimum contrast thresholds // Source: https://www.w3.org/TR/WCAG21/#contrast-minimum // Source: https://www.w3.org/TR/WCAG21/#contrast-enhanced exports.WCAG_LEVELS = { AA: { normal: 4.5, large: 3.0, }, AAA: { normal: 7.0, large: 4.5, }, }; // RGB color space bounds exports.RGB_BOUNDS = { MIN: 0, MAX: 255, }; // HSL conversion constants // Source: https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB exports.HSL_CONVERSION = { LIGHTNESS_THRESHOLD: 0.5, HUE_SECTORS: 6, FULL_CIRCLE_DEGREES: 360, PERCENTAGE_MULTIPLIER: 100, };