Results 1 to 7 of 7

Thread: Formula from RGB to HSL(Hue, Sat, Lum)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    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

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    169

    Re: Formula from RGB to HSL(Hue, Sat, Lum)

    Thanks man... Rated

  4. #4
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    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.
    Chris

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    488

    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.
    Chris

  7. #7
    New Member ashleedawg's Avatar
    Join Date
    Sep 2018
    Posts
    1

    Re: Formula from RGB to HSL(Hue, Sat, Lum)

    Quote Originally Posted by Krass View Post
    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
  •  



Click Here to Expand Forum to Full Width