# hexToHsl typescript function

iam making a javascript library called functionality and iam wondering how to make hexToHsl function with typescript i had serched a lot but all i have got is in js and can not convert to typescript
what i have found in js.

``````function hexToHsl(hex) {
// Convert hex to RGB first
let r, g, b;
if (hex.length === 4) {
r = "0x" + hex + hex;
g = "0x" + hex + hex;
b = "0x" + hex + hex;
} else if (hex.length === 7) {
r = "0x" + hex + hex;
g = "0x" + hex + hex;
b = "0x" + hex + hex;
}
// Then to HSL
r /= 255;
g /= 255;
b /= 255;
let cmin = Math.min(r, g, b),
cmax = Math.max(r, g, b),
delta = cmax - cmin,
h = 0,
s = 0,
l = 0;

if (delta === 0) h = 0;
else if (cmax === r) h = ((g - b) / delta) % 6;
else if (cmax === g) h = (b - r) / delta + 2;
else h = (r - g) / delta + 4;

h = Math.round(h * 60);

if (h < 0) h += 360;

l = (cmax + cmin) / 2;
s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
s = +(s * 100).toFixed(1);
l = +(l * 100).toFixed(1);

console.log(
"hsl(" + Math.round(h) + "," + Math.round(s) + "," + Math.round(l) + ")"
);
}

``````
Posted on Categories javascript