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