UNPKG

@zero-deps/hex-2-rgb

Version:

lightweight typed function to convert hexes of format #rrggbb to rgb(x,y,z) or rgba(x,y,z,a) without runtime dependencies

18 lines (16 loc) 432 B
//#region src/index.ts /** * @example * hex2rgb('#000000') * hex2rgb('#FFFFFF', 0.5) */ 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})`; } //#endregion exports.hex2rgb = hex2rgb;