Author Topic: TIP - Convert colors in RGB to HLS and vice versa.  (Read 2360 times)

0 Members and 1 Guest are viewing this topic.

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2399
  • Gender: Male
    • www.zapsolution.com
TIP - Convert colors in RGB to HLS and vice versa.
« on: January 14, 2012, 03:44:50 PM »
Code: [Select]
#COMPILE EXE

DECLARE SUB ColorRGBToHLS LIB "SHLWAPI.DLL" ALIAS "ColorRGBToHLS" (BYVAL clrRGB AS DWORD, BYREF pwHue AS WORD, BYREF pwLuminance AS WORD, BYREF pwSaturation AS WORD)
DECLARE FUNCTION ColorHLSToRGB LIB "SHLWAPI.DLL" ALIAS "ColorHLSToRGB" (BYVAL wHue AS WORD, BYVAL wLuminance AS WORD, BYVAL wSaturation AS WORD) AS DWORD
DECLARE FUNCTION ColorAdjustLuma LIB "SHLWAPI.DLL" ALIAS "ColorAdjustLuma" (BYVAL clrRGB AS DWORD, BYVAL n AS LONG, BYVAL fScale AS LONG) AS DWORD         

FUNCTION GetRValue(BYVAL clrRGB AS DWORD) AS BYTE
    FUNCTION = (clrRGB AND &H000000FF???)
END FUNCTION

FUNCTION GetGValue(BYVAL clrRGB AS DWORD) AS BYTE
    SHIFT RIGHT clrRGB, 8
    FUNCTION = (clrRGB AND &H000000FF???)
END FUNCTION

FUNCTION GetBValue(BYVAL clrRGB AS DWORD) AS BYTE
    SHIFT RIGHT clrRGB, 16
    FUNCTION = (clrRGB AND &H000000FF???)
END FUNCTION

FUNCTION PBMAIN()

'// RED color in RGB
LOCAL clrRGB AS DWORD
clrRGB = RGB(255, 0, 0)

'// Convert to HLS color space
LOCAL Hue, Luminance, Saturation AS WORD
Hue = 0: Luminance = 0: Saturation = 100
ColorRGBToHLS(clrRGB, Hue, Luminance, Saturation)

'// It is converted to Hue, Luminance and Saturation.
'// You can adjust the parameters according to your preference.
MsgBox "RGB(255, 0, 0) converted to HLS" + $CR _
       + $CR + "Hue =" + STR$(Hue) _
       + $CR + "Luminance =" + STR$(Luminance) _
       + $CR + "Saturation =" + STR$(Saturation),,"RgbHls"

'// Now convert back to RGB.
clrRGB = ColorHLSToRGB(Hue, Luminance, Saturation)
MsgBox "HLS splitted back to RGB values" + $CR _
       + $CR + "Red =" + STR$(GetRValue(clrRGB)) _
       + $CR + "Green =" + STR$(GetGValue(clrRGB)) _
       + $CR + "Blue =" + STR$(GetBValue(clrRGB)),,"RgbHls"

END FUNCTION
« Last Edit: January 14, 2012, 03:46:23 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com