@modern-kit/utils
Version:
33 lines (28 loc) • 860 B
JavaScript
;
var stringRepeatCharacters = require('../../string/repeatCharacters/index.cjs');
const FULL_HEX_LENGTH = 6;
const REPEAT_COUNT = 2;
const getExpandedHex = (hex) => {
const hexString = hex.replace("#", "");
return hexString.length === FULL_HEX_LENGTH ? hexString : stringRepeatCharacters.repeatCharacters(hexString, REPEAT_COUNT);
};
const HEXADECIMAL = 16;
function hexToRgba(hex, alpha = 1) {
const regex = /^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/;
if (!regex.test(hex)) {
return null;
}
const expandedHex = getExpandedHex(hex);
const [r, g, b] = [0, 2, 4].map((offset) => {
return parseInt(expandedHex.slice(offset, offset + 2), HEXADECIMAL);
});
return {
r,
g,
b,
a: alpha,
stringifiedValue: `rgba(${r},${g},${b},${alpha})`
};
}
exports.hexToRgba = hexToRgba;
//# sourceMappingURL=index.cjs.map