Color Conversions (Hue/Lum/Saturation) to RGB 
Author Message
 Color Conversions (Hue/Lum/Saturation) to RGB

Hello,

I have a little problem.  I would like to convert a bunch of HLS
values to their RGB equivilant so that I can turn them into decimal
color values to use on my forms.  I have one form that is used for
several related tasks, and the colors change based on the situation.  

I am making the backgrounds of section headers fade as they go down,
and am doing this by increasing the Luminance (or saturation).  I
increase the color's lum by 20 or so each time.

I found a function for this on MS's site, but it doesn't appear to
work :(  I converted it and it's helper functions over from C, but one
of the 3 values is always way way off (2 times or more).  The
functions are mostly math, and I can't find any errors at all.

Cam anyone help?  If there is a module or function out there to do
this, my web searches did not turn it up.  This is the MS KB article
detaling the conversion.

http://www.*-*-*.com/
I set HLSMAX to 240, as in the access color picker.
I have used both long and single as the datatype, with the same
result.

I can send the code to anyone who would like to take a look.  Just
post a followup.

TIA
Andrew



Wed, 21 Apr 2004 07:34:22 GMT  
 Color Conversions (Hue/Lum/Saturation) to RGB
A quick search on DejaGroups turned up a post by Jim Deutch. I have used
his code in the past with good success.


Subject: Re: How do I get the saturation and luminescence values from an
RGB value, using API in VB5.
Newsgroups: microsoft.public.vb.winapi.graphics
View: Complete Thread (7 articles) | Original Format
Date: 1998/06/18

Here's all the color-format conversion routines in my code library:
HSL, YUV, and CMYK.

Jim Deutch
MS Dev MVP

Sub rgb2hsl(ByVal r As Single, ByVal g As Single, ByVal b As Single, h
As
Single, s As Single, l As Single)
    Dim sMax As Single
    Dim sMin As Single
    Dim delta As Single

    r = r / 255
    g = g / 255
    b = b / 255

    sMax = Max(r, Max(g, b))
    sMin = Min(r, Min(g, b))

    l = (sMax + sMin) / 2
    If sMax = sMin Then
        s = 0
        h = 0 'undefined
        l = l * 240
        Exit Sub
    End If

    If l < 0.5 Then
        s = (sMax - sMin) / (sMax + sMin)
    Else
        s = (sMax - sMin) / (2 - sMax - sMin)
    End If
    If s < 0 Then s = 0

    delta = sMax - sMin

    If r = sMax Then
        h = (g - b) / delta
    ElseIf g = sMax Then
        h = 2 + (b - r) / delta
    ElseIf b = sMax Then
        h = 4 + (r - g) / delta
    End If

    h = h * 40
    If h < 0 Then h = h + 240
    s = s * 240
    l = l * 240

End Sub

Sub hsl2rgb(ByVal h As Single, ByVal s As Single, ByVal l As Single, r
As
Single, g As Single, b As Single)
    Dim m1 As Single, m2 As Single

    h = h / 240
    s = s / 240
    l = l / 240

    If s = 0 Then
        r = l * 255
        g = l * 255
        b = l * 255
        Exit Sub
    End If

    If l < 0.5 Then
        m2 = l * (1# + s)
    Else
        m2 = l + s - l * s
    End If
    m1 = 2# * l - m2

    r = HueToRGB(m1, m2, h + 1# / 3#) * 255
    g = HueToRGB(m1, m2, h) * 255
    b = HueToRGB(m1, m2, h - 1# / 3#) * 255

End Sub

Private Function HueToRGB(m1 As Single, m2 As Single, h As Single) As
Single
    If (h < 0) Then h = 1# + h
    If (h > 1) Then h = h - 1#
    If (6# * h < 1) Then
        HueToRGB = (m1 + (m2 - m1) * h * 6#)
    ElseIf (2# * h < 1) Then
        HueToRGB = m2
    ElseIf (3# * h < 2#) Then
        HueToRGB = (m1 + (m2 - m1) * ((2# / 3#) - h) * 6#)
    Else
        HueToRGB = m1
    End If
End Function

Sub Cmyk2Rgb(ByVal c As Single, ByVal m As Single, ByVal y As Single,
ByVal
k As Single, r As Single, g As Single, b As Single)

    If (c + k) < 255 Then r = 255 - (c + k) Else r = 0
    If (m + k) < 255 Then g = 255 - (m + k) Else g = 0
    If (y + k) < 255 Then b = 255 - (y + k) Else b = 0

End Sub

Sub Rgb2Cmyk(ByVal r As Single, ByVal g As Single, ByVal b As Single, c
As
Single, m As Single, y As Single, k As Single)

    c = 255 - r
    m = 255 - g
    y = 255 - b
    If c < m Then k = c Else k = m
    If y < k Then k = y
    If k > 0 Then
        c = c - k
        m = m - k
        y = y - k
    End If

End Sub

Sub RGB2YUV(ByVal r As Single, ByVal g As Single, ByVal b As Single, y
As
Single, u As Single, v As Single)
    y = 0.299 * r + 0.587 * g + 0.114 * b
    u = (-0.169 * r) + (-0.331 * g) + (0.5 * b)
    v = (0.5 * r) + (-0.419 * g) + (-0.081 * b)
End Sub

Private Function Max(ByVal s1 As Single, ByVal s2 As Single) As Single
    If s1 > s2 Then
        Max = s1
    Else
        Max = s2
    End If
End Function

Private Function Min(ByVal s1 As Single, ByVal s2 As Single) As Single
    If s1 < s2 Then
        Min = s1
    Else
        Min = s2
    End If
End Function

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Quote:

> Hello,

> I have a little problem.  I would like to convert a bunch of HLS
> values to their RGB equivilant so that I can turn them into decimal
> color values to use on my forms.  I have one form that is used for
> several related tasks, and the colors change based on the situation.

> I am making the backgrounds of section headers fade as they go down,
> and am doing this by increasing the Luminance (or saturation).  I
> increase the color's lum by 20 or so each time.

> I found a function for this on MS's site, but it doesn't appear to
> work :(  I converted it and it's helper functions over from C, but one
> of the 3 values is always way way off (2 times or more).  The
> functions are mostly math, and I can't find any errors at all.

> Cam anyone help?  If there is a module or function out there to do
> this, my web searches did not turn it up.  This is the MS KB article
> detaling the conversion.

> http://support.microsoft.com/support/kb/articles/Q29/2/40.ASP
> I set HLSMAX to 240, as in the access color picker.
> I have used both long and single as the datatype, with the same
> result.

> I can send the code to anyone who would like to take a look.  Just
> post a followup.

> TIA
> Andrew



Wed, 21 Apr 2004 08:03:55 GMT  
 Color Conversions (Hue/Lum/Saturation) to RGB
Thanks!

Whenever I need to find something, the last thing that I think of is
doing a newsgroup search.  I go and search google and go to your site,
candice's, access developer, etc... and then I post a question in the
group :)  I would throw it at the mailing list but I am at work and on
a wimpy 56k line.

I use googe to search, don't know why I never use google groups to
search (well, it *is* new), but I never used deja to search either.  

As for the calculations that code produces... they are off by a tiny
margin.  I don't give a {*filter*}about totall accuracy, but I wonder where
it looses a bit (or does the MS one just do the rounding a bit
different?).  To duplicate the color picker values (using just the end
results) I am going to need to round some values up and some down, and
thier decimal part does not indicate which direction (.7 needs to be
rounded down)...

 I may play with it more to get it to match the values exactly, but
the difference is not visible anyway.  Great code, and thanks to the
gentleman who posted it.  

BTW. I did the same search (color conversion HSL) on google groups in
ms.pub.vb and found the article you referenced.  For those interested,
here is the link (long!!!)
http://www.*-*-*.com/ %24egn%242%40rema...

Thanks again, and for pointing out a resource that is underutilized by
me :(  I could have saved myself so much time.

Thanks,
Andrew Backer

On Sat, 03 Nov 2001 00:03:55 GMT, "Stephen Lebans"

Quote:

>A quick search on DejaGroups turned up a post by Jim Deutch. I have used
>his code in the past with good success.


>Subject: Re: How do I get the saturation and luminescence values from an
>RGB value, using API in VB5.



Wed, 21 Apr 2004 08:52:10 GMT  
 Color Conversions (Hue/Lum/Saturation) to RGB
Here are the functions I wrote to compliment the conversion and deal
with the control's 'decimal' color format.  The use the functions you
pointed me to in your post, but basically deal with extracting the rgb
values from the decimal color value.

I have also attached the module I made that is composed of those
functions and these on top of them.  Very usefull.

Need a penny take a penny, have a penny leave a penny.

Thanks,
Andrew Backer

-------------------------
'Returns a decimal value that is a color code you can use to set a
back/fore/etc. color with
Public Function HSLToDecimal(hue As Single, sat As Single, lum As
Single) As Long
    Dim red As Single, green As Single, blue As Single
    Dim ret As Long
    Call hsl2rgb(hue, sat, lum, red, green, blue)
    ret = RGB(CInt(red), CInt(green), CInt(blue))
    color_HSLToDecimal = ret
End Function

'just a wrapper for 2 other functions
Public Function DecimalToHSL(ByVal Dec As Long, hue As Single, sat As
Single, lum As Single)
    Dim red As Single, blue As Single, green As Single

    'extract the RGB values, then convert them to HSL
    Call DecimalToRGB(Dec, red, green, blue)

    'now convert it over to HSL
    Call rgb2hsl(red, green, blue, hue, sat, lum)
End Function

Public Function DecimalToRGB(ByVal Dec As Long, red As Single, blue As
Single, green As Single)
    'decimal is in this format BBGGRR when converted to hex.  2 hex
per color (BB is the
    '2 hex for the blue value)

    Dim arrHex(3) As String
    Dim sHex

    If (Dec < 0) Then Exit Function
    sHex = hex(Dec)

    'if we do not have a full string.  If blue is missing then the
string will be just 'GGRR'
    'if both blue & green are, it is just 'RR'
    'if everything is zero (black) then it will be just '0'
    'so we need to add the extra zeros so we can simply the decoding,
and use 1 step instead of 3
    If (Len(sHex < 6)) Then
        sHex = String(6 - Len(sHex), "0") & sHex 'how we have our
leading zeroes
    End If

    'tack on a '&H' to the hex chars so that the conversion function
knows they are hex
    arrHex(1) = "&H" & Right(sHex, 2)  'right 2 chars are the red
    arrHex(2) = "&H" & Mid(sHex, 3, 2) 'middle 2 are the green
    arrHex(3) = "&H" & Left(sHex, 2)  'left 2 are the blue

    red = CSng(arrHex(1))
    green = CSng(arrHex(2))
    blue = CSng(arrHex(3))

End Function

On Sat, 03 Nov 2001 00:03:55 GMT, "Stephen Lebans"

Quote:

>A quick search on DejaGroups turned up a post by Jim Deutch. I have used
>his code in the past with good success.


>Subject: Re: How do I get the saturation and luminescence values from an
>RGB value, using API in VB5.
>Newsgroups: microsoft.public.vb.winapi.graphics
>View: Complete Thread (7 articles) | Original Format
>Date: 1998/06/18

>Here's all the color-format conversion routines in my code library:
>HSL, YUV, and CMYK.



Wed, 21 Apr 2004 09:43:00 GMT  
 Color Conversions (Hue/Lum/Saturation) to RGB
Don't know why agent screws up when posing.  It both does not put them
as a followup when I have an attachment, AND makes the attacment a
seperate file.  Guess some alt.test fun is what I need to try and
resolve this.

There is a bad line in HSLToDecimal.  The return value is set to the
old name of the function.  Last line, remove the "color_" and
everythign should work fine.

Sorry

On Sat, 03 Nov 2001 01:43:00 GMT, Andrew Backer

Quote:

>Here are the functions I wrote to compliment the conversion and deal
>with the control's 'decimal' color format.  The use the functions you
>pointed me to in your post, but basically deal with extracting the rgb
>values from the decimal color value



Wed, 21 Apr 2004 09:46:22 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. HELP: Convert RGB Color to Hue, Saturation, Lumenosity

2. Hue, Sat and Lum

3. Hue/ Saturation Values

4. Hue/Saturation

5. 256 Colors to RGB conversion?

6. Color Conversions (RGB, CMYK, HSB)

7. RGB to CIELAB (or simply lab) color conversion

8. Converting RGB to Hue/Sat/Luminance

9. HSV-to-RGB and RGB-to-HSV conversion

10. VB Colors vs. Web Hex RGB colors

11. Converting RGB-color to HEX color

12. Converting RGB-color to HEX color

 

 
Powered by phpBB® Forum Software