Results 1 to 8 of 8

Thread: [RESOLVED] HSL to RGB colour space

  1. #1

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Resolved [RESOLVED] HSL to RGB colour space

    I need to convert HSL colours to RGB colours.
    The internet has about ten different routines, formulae or pseudo codes on this, but none is the same. I get different answers on all.
    Is there perhaps somebody who have one which works over the whole domain or color space.
    I am also not sure whether my RGB to HSL conversion routine works 100%, so I need some input on that as well if you please.
    Thanks
    PK

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: HSL to RGB colour space

    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: HSL to RGB colour space

    LaVolpe,
    It seems that the code for that Colour Picker is not in the Code bank yet - or - how would I know that it is and how do I access it in the Code Bank.
    Thanks
    PK

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: HSL to RGB colour space

    The reason why I pointed you to that thread was because it gave a few methods of HSL conversion. And, yes, the color picker was posted to the code bank last 10 days or so. Ensure the "Thread Display Options" section, at bottom of that listing, is not filtering out everything that is just a couple days old.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Thumbs up Re: HSL to RGB colour space

    Thanks for all the guys who helped me.

    I am closing this as I have reached a very good solution and I wish to share my experience here:

    1. There are many code snippets who will take you from RGB to HSL and back .. I wish I could say to a very hot place and back.
    2. Many of them have bugs in the sense that they give you RGB values over 255 and stop on an error.
    3. Test everyone in both directions for a range of colour values and see whether you get the values which you started with. Most won't pass that test.
    4. Finally I used the API's colorHSLtoRGB and colorRGBtoHSL. They both work excellently, but remember the H value only goes to 239 degrees for the whole spectrum and the luminosity goes over 100%. I used up to 230% with success (I did not think that was that possible.)

    Hope it is of use to some member.
    PK

  6. #6
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    Re: HSL to RGB colour space

    @Peekay it's interesting to hear your resolution on this, as some of the things you consider "features" (like not throwing errors or accepting out-of-range luminosity values) are things others would consider "bugs".

    I think that's why there are so many solutions for tasks like RGB <-> HSL conversion. There are trade-offs between performance, accuracy, and strictness, and different developers have different needs.

    Just one last FYI, re this comment:

    Test everyone in both directions for a range of colour values and see whether you get the values which you started with. Most won't pass that test.
    The APIs certainly don't pass this test. If this is a requirement for you, I'm afraid you're out of luck, as some values simply do not map round-trip perfectly between HSL and RGB. (e.g., pass a non-zero hue value with a zero-saturation value through the RGB translation, then try to get your original hue value back. Your starting hue value will always be lost because hue is "undefined" when coming from an RGB triplet with no saturation.)

    If accuracy is your main concern, you'd want a floating-point conversion that uses a true polar conversion instead of a hexagon approximation (like the Atan2() and Sqr()-based solution on the Wiki page), but of course, this yields slightly different results than you get from a typical color picker. So even then, there are trade-offs to be made.

    Anyway, none of this probably helps now that you've found a solution. Just some food for thought, perhaps, and some explanation for why there are so many different "solutions" available.
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

  7. #7

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: [RESOLVED] HSL to RGB colour space

    Hi Tanner_H,

    Yes, your comments are perfectly true and I understand the discreteness and singularities of both the RGB and HSL colour spaces. In fact, I stand in awe when I just think how limited our access to colour spaces are, how many colours spaces we have, how limited they are and how rich and unlimited the colours of light really are.

    My application was very interesting one.

    I have developed a very large Integrated Business System and I wanted to give clients an almost unlimited choice of skins, and in the end decided on 48 skins using the primary colour, the complementary colour and the two triadic ones and different tones of each - four colours per skin. I also wanted a smooth and minute transition from one skin to the next.

    I could not do it with the standard routines on the internet, but the API's did it for me.
    I thus had to manufacture 7200 colours during runtime - impossible to use a colour picker. So now I can make the complete 48 palettes with 150 colours each in two seconds.

    PK

  8. #8

    Thread Starter
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: HSL to RGB colour space

    Name:  Swathes.jpg
Views: 1419
Size:  26.1 KB
    Here is the final result.

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