Results 1 to 4 of 4

Thread: HSB and RGB Colours

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    7

    Post

    Does anyone know how to convert HSB colour values to RGB values, as I want to make some graphics that change through the spectrum with time.

    Thanks

    Chris

    ------------------
    "Trust No One, Including Yourself"

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Post

    What's HSB? I have functions that convert a long to RGB componants or HEX to RGB... Am I playing the right sport?


  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    7

    Post

    HSB stands for Hue (Pos. in Spectrum), Saturation (Intensity of Colour) and Brightness. It is useful for making rainbows or other nice effects.

    ------------------
    "Trust No One, Including Yourself"

  4. #4
    Addicted Member
    Join Date
    Feb 2000
    Posts
    224

    Post

    Hope this helps , with due credits to
    Neil Pearson

    '============================================

    Function HSL(ByVal Hue As Integer, ByVal Sat As Integer, ByVal Lum As Integer)
    Do While Hue > 359: Hue = Hue - 360: Loop
    Do While Hue < 0: Hue = Hue + 360: Loop
    If Sat > 100 Or Sat < 0 Then Exit Function
    If Lum > 100 Or Lum < 0 Then Exit Function
    Dim R As Integer, G As Integer, B As Integer
    If Hue <= 60 Then
    R = 255
    G = CInt(Hue / 60 * 256)
    B = 0
    ElseIf Hue > 60 And Hue <= 120 Then
    R = CInt((1 - (Hue - 60) / 60) * 256)
    G = 255
    B = 0
    ElseIf Hue > 120 And Hue <= 180 Then
    R = 0
    G = 255
    B = CInt((Hue - 120) / 60 * 256)
    ElseIf Hue > 180 And Hue <= 240 Then
    R = 0
    G = CInt((1 - (Hue - 180) / 60) * 256)
    B = 255
    ElseIf Hue > 240 And Hue <= 300 Then
    R = CInt((Hue - 240) / 60 * 256)
    G = 0
    B = 255
    ElseIf Hue > 300 Then
    R = 255
    G = 0
    B = CInt((1 - (Hue - 300) / 60) * 256)
    End If
    R = R + (50 - Sat) / 100 * (128 - R)
    G = G + (50 - Sat) / 100 * (128 - G)
    B = B + (50 - Sat) / 100 * (128 - B)
    If Lum < 50 Then
    R = (R / 50) * Lum
    G = (G / 50) * Lum
    B = (B / 50) * Lum
    Else
    R = Lum * ((255 - R) / 50) + 2 * R - 255
    G = Lum * ((255 - G) / 50) + 2 * G - 255
    B = Lum * ((255 - B) / 50) + 2 * B - 255
    End If
    If R > 255 Then R = 255
    If G > 255 Then G = 255
    If B > 255 Then B = 255
    If R < 0 Then R = 0
    If G < 0 Then G = 0
    If B < 0 Then B = 0
    HSL = RGB(R, G, B)
    End Function

    Sub RGB_To_HSL(R As Single, G As Single, B As Single)
    'error checking:
    If R < 0 Or R > 255 Or G < 0 Or G > 255 Or B < 0 Or B > 255 Then
    'found an error, insert error handling code
    H = 0: S = 0: L = 0
    Exit Sub
    End If
    'syntax is valid, proceed
    Dim Delta, CMin, CMax As Single 'declare floating-point variables
    R = R / 255 'scale color channels
    G = G / 255 'to values between
    B = B / 255 'one and zero
    CMax = R: CMin = R 'assume that read is highest and lowest
    If CMax < G Then CMax = G: If CMin > G Then CMin = G 'if green is highest or lowest, replace with green value
    If CMax < B Then CMax = B: If CMin > B Then CMin = B 'if blue is highest or lowest, replace with blue value
    L = (CMax + CMin) / 2 'determine luminance (average of extremities)
    If CMax = CMin Then 'color is greyscale, and has
    S = 0 'no saturation and
    H = 0 'unknown hue
    Else
    Delta = CMax - CMin 'range of colour values
    If L < 0.5 Then 'determine saturation depending on luminance and
    S = Delta / (CMax + CMin)
    Else
    S = Delta / (2 - CMax - CMin)
    End If
    Select Case CMax 'determine which color is most predominant
    Case R: H = (G - B) / Delta 'hue is between 0 and 119
    Case G: H = 2 + (B - R) / Delta 'hue is between 120 and 239
    Case Else: H = 4 + (R - G) / Delta 'hue is between 240 and 359
    End Select
    H = H / 6 'scale hue back to decimal value
    If H < 0 Then H = H + 1 'ensure that hue is positive
    End If
    H = CInt(H * 360) 'scale hue to degrees on color wheel
    L = CInt(L * 100) 'scale luminance to percentage
    S = CInt(S * 100) 'scale saturation to percentage
    End Sub


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