UNPKG

@yookue/ts-lang-utils

Version:

Common lang utilities for typescript

17 lines 540 B
export function hexToRgb(hex) { if (!hex) { return undefined; } var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; var rep = hex.replace(rgx, function (m, r, g, b) { return r + r + g + g + b + b; }); var arr = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(rep); if (!arr || arr.length !== 4) { return undefined; } var hexR = Number.parseInt(arr[1], 16); var hexG = Number.parseInt(arr[2], 16); var hexB = Number.parseInt(arr[3], 16); return "rgb(".concat(hexR, ", ").concat(hexG, ", ").concat(hexB, ")"); }