@mapcss/preset-typography
Version:
Typography preset for MapCSS
41 lines (40 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hex2RGBA = void 0;
function hex2RGBA(str) {
const [, body] = str.match(/^#?([\da-f]+)$/i) || [];
if (!body) {
return;
}
switch (body.length) {
case 3:
case 4: {
const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => (n << 4) | n);
const [r, g, b] = digits;
return {
r,
g,
b,
a: body.length === 3
? undefined
: Math.round(digits[3] / 255 * 100) / 100,
};
}
case 6:
case 8: {
const value = Number.parseInt(body, 16);
const [r, g, b] = body.length === 6
? [(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]
: [(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF];
return {
r,
g,
b,
a: body.length === 6
? undefined
: Math.round((value & 0xFF) / 255 * 100) / 100,
};
}
}
}
exports.hex2RGBA = hex2RGBA;