contrastrast
Version:
A lightweight tool that parses color strings and recommends text contrast based on WCAG Standards
25 lines (24 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRGBFromColorString = void 0;
const rgbConverters_js_1 = require("./rgbConverters.js");
const RGB_REGEX = /rgb\((0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d)\)/i;
const HEXCOLOR_REGEX = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i;
const HSL_REGEX = /hsl\(\s*((?:360|3[0-5][0-9]|2[0-9][0-9]|1[0-9][0-9]|(?:100|0{0,1}[0-9][0-9]|0{0,1}0{0,1}[0-9])))(?:°|deg){0,1}\s*,{0,1}\s*((?:100|0{0,1}[0-9][0-9]|0{0,1}0{0,1}[0-9])(?:\.\d+)?)%{0,1}\s*,{0,1}\s*((?:100|0{0,1}[0-9][0-9]|0{0,1}0{0,1}[0-9])(?:\.\d+)?)%{0,1}\)/i;
const getRGBFromColorString = (colorString) => {
const [fullRgbMatch, red, green, blue] = colorString.match(RGB_REGEX) || [];
if (fullRgbMatch) {
return (0, rgbConverters_js_1.extractRGBValuesFromRGBStrings)(red, green, blue);
}
const [, hexMatch] = colorString.match(HEXCOLOR_REGEX) || [];
if (hexMatch) {
return (0, rgbConverters_js_1.extractRGBValuesFromHex)(hexMatch);
}
const [fullHslMatch, hue, saturation, light] = colorString.match(HSL_REGEX) ||
[];
if (fullHslMatch) {
return (0, rgbConverters_js_1.extractRGBValuesFromHSL)(hue, saturation, light);
}
throw new Error(`Unsupported color string "${colorString}"`);
};
exports.getRGBFromColorString = getRGBFromColorString;