hd-utils
Version:
A handy utils for modern JS developers
19 lines (18 loc) • 542 B
JavaScript
import isHexColor from '../validation/isHexColor';
/**
* @description converts hex colors to rgba color.
* @example hexToRgbA("#fff") // rgba(255,255,255,1)
*/
export default function hexToRgbA(hex, alpha = 1) {
if (!isHexColor(hex))
throw new Error('Bad Hex');
let c;
c = hex.substring(1).split('');
if (c.length == 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = '0x' + c.join('');
return ('rgba(' +
[(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') +
`,${alpha})`);
}