Last active
January 31, 2025 01:59
-
-
Save xenozauros/f6e185c8de2a04cdfecf to your computer and use it in GitHub Desktop.
Javascript: HEX to RGB to HSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hexToHSL(hex) { | |
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); | |
r = parseInt(result[1], 16); | |
g = parseInt(result[2], 16); | |
b = parseInt(result[3], 16); | |
r /= 255, g /= 255, b /= 255; | |
var max = Math.max(r, g, b), min = Math.min(r, g, b); | |
var h, s, l = (max + min) / 2; | |
if(max == min){ | |
h = s = 0; // achromatic | |
}else{ | |
var d = max - min; | |
s = l > 0.5 ? d / (2 - max - min) : d / (max + min); | |
switch(max){ | |
case r: h = (g - b) / d + (g < b ? 6 : 0); break; | |
case g: h = (b - r) / d + 2; break; | |
case b: h = (r - g) / d + 4; break; | |
} | |
h /= 6; | |
} | |
var HSL = new Object(); | |
HSL['h']=h; | |
HSL['s']=s; | |
HSL['l']=l; | |
return HSL; | |
} |
Thank you : )
Much appreciated
Thank you! Here is a variation that returns in hsl()
CSS format, e.g. hexToCssHsl(#1d4e8f)
➡️ hsl(214,66%,34%)
:
function hexToCssHsl(hex, valuesOnly = false) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
var r = parseInt(result[1], 16);
var g = parseInt(result[2], 16);
var b = parseInt(result[3], 16);
var cssString = '';
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
h = Math.round(h * 360);
s = Math.round(s * 100);
l = Math.round(l * 100);
cssString = h + ',' + s + '%,' + l + '%';
cssString = !valuesOnly ? 'hsl(' + cssString + ')' : cssString;
return cssString;
}
Btw, I never knew about the /=
division assignment operator until today 🤯
Awesome job! Thank you!
Typescript version that avoids var
and ==
:
function hexToCssHsl(hex: string, valuesOnly = false) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (!result || result.length !== 3) throw "Failed to parse hex";
let r = parseInt(result[1], 16);
let g = parseInt(result[2], 16);
let b = parseInt(result[3], 16);
let cssString = "";
(r /= 255), (g /= 255), (b /= 255);
const max = Math.max(r, g, b),
min = Math.min(r, g, b);
let h,
s,
l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}
h = Math.round(h * 360);
s = Math.round(s * 100);
l = Math.round(l * 100);
cssString = h + "," + s + "%," + l + "%";
cssString = !valuesOnly ? "hsl(" + cssString + ")" : cssString;
return cssString;
}
Typescript version that avoids
var
and==
:function hexToCssHsl(hex: string, valuesOnly = false) { const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); if (!result || result.length !== 3) throw "Failed to parse hex"; let r = parseInt(result[1], 16); let g = parseInt(result[2], 16); let b = parseInt(result[3], 16); let cssString = ""; (r /= 255), (g /= 255), (b /= 255); const max = Math.max(r, g, b), min = Math.min(r, g, b); let h, s, l = (max + min) / 2; if (max === min) { h = s = 0; // achromatic } else { const d = max - min; s = l > 0.5 ? d / (2 - max - min) : d / (max + min); switch (max) { case r: h = (g - b) / d + (g < b ? 6 : 0); break; case g: h = (b - r) / d + 2; break; case b: h = (r - g) / d + 4; break; } h /= 6; } h = Math.round(h * 360); s = Math.round(s * 100); l = Math.round(l * 100); cssString = h + "," + s + "%," + l + "%"; cssString = !valuesOnly ? "hsl(" + cssString + ")" : cssString; return cssString; }
result.length !== 3
check should result.length !== 4
the 0 element is full matched hex string.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has saved my life thank you!