@zero-deps/hex-2-rgb
Version:
lightweight typescript function to convert hexes of format #rrggbb to rgb(x,y,z) or rgba(x,y,z,a)
18 lines (17 loc) • 375 B
JavaScript
// src/index.ts
function hex2rgb(hex, alpha) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
if (alpha && (alpha < 0 || alpha > 1)) {
alpha = 1;
}
if (alpha) {
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
} else {
return `rgb(${r}, ${g}, ${b})`;
}
}
export {
hex2rgb
};