@modern-kit/utils
Version:
31 lines (27 loc) • 808 B
JavaScript
import { repeatCharacters } from '../../string/repeatCharacters/index.mjs';
const FULL_HEX_LENGTH = 6;
const REPEAT_COUNT = 2;
const getExpandedHex = (hex) => {
const hexString = hex.replace("#", "");
return hexString.length === FULL_HEX_LENGTH ? hexString : 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})`
};
}
export { hexToRgba };
//# sourceMappingURL=index.mjs.map