Here are a few functions that you can check out that will allow you to use HSL.
Code: Select all
;**************************************
HSL_ToRGB(hue, sat:=1, lum:=0.5 ) {
; Function by [VxE]. See > http://www.wikipedia.org/wiki/HSV_color_space
; HSL to/from RGB conversion functions by [VxE]. Freely avalable @ http://www.autohotkey.com/community/viewtopic.php?f=2&t=88707
; Converts a hue/sat/lum into a 24-bit RGB color code. Input: 0 <= hue <= 360, 0 <= sat <= 1, 0 <= lum <= 1.
Static i24 := 0xFFFFFF, i40 := 0xFFFFFF0000, hx := "0123456789ABCDEF"
; Transform the decimal inputs into 24-bit integers. Integer arithmetic is nice..
sat := ( sat * i24 ) & i24
lum := ( lum * i24 ) & i24
hue := ( hue * 0xB60B60 >> 8 ) & i24 ; conveniently, 360 * 0xB60B60 = 0xFFFFFF00
; Determine the chroma value and put it in the 'sat' var since the saturation value is not used after this.
sat := lum + Round( sat * ( i24 - Abs( i24 - lum - lum ) ) / 0x1FFFFFE )
; Calculate the base values for red and blue (green's base value is the hue)
red := hue < 0xAAAAAA ? hue + 0x555555 : hue - 0xAAAAAA
blu := hue < 0x555555 ? hue + 0xAAAAAA : hue - 0x555555
; Run the blue value through the cases
If ( blu < 0x2AAAAB )
blu := sat + 2 * ( i24 - 6 * blu ) * ( lum - sat ) / i24 >> 16
Else If ( blu < 0x800000 )
blu := sat >> 16
Else If ( blu < 0xAAAAAA )
blu := sat + 2 * ( i24 - 6 * ( 0xAAAAAA - blu ) ) * ( lum - sat ) / i24 >> 16
Else
blu := 2 * lum - sat >> 16
; Run the red value through the cases
If ( red < 0x2AAAAB )
red := sat + 2 * ( i24 - 6 * red ) * ( lum - sat ) / i24 >> 16
Else If ( red < 0x800000 )
red := sat >> 16
Else If ( red < 0xAAAAAA )
red := sat + 2 * ( i24 - 6 * ( 0xAAAAAA - red ) ) * ( lum - sat ) / i24 >> 16
Else
red := 2 * lum - sat >> 16
; Run the green value through the cases
If ( hue < 0x2AAAAB )
hue := sat + 2 * ( i24 - 6 * hue ) * ( lum - sat ) / i24 >> 16
Else If ( hue < 0x800000 )
hue := sat >> 16
Else If ( hue < 0xAAAAAA )
hue := sat + 2 * ( i24 - 6 * ( 0xAAAAAA - hue ) ) * ( lum - sat ) / i24 >> 16
Else
hue := 2 * lum - sat >> 16
; Return the values in RGB as a hex integer
Return "0x" SubStr( hx, ( red >> 4 ) + 1, 1 ) SubStr( hx, ( red & 15 ) + 1, 1 )
. SubStr( hx, ( hue >> 4 ) + 1, 1 ) SubStr( hx, ( hue & 15 ) + 1, 1 )
. SubStr( hx, ( blu >> 4 ) + 1, 1 ) SubStr( hx, ( blu & 15 ) + 1, 1 )
} ; END - HSL_ToRGB( hue, sat, lum )
;*****************************************************
HSL_Lum( RGB ) {
; Function by [VxE]. Returns the HSL lightness of the input 24 bit color.
; Returns a floating point value between 0 and 1, inclusive.
blu := 255 & ( RGB )
grn := 255 & ( RGB >> 8 )
red := 255 & ( RGB >> 16 )
If ( blu < grn )
{
If ( red < blu )
Return ( red + grn ) / 510
Else If ( grn < red )
Return ( blu + red ) / 510
Else
Return ( blu + grn ) / 510
} Else
{
If ( red < grn )
Return ( red + blu ) / 510
Else If ( blu < red )
Return ( grn + red ) / 510
Else
Return ( grn + blu ) / 510
}
} ; END - HSL_Lum( RGB )
;*****************************************************
ConvertRGBtoHSL(R, G, B) {
; http://www.easyrgb.com/index.php?X=MATH&H=18#text18
SetFormat, float, 0.5 ; for some reason I need this for some colors to work.
R := (R / 255)
G := (G / 255)
B := (B / 255)
Min := min(R, G, B)
Max := max(R, G, B)
del_Max := Max - Min
L := (Max + Min) / 2
if (del_Max = 0)
{
H := S := 0
} else
{
if (L < 0.5)
S := del_Max / (Max + Min)
else
S := del_Max / (2 - Max - Min)
del_R := (((Max - R) / 6) + (del_Max / 2)) / del_Max
del_G := (((Max - G) / 6) + (del_Max / 2)) / del_Max
del_B := (((Max - B) / 6) + (del_Max / 2)) / del_Max
if (R = Max)
{
H := del_B - del_G
} else
{
if (G = Max)
H := (1 / 3) + del_R - del_B
else if (B = Max)
H := (2 / 3) + del_G - del_R
}
if (H < 0)
H += 1
if (H > 1)
H -= 1
}
; return round(h*360) "," s "," l
; return (h*360) "," s "," l
return [abs(round(h*360, 3)), abs(s), abs(l)]
}
;*******************************************************************
ConvertHSLtoRGB(H, S, L) {
; http://www.had2know.com/technology/hsl-rgb-color-converter.html
H := H/360
if (S == 0)
{
R := L*255
G := L*255
B := L*255
} else
{
if (L < 0.5)
var_2 := L * (1 + S)
else
var_2 := (L + S) - (S * L)
var_1 := 2 * L - var_2
R := 255 * ConvertHueToRGB(var_1, var_2, H + (1 / 3))
G := 255 * ConvertHueToRGB(var_1, var_2, H)
B := 255 * ConvertHueToRGB(var_1, var_2, H - (1 / 3))
}
; Return round(R) "," round(G) "," round(B)
; Return (R) "," (G) "," (B)
Return [round(R), round(G), round(B)]
}
;********************************************************************
ConvertHueToRGB(v1, v2, vH) {
vH := ((vH<0) ? ++vH : vH)
vH := ((vH>1) ? --vH : vH)
return ((6 * vH) < 1) ? (v1 + (v2 - v1) * 6 * vH)
: ((2 * vH) < 1) ? (v2)
: ((3 * vH) < 2) ? (v1 + (v2 - v1) * ((2 / 3) - vH) * 6)
: v1
}
;**********************************************************************
RGBtoHEXandBack(clr) {
If (StrLen(clr)=9)
{
R := SubStr(clr, 1, 3)
G := SubStr(clr, 4, 3)
B := SubStr(clr, 7, 3)
z := Format("{1:02x}", R) Format("{1:02x}", G) Format("{1:02x}", B)
} Else
{
R := "0x" SubStr(clr, 1, 2)
G := "0x" SubStr(clr, 3, 2)
B := "0x" SubStr(clr, 5, 2)
z := Format("{1:03d}", R) Format("{1:03d}", G) Format("{1:03d}", B)
}
Return z
}