Created
June 6, 2025 16:06
-
-
Save rodionbykov/09a1fa1e6638a1f9d0baf5cf7455fdb3 to your computer and use it in GitHub Desktop.
HEX to HSL color conversion function
This file contains hidden or 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
<cfscript> | |
/** | |
* Convert a hex RGB triplet to HSL (hue, saturation, luminance). | |
* | |
* @param RGBTriplet Hex RGB triplet to convert to HSL triplet. | |
* @return Returns a comma delimited list of values. | |
* @author Matthew Walker | |
* @version 1, November 6, 2001 | |
* @author Roddy Bykov | |
* @version 2, June 6, 2025 | |
*/ | |
function hexToHSL(RGBTriplet) { | |
// ref Foley and van Dam, Fundamentals of Interactive Computer Graphics | |
var R = 0; | |
var G = 0; | |
var B = 0; | |
var H = 0; | |
var S = 0; | |
var L = 0; | |
var MinColor = 0; | |
var MaxColor = 0; | |
var MCL = ""; | |
var ISGREY = false; | |
R = InputBaseN(Left(RGBTriplet, 2), 16); | |
G = InputBaseN(Mid(RGBTriplet, 3, 2), 16); | |
B = InputBaseN(Right(RGBTriplet, 2), 16); | |
MinColor = Min(R, Min(G, B)); | |
MaxColor = Max(R, Max(G, B)); | |
if ( MaxColor EQ MinColor ) { | |
ISGREY = true; | |
} | |
if( R EQ MaxColor) { | |
MCL = "R"; | |
} | |
if( G EQ MaxColor) { | |
MCL = "G"; | |
} | |
if( B EQ MaxColor) { | |
MCL = "B"; | |
} | |
R = R / 255; | |
G = G / 255; | |
B = B / 255; | |
MinColor = MinColor / 255; | |
MaxColor = MaxColor / 255; | |
L = (MaxColor + MinColor)/2; | |
if ( NOT ISGREY ) { | |
if ( L LT 0.5 ) { | |
S=(MaxColor-MinColor)/(MaxColor+MinColor); | |
} else { | |
S=(MaxColor-MinColor)/(2-MaxColor-MinColor); | |
} | |
if ( MCL EQ "R" ) { | |
H = (G-B)/(MaxColor-MinColor); | |
if ( G LT B ) { | |
H = H + 6; | |
} | |
} | |
if ( MCL EQ "G" ) { | |
H = 2 + (B-R)/(MaxColor-MinColor); | |
} | |
if ( MCL EQ "B" ) { | |
H = 4 + (R-G)/(MaxColor-MinColor); | |
} | |
H = (360 * H) / 6; | |
} | |
return "#Round(H)#, #Round(S * 100)#%, #Round(L * 100)#%"; | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment