Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleConvert RGB values into HLS values and vice versa
KeywordsRGB, HLS, color, convert
CategoriesGraphics
 
Visual Basic uses RGB (red, green, and blue) component values to specify colors. In Visual Basic, the component values must be between 0 and 255. In this example, the values should be between 0.0 and 1.0.

The HLS system uses the values hue, lightness, and saturation. Hue determines the color with a 0 to 360 degree direction on a color wheel.

Lightness indicates how much light is in the color. When lightness = 0, the color is black. When lightness = 1, the color is white. When lightness = 0.5, the color is as "pure" as possible.

Saturation indicates the amount of color added. You can think of this as the opposite of "grayness." When saturation = 0, the color is pure gray. In this case, if lightness = 0.5 you get a neutral color. When saturation is 1, the color is "pure."

Here are some examples:

ColorRGBHLSSample
Red1.00.00.000.51.0 
Yellow1.01.00.0600.51.0 
Green0.01.00.01200.51.0 
Cyan0.01.01.01800.51.0 
Blue0.00.01.02400.51.0 
Magenta1.00.01.03000.51.0 
White1.01.01.0(any)1.0(any) 
Black0.00.00.0(any)0.0(any) 
Maroon0.50.00.000.251.0 
Pink1.00.50.500.751.0 

Use the example program to experiment with other values. Remember to keep RGB values between 0.0 and 1.0.

This example uses code translated from the first edition of the book Three-Dimensional Computer Graphics by Alan Watt.

The following code converts an RGB value into an HLS value.

 
' Convert an RGB value into an HLS value.
Private Sub RgbToHls(ByVal R As Double, ByVal G As Double, _
    ByVal B As Double, ByRef H As Double, ByRef L As _
    Double, ByRef S As Double)
Dim max As Double
Dim min As Double
Dim diff As Double
Dim r_dist As Double
Dim g_dist As Double
Dim b_dist As Double

    ' Get the maximum and minimum RGB components.
    max = R
    If max < G Then max = G
    If max < B Then max = B

    min = R
    If min > G Then min = G
    If min > B Then min = B

    diff = max - min
    L = (max + min) / 2
    If Abs(diff) < 0.00001 Then
        S = 0
        H = 0   ' H is really undefined.
    Else
        If L <= 0.5 Then
            S = diff / (max + min)
        Else
            S = diff / (2 - max - min)
        End If

        r_dist = (max - R) / diff
        g_dist = (max - G) / diff
        b_dist = (max - B) / diff

        If R = max Then
            H = b_dist - g_dist
        ElseIf G = max Then
            H = 2 + r_dist - b_dist
        Else
            H = 4 + g_dist - r_dist
        End If

        H = H * 60
        If H < 0 Then H = H + 360
    End If
End Sub
 
The following code converts an HLS value into an RGB value.
 
' Convert an HLS value into an RGB value.
Private Sub HlsToRgb(ByVal H As Double, ByVal L As Double, _
    ByVal S As Double, ByRef R As Double, ByRef G As _
    Double, ByRef B As Double)
Dim p1 As Double
Dim p2 As Double

    If L <= 0.5 Then
        p2 = L * (1 + S)
    Else
        p2 = L + S - L * S
    End If
    p1 = 2 * L - p2
    If S = 0 Then
        R = L
        G = L
        B = L
    Else
        R = QqhToRgb(p1, p2, H + 120)
        G = QqhToRgb(p1, p2, H)
        B = QqhToRgb(p1, p2, H - 120)
    End If
End Sub

Private Function QqhToRgb(ByVal q1 As Double, ByVal q2 As _
    Double, ByVal hue As Double) As Double
    If hue > 360 Then
        hue = hue - 360
    ElseIf hue < 0 Then
        hue = hue + 360
    End If
    If hue < 60 Then
        QqhToRgb = q1 + (q2 - q1) * hue / 60
    ElseIf hue < 180 Then
        QqhToRgb = q2
    ElseIf hue < 240 Then
        QqhToRgb = q1 + (q2 - q1) * (240 - hue) / 60
    Else
        QqhToRgb = q1
    End If
End Function
 
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated