-
Dec 3rd, 2007, 01:58 PM
#1
Thread Starter
Addicted Member
Formula from RGB to HSL(Hue, Sat, Lum)
Does anyone know a formula to convert a RGB long into a public type 'HSL'.
I want it to work the same as the colors on mspaint.
Code:
Public Type HSL
Hue As Long
Saturation As Long
Luminance As Long
End Type
So if I had the RGB long "1108810" ||| and it converted it to a HSL then the HSL value for it would be.
Hue = 70
Saturation = 200
Luminance = 120
-
Dec 3rd, 2007, 02:32 PM
#2
Re: Formula from RGB to HSL(Hue, Sat, Lum)
There are API functions that you can use to convert RGB to HLS and vice versa...
Public Declare Function ColorRGBToHLS Lib "shlwapi.dll" (ByVal clrRGB As Long, pwHue As Long, pwLuminance As Long, pwSaturation As Long) As Long
Public Declare Function ColorHLSToRGB Lib "shlwapi.dll" (ByVal wHue As Long, ByVal wLuminance As Long, ByVal wSaturation As Long) As Long
-
Dec 3rd, 2007, 10:13 PM
#3
Thread Starter
Addicted Member
Re: Formula from RGB to HSL(Hue, Sat, Lum)
-
May 29th, 2012, 05:25 PM
#4
Hyperactive Member
Re: Formula from RGB to HSL(Hue, Sat, Lum)
I've tried using ColorRGBToHLS (and also many other code posted on forums) but I can't seem to get it work. I'm interested in retrieving the SATURATION value off an RGB value.
R: 77
G: 53
B: 69
Should return a saturation of '31'. (checked it on Paint.NET and Photoshop)
Here's the code I used:
Code:
Public Declare Sub ColorRGBToHLS Lib "shlwapi.dll" (ByVal clrRGB As Long, wHue As Integer, wLuminance As Integer, wSaturation As Integer)
Code:
Dim Hue As Integer, Lum As Integer, Sat As Integer
Dim lngBackColor As Long
lngBackColor = RGB(77, 53, 69)
ColorRGBToHLS lngBackColor, Hue, Lum, Sat
MsgBox Sat
... with this piece of code, the value of 'Sat" = 44
What am I missing?
Thank you.
-
May 29th, 2012, 05:49 PM
#5
Re: Formula from RGB to HSL(Hue, Sat, Lum)
Well colour must be a strange science because I just tested your conversion using three different online converters. Two of them report the saturation being 18 % and the 3rd 31.2 % and your code returns 41 for me. Perhaps everyone uses a slightly different formula.
Here are the three sites I tested with:-
The 1st link is the one that returned 31.2 which is closest to what you expected.
-
May 29th, 2012, 07:18 PM
#6
Hyperactive Member
Re: Formula from RGB to HSL(Hue, Sat, Lum)
Hi Niya.
Thank you very much for your input. Like you said, there are different ways to calculate it, so it seems.
Anyway I've found a piece of code that matches the data I see for saturation in Paint.NET and Photoshop. HEre's the link:
http://www.xbeat.net/vbspeed/c_RGBToHSL.htm
This post shows functions from RGBToHSL01 to RGBToHSL04.
RGBToHSL01 is the one that did it for me!
Thanks.
-
Oct 24th, 2020, 08:23 AM
#7
New Member
Re: Formula from RGB to HSL(Hue, Sat, Lum)
Originally Posted by Krass
I've tried using ColorRGBToHLS . . .What am I missing?
The short explanation is the numbers from that API are in a range of 0 to 240, not 0 to 255 as you might expect.
More info in my post on the topic here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|