@react-md/utils
Version:
General utils for react-md.
23 lines • 965 B
JavaScript
var SHORTHAND_REGEX = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
var VERBOSE_REGEX = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
/**
* Converts a hex string into an rgb value. This is useful for detecting color
* contrast ratios and other stuff.
*
* @param hex - The hex string to convert
* @returns an object containing the r, g, b values for the color.
*/
export function hexToRGB(hex) {
if (process.env.NODE_ENV !== "production" &&
!SHORTHAND_REGEX.test(hex) &&
!VERBOSE_REGEX.test(hex)) {
throw new TypeError("Invalid color string.");
}
hex = hex.replace(SHORTHAND_REGEX, function (_m, r, g, b) { return "".concat(r).concat(r).concat(g).concat(g).concat(b).concat(b); });
var result = hex.match(VERBOSE_REGEX) || [];
var r = parseInt(result[1] || "", 16) || 0;
var g = parseInt(result[2] || "", 16) || 0;
var b = parseInt(result[3] || "", 16) || 0;
return [r, g, b];
}
//# sourceMappingURL=hexToRGB.js.map