Hello,

I'm trying to create a function that will convert RGB (red, green, blue) colour values into HSV(hue, saturation, value) and HSL(hue, saturation, lightness) values. RGB, HSV and HSL all consist of 3 cells of numerical values.

I'm very new to programming and struggling. Although I have found VB codes that claim to perform the functions, when I create functions from them in Excel(2010) I get an error, typically a 'compile error' saying User-defined type not defined. I think this may be because they are not now compatible with VBA 7, because I'm just not inputting my variables correctly, or very possibly a combination of both.

If this is easy for someone to understand you would be really helping me out by showing me where I'm going wrong.

Thank you in advance.

RGB to HSL:

' From RGB to HSL

Function RGBtoHSL(RGB As Long) As HSL

Dim R As Double ' Range 0 - 1
Dim G As Double ' Range 0 - 1
Dim B As Double ' Range 0 - 1

Dim RGB_Max As Double
Dim RGB_Min As Double
Dim RGB_Diff As Double

Dim HexString As String

HexString = Right$(String$(7, "0") & Hex$(RGB), 8)
R = CDbl("&H" & Mid$(HexString, 7, 2)) / 255
G = CDbl("&H" & Mid$(HexString, 5, 2)) / 255
B = CDbl("&H" & Mid$(HexString, 3, 2)) / 255

RGB_Max = R
If G > RGB_Max Then RGB_Max = G
If B > RGB_Max Then RGB_Max = B

RGB_Min = R
If G < RGB_Min Then RGB_Min = G
If B < RGB_Min Then RGB_Min = B

RGB_Diff = RGB_Max - RGB_Min

With RGBtoHSL

.L = (RGB_Max + RGB_Min) / 2

If RGB_Diff = 0 Then

.S = 0
.H = 0

Else

Select Case RGB_Max
Case R: .H = (1 / 6) * (G - B) / RGB_Diff - (B > G)
Case G: .H = (1 / 6) * (B - R) / RGB_Diff + (1 / 3)
Case B: .H = (1 / 6) * (R - G) / RGB_Diff + (2 / 3)
End Select

Select Case .L
Case Is < 0.5: .S = RGB_Diff / (2 * .L)
Case Else: .S = RGB_Diff / (2 - (2 * .L))
End Select

End If

End With

End Function

reference http://www.wordarticles.com/Articles...olourSpace.php

Here is an alternate website with code that performs the same function: http://www.xbeat.net/vbspeed/c_RGBToHSL.htm

Now for RGB to HSV::

'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

reference http://www.xtremevbtalk.com/showthread.php?t=302304