Need some guide on how to convert HSV to RGB

Asked By 30 points N/A Posted on -
qa-featured

I am working on a live wallpaper using Visual Basic wherein the colors change slowly in the image and go back to the original.

To do this, I need to convert HSV to RGB, but I'm having difficulty with computation on the reverse process.

I am trying to understand the explanation on Wikipedia but I need someone to better explain the process to me.

Can someone explain how to properly convert HSV color to RGB in VBA, and can I use Excel to do the computation?

SHARE
Answered By 0 points N/A #196288

Need some guide on how to convert HSV to RGB

qa-featured

Below is the code for converting to and from HSV from RGB:–

 

Private Sub HSVtoRGB(ByRef Red, ByRef Green, ByRef Blue, Hue, Sat, Value)

    Dim i As Long

    Dim f As Double, p As Double, q As Double, t As Double

   

    If Sat = 0 Then

        Red = Value

        Green = Value

        Blue = Value

        Exit Sub

    End If

   

    i = Hue 60

    Hue = Hue / 60

    f = Hue – i

    p = Value * (1 – Sat)

    q = Value * (1 – Sat * f)

    t = Value * (1 – Sat * (1 – f))

   

    Select Case i

        Case 0

            Red = Value

            Green = t

            Blue = p

        Case 1

            Red = q

            Green = Value

            Blue = p

        Case 2

            Red = p

            Green = Value

            Blue = t

        Case 3

            Red = p

            Green = q

            Blue = Value

        Case 4

            Red = t

            Green = p

            Blue = Value

        Case 5

            Red = Value

            Green = p

            Blue = q

    End Select

   

End Sub

 

'Function RGBtoHSV(Red, Green, Blue, ByRef Hue, ByRef Sat, ByRef Value)

'    Dim Temp As Double

'    Dim xa As Double

'    Dim ya As Double

'

'    Temp = (Red + Green + Blue) / 3

'    xa = (Green – Red) / Sqr(2)

'    ya = (Blue + Blue – Red – Green) / Sqr(6)

'    Hue = Arg(xa, ya) * 180 / pi + 150

'    Sat = Arg(Temp, Module(Red – Temp, Green – Temp, Blue – Temp)) * 100 / Atn(Sqr(6))

'    Value = Temp / 2.55

'

'    If Sat = 0 Or Value = 0 Then Hue = 0

'    If Hue < 0 Then Hue = Hue + 360

'    If Hue >= 360 Then Hue = Hue – 360

'End Function

 

Sub RGBtoHSV(Red, Green, Blue, ByRef Hue, ByRef Sat, ByRef Value)

    Dim min As Double, max As Double, delta As Double

    If Red <= Green And Red <= Blue Then min = Red

    If Green <= Red And Green <= Blue Then min = Green

    If Blue <= Red And Blue <= Green Then min = Blue

   

    If Red >= Green And Red >= Blue Then max = Red

    If Green >= Red And Green >= Blue Then max = Green

    If Blue >= Red And Blue >= Green Then max = Blue

   

    Value = max

    delta = max – min

   

    If Not delta = 0 Then

        Sat = delta / max

    Else

        Sat = 0

        Hue = 0

        Exit Sub

    End If

   

    If Red = max Then

        Hue = (Green – Blue) / delta

    ElseIf Green = max Then

        Hue = 2 + (Blue – Red)

    Else

        Hue = 4 + (Red – Green) / delta

    End If

    Hue = Hue * 60

    If Hue < 0 Then Hue = Hue + 360

End Sub

 

Private Sub UnRGB(ByVal Color As OLE_COLOR, ByRef r, ByRef g, ByRef b)

r = Color And (Not &HFFFFFF00)

g = (Color And (Not &HFFFF00FF)) &H100&

b = (Color And (Not &HFF00FFFF)) &HFFFF&

End Sub

 

Function Module(X, Y, z)

' Returns the module of a 3D vector

Module = Sqr(X * X + Y * Y + z * z)

End Function

 

Function Arg(xa, ya)

'Returns the argument (in radians) of a point in the (x,y) plan

 

If xa = 0 And ya = 0 Then Arg = 0: Exit Function

If xa = 0 And ya >= 0 Then Arg = pi / 2: Exit Function

If ya = 0 And xa < 0 Then Arg = pi: Exit Function

If xa = 0 And ya < 0 Then Arg = -pi / 2: Exit Function

 

If xa > 0 Then Arg = Atn(ya / xa): Exit Function

 

If xa < 0 And ya >= 0 Then Arg = pi – Atn(-ya / xa)

If xa < 0 And ya < 0 Then Arg = -pi + Atn(-ya / -xa)

 

End Function

 

Best of Luck

Suominen Ross

Related Questions