Long to .....

DR00ME

Well-known member
Joined
Feb 6, 2004
Messages
169
Location
Finland
Hi, Im using API- function:

[VB]Private Declare Function ColorHLSToRGB Lib "SHLWAPI.DLL" _
(ByVal wHue As Integer, ByVal wLuminance As Integer, ByVal wSaturation As Integer) As Long[/VB]


How do I convert Long to RGB so I could take the R,G and B component out of it.
 
This is something that Squirm came up with a couple of years ago...

Code:
dim nColor as long = yourValue
                nRed = nColor And 255
                nGreen = (nColor \ 256) And 255
                nBlue = (nColor \ 65536) And 255
 
R=252, G=254, B=214 converted to HSB is H=63, S=0,952381, B=0,9176471
This is a fact I know (I got the values from the same pixel)
(pixel.R pixel.G pixel.B) and (pixel.GetHue, pixel.GetSaturation, pixel.GetBrightness)

B is sometimes called V (value) or L(luminance) I guess... anyway.

Now here is my function and some code:

[VB]Private Declare Function ColorHLSToRGB Lib "SHLWAPI.DLL" _
(ByVal wHue As Integer, ByVal wLuminance As Integer, ByVal wSaturation As Integer) As Long


Dim nColor as Long
Dim nRed,nGreen,nBlue as Integer

nColor = ColorHLSToRGB(63, 0.9176471, 0.952381)

nRed = nColor And 255
nGreen = (nColor / 256) And 255
nBlue = (nColor / 65536) And 255


Me.Text = CStr(nRed) + " " + CStr(nGreen) + " " + CStr(nBlue)[/VB]

Why it is giving me a wrong result(1 1 1) instead of (252 254 214) please help this is doing my head in.
 
Last edited by a moderator:
I think Im not gonna use that bloody API. Gonna make my own function which actually works properly = gives you proper Byte values.

Thanks everyone who participated on this thread.

btw. bri189a sometimes I want to do same as your avatar.
 
Thanks... Im currently doing that now with my db problem... I guess the results from the API werent what you expected?

:)
 
In case you were wondering, the problem lies in this line:
Code:
nColor = ColorHLSToRGB(63, 0.9176471, 0.952381)

Since the API only accepts integers, the last two parameters were
rounded off.
 
[VB]Private Declare Function ColorHLSToRGB Lib "SHLWAPI.DLL" _
(ByVal wHue As Single, ByVal wLuminance As Single, ByVal wSaturation As Single) As Long[/VB]

As you can see I changed the types to singles... the result changed but it is still a wrong result.


Now the long is: 1005025044970

According to this formula:
nRed = nColor And 255
nGreen = (nColor / 256) And 255
nBlue = (nColor / 65536) And 255

it gives result of: 234 42 41 (RGB) = wrong answer.
 
Changing the declaration to single rather than integer doesnt alter the fact that the under-lying API still uses integers. You are just slightly changing the rounding error.
 
I think ColorRGBToHLS is using standard values ? like saturation min=0 max=1 and brightness min=0 max=1... so they are single not integer... I dont know from where I got the failure API structure but there shouldnt be integers as input. The under-lying API is using long or singles. Maybe I should try Longs too.


????????????????
 
Last edited by a moderator:
Nope, the api doesnt accept floating point values - just checked in MSDN and they accept 16-bit unsigned integers and returns a 32-bit integer so I suppose the proper declaration is

Code:
Private Declare Function ColorHLSToRGB Lib "SHLWAPI.DLL" _
    (ByVal wHue As Short, ByVal wLuminance As Short, ByVal wSaturation As Short) As integer
but as Bucky said - the 2nd and 3rd parameters are being rounded off. Where are you getting the values for the last two parameters from?
 
Oh I see, well do you know the min and max values for saturation and luminance for input ? Like what standard it is using ? I guess it doesnt mean Short max value is e.g. saturation max value ? This is a bit confusing now.

pls dont tell me it is using max values like 239,240,240 = MS Paint (hue,sat,lum) .... oh dear...this is something I didnt want to know....:) but then again it doesnt make any sense why the API is using short as Sat. and Lum. input instead of Byte (min. 0 max 255) grrr :/


(63, 0.9176471, 0.952381) I got those values using .hue .getsaturation and .getbrightness from a specific pixel.... and I cant convert them to right RGB values without using my own function... ????
 
Last edited by a moderator:
Back
Top