@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
39 lines (38 loc) • 1.08 kB
JavaScript
const RE_HEX = /^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
const RE_HEXA = /^#?([A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$/;
const hex2rgb = (hex) => {
if (hex.match(RE_HEX)) {
if (hex.length === 4 || hex.length === 7) {
hex = hex.substr(1);
}
if (hex.length === 3) {
hex = hex.split("");
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
const u = parseInt(hex, 16);
const r = u >> 16;
const g = u >> 8 & 255;
const b = u & 255;
return [r, g, b, 1];
}
if (hex.match(RE_HEXA)) {
if (hex.length === 5 || hex.length === 9) {
hex = hex.substr(1);
}
if (hex.length === 4) {
hex = hex.split("");
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
}
const u = parseInt(hex, 16);
const r = u >> 24 & 255;
const g = u >> 16 & 255;
const b = u >> 8 & 255;
const a = Math.round((u & 255) / 255 * 100) / 100;
return [r, g, b, a];
}
throw new Error(`unknown hex color: ${hex}`);
};
export {
hex2rgb as default
};
//# sourceMappingURL=hex2rgb.js.map