WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Shell LightWeight API calls to ColorRGBToHLS and ColorHLSToRGB

 Keywords: shlwapi.dll hell LightWeight API ColorRGBToHLS ColorHLSToRGB DllCall

Question:

I'm having a devil of a time with a couple of the Shell LightWeight API functions: ColorRGBToHLS and ColorHLSToRGB.

The functions convert to/from Hue Luminance Saturation format.

I appear to be running them OK, but they don't appear to be working because they do not invert each other.

For example:

 255|128|128    ----->       160 / 30 / 240      ----->         0|0|64
   (pink)     ColorRGBToHLS   (h / l / s )     ColorHLSToRGB  (very dark blue)
That isn't even close to right. I can't check the intermediate result because I don't have anything that displays HLS colors.

(There is a very common scheme called Hue Saturation Lightness that is similar, but not the same). Can anyone tell me what color HLS=160/30/240 is? is it pink? Can anyone see what I'm doing wrong in the code below? The spec for the functions is at the bottom, including COLORREF, all cut/paste from the MS website.


; shlw_colors.wbt
#DefineFunction shlwRGBToHLS(rgb)

  r = ItemExtract(1,rgb,"|")
  g = ItemExtract(2,rgb,"|")
  b = ItemExtract(3,rgb,"|")

  rgb = (b << 8 + g) << 8 + r ; make a COLORREF

  bb = BinaryAlloc(6)
  BinaryEodSet(bb,6)
  pwH = IntControl(42,bb,0,0,0) + 0
  pwL = IntControl(42,bb,0,0,0) + 2
  pwS = IntControl(42,bb,0,0,0) + 4

  DllCall(StrCat(DirWindows(1),"shlwapi.dll"), void:"ColorRGBToHLS", long:rgb, long:pwH, long:pwL, long:pwS)

  arr = ArrDimension(3)
  arr[0] = BinaryPeek2(bb,0) ; H
  arr[1] = BinaryPeek2(bb,2) ; L
  arr[2] = BinaryPeek2(bb,4) ; S

  BinaryFree(bb)
  Return arr

#EndFunction

#DefineFunction shlwHLSToRGB(hls)

  h = hls[0]
  l = hls[1]
  s = hls[2]

  rgb = DllCall(StrCat(DirWindows(1),"shlwapi.dll"), long:"ColorHLSToRGB", word:h, word:l, word:s)

  ; decode the COLORREF
  r = rgb       & 255
  g = rgb >> 8  & 255
  b = rgb >> 16 & 255

  rgb = StrCat(r,"|",g,"|",b)
  Return rgb

#EndFunction

arr = shlwRGBToHLS("255|128|128")
Message("HLS",StrCat(arr[0],"/",arr[1],'/',arr[2]))

;arr[2] = arr[2] / 2

rgb = shlwHLSToRGB(arr)
Message("RGB",rgb)

Exit





--------------------------------------------------------------------------------


ColorRGBToHLS Function

Converts colors from RGB to hue-luminance-saturation (HLS) format. 

Syntax

VOID ColorRGBToHLS( COLORREF clrRGB,
WORD *pwHue,
WORD *pwLuminance,
WORD *pwSaturation
);
Parameters

clrRGB
[in] RGB color. 

pwHue
[out] HLS hue value. 

pwLuminance
[out] HLS luminance value. 

pwSaturation
[out] HLS saturation value. 

Return Value
No return value. 

Function Information

Minimum DLL Version shlwapi.dll version 5.0 or later 
Custom Implementation No 
Header shlwapi.h 
Import library shlwapi.lib 
Minimum operating systems Windows 2000, Windows NT 4.0 with Internet Explorer 5, Windows 98, Windows 95 with Internet Explorer 5 


--------------------------------------------------------------------------------

ColorHLSToRGB Function

Converts colors from hue-luminance-saturation (HLS) to RGB format. 

Syntax

COLORREF ColorHLSToRGB( WORD wHue,
WORD wLuminance,
WORD wSaturation
);
Parameters

wHue
HLS hue value. 

wLuminance
HLS luminance value. 

wSaturation
HLS saturation value. 

Return Value
Returns the RGB value. 

Function Information

Minimum DLL Version shlwapi.dll version 5.0 or later 
Custom Implementation No 
Header shlwapi.h 
Import library shlwapi.lib 
Minimum operating systems Windows 2000, Windows NT 4.0 with Internet Explorer 5, Windows 98, Windows 95 with Internet Explorer 5 

--------------------------------------------------------------------------------


ColorAdjustLuma Function

Changes the luminance of a RGB value. Hue and saturation are not affected. 

Syntax

COLORREF ColorAdjustLuma( COLORREF clrRGB,
int n,
BOOL fScale
);
Parameters

clrRGB
Initial RGB value. 

n
Luminance in units of 0.1 percent of the total range. For example, a value of n = 50 corresponds 
to 5 percent of the maximum luminance. 

fScale
If fScale is set to TRUE, n specifies how much to increment or decrement the current luminance. 
If fScale is set to FALSE, n specifies the absolute luminance. 

Return Value
Returns the modified RGB value. 

Remarks

If fScale is set to TRUE, n can range from -1000 to +1000.

If fScale is set to FALSE, n can range from 0 to 1000. Available luminance values range from 0 
to a maximum. If the requested value is negative or exceeds the maximum, the luminance will be 
set to either zero or the maximum value, respectively. 

Function Information

Minimum DLL Version shlwapi.dll version 5.0 or later 
Custom Implementation No 
Header shlwapi.h 
Import library shlwapi.lib 
Minimum operating systems Windows 2000, Windows NT 4.0 with Internet Explorer 5, Windows 98, Windows 95 with Internet Explorer 5 

--------------------------------------------------------------------------------

The COLORREF value is a 32-bit value used to specify an RGB color. 
Remarks

When specifying an explicit RGB color, the COLORREF value has the following 
hexadecimal form: 

0x00bbggrr 

The low-order byte contains a value for the relative intensity of red; 
the second byte contains a value for green; and the third byte contains 
a value for blue. The high-order byte must be zero. The maximum value 
for a single byte is 0xFF. 


--------------------------------------------------------------------------------

Answer:

The API calls were working fine.. It was just getting the right numbers out that was the problem

Change the 1st RGB= to (from shlwRGBToHLS(rgb))

rgb = (b*256*256 + g*256 + r) ; make a COLORREF

And in shlwHLSToRGB(hls) change the decode to

; decode the COLORREF
r = rgb & 255
g = (rgb & (255 * 256)) / 256
b = (rgb & (255 * 256 * 256)) / (256 * 256)
Here is the completely revised coe:
; shlw_colors.wbt
#DefineFunction shlwRGBToHLS(rgb)

  r = ItemExtract(1,rgb,"|")
  g = ItemExtract(2,rgb,"|")
  b = ItemExtract(3,rgb,"|")

  rgb = (b*256*256 + g*256 + r) ; make a COLORREF

  bb = BinaryAlloc(6)
  BinaryEodSet(bb,6)
  pwH = IntControl(42,bb,0,0,0) + 0
  pwL = IntControl(42,bb,0,0,0) + 2
  pwS = IntControl(42,bb,0,0,0) + 4

  DllCall(StrCat(DirWindows(1),"shlwapi.dll"), void:"ColorRGBToHLS", long:rgb, long:pwH, long:pwL, long:pwS)

  arr = ArrDimension(3)
  arr[0] = BinaryPeek2(bb,0) ; H
  arr[1] = BinaryPeek2(bb,2) ; L
  arr[2] = BinaryPeek2(bb,4) ; S

  BinaryFree(bb)
  Return arr

#EndFunction

#DefineFunction shlwHLSToRGB(hls)

  h = hls[0]
  l = hls[1]
  s = hls[2]

  rgb = DllCall(StrCat(DirWindows(1),"shlwapi.dll"), long:"ColorHLSToRGB", word:h, word:l, word:s)

  ; decode the COLORREF
	r = rgb & 255
	g = (rgb & (255 * 256)) / 256
	b = (rgb & (255 * 256 * 256)) / (256 * 256)

  rgb = StrCat(r,"|",g,"|",b)
  Return rgb

#EndFunction

arr = shlwRGBToHLS("255|128|128")
Message("HLS",StrCat(arr[0],"/",arr[1],'/',arr[2]))

;arr[2] = arr[2] / 2

rgb = shlwHLSToRGB(arr)
Message("RGB",rgb)

Exit


Article ID:   W15487
File Created: 2003:05:13:11:28:08
Last Updated: 2003:05:13:11:28:08