Code:
'---------------------------------------------------------------------------------------
' Procedure : HlsToRgb
' DateTime : 11/02/2005 11:58
' Author : lee dale
' Purpose : Take HLS values and return corresponding RGB values as ByRef variables
'---------------------------------------------------------------------------------------
'
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
Private Sub txtSaturation_Change()
On Error GoTo txtSaturation_Change_Error
If frmConverter.ActiveControl = txtSaturation Then
If Not txtSaturation = "" Then
If IsNumeric(txtSaturation.Text) Then
If CInt(txtSaturation.Text) <= 1 And CInt(txtSaturation.Text) >= 0 Then
Call ChangeColour(True)
Else
If CInt(txtLight.Text) > 1 Then
txtSaturation.Text = 1
End If
If CInt(txtSaturation.Text) < 0 Then
txtSaturation.Text = 0
End If
End If
End If
End If
End If
On Error GoTo 0
Exit Sub
txtSaturation_Change_Error:
Select Case Err.Number
Case Else
Call ErrorHandler
End Select
End Sub
'---------------------------------------------------------------------------------------
' Procedure : RGBtoVBHex
' DateTime : 11/02/2005 15:48
' Author : lee.dale
' Purpose : Converts supplied RGB values to corresponding vb hex string
'---------------------------------------------------------------------------------------
'
Private Function RGBtoVBHex(ByVal iRed As Integer, ByVal iGreen As Integer, ByVal iBlue As Integer) As String
On Error GoTo RGBtoVBHex_Error
RGBtoVBHex = "&H00" & Hex$(iBlue) & Hex$(iGreen) & Hex$(iRed) & "&"
On Error GoTo 0
Exit Function
RGBtoVBHex_Error:
Select Case Err.Number
Case Else
Call ErrorHandler
End Select
End Function
'---------------------------------------------------------------------------------------
' Procedure : HEXCOL2RGB
' DateTime : 11/02/2005 16:23
' Author : lee.dale
' Purpose :
'---------------------------------------------------------------------------------------
'
Private Function HEXCOL2RGB(ByVal HexColor As String) As String
'The input at this point could be HexColor = "#00FF1F"
Dim Red As String
Dim Green As String
Dim Blue As String
Dim Color As String
On Error GoTo HEXCOL2RGB_Error
Color = Replace(HexColor, "#", "")
'Here HexColor = "00FF1F"
Red = Val("&H" & Mid(Color, 1, 2))
'The red value is now the long version of "00"
Green = Val("&H" & Mid(Color, 3, 2))
'The red value is now the long version of "FF"
Blue = Val("&H" & Mid(Color, 5, 2))
'The red value is now the long version of "1F"
HEXCOL2RGB = CStr(Red) & CStr(Green) & CStr(Blue)
'The output is an RGB value
On Error GoTo 0
Exit Function
HEXCOL2RGB_Error:
Select Case Err.Number
Case Else
Call ErrorHandler
End Select
End Function
Private Sub txtVBHex_Change()
Dim sRGBValue As String
On Error GoTo txtVBHex_Change_Error
If frmConverter.ActiveControl = txtVBHex Then
If Left(txtVBHex, 1) = "&" Then
sRGBValue = VBHextoRGB(txtVBHex)
txtRed.Text = Left(sRGBValue, 3)
txtGreen.Text = Mid(sRGBValue, 4, 3)
txtBlue.Text = Right(sRGBValue, 3)
Call ChangeColour(False)
End If
End If
On Error GoTo 0
Exit Sub
txtVBHex_Change_Error:
Select Case Err.Number
Case Else
Call ErrorHandler
End Select
End Sub
'---------------------------------------------------------------------------------------
' Procedure : VBHextoRGB
' DateTime : 11/02/2005 16:40
' Author : lee.dale
' Purpose : Convert Supplied vb hex value to RGB string
'---------------------------------------------------------------------------------------
'
Private Function VBHextoRGB(ByVal sHexString As String) As String
Dim sColor As String
Dim iRed As Integer, iGreen As Integer, iBlue As Integer
On Error GoTo VBHextoRGB_Error
sColor = Mid(sHexString, 5, 6)
iRed = Val("&H" & Left(sColor, 2))
iGreen = Val("&H" & Mid(sColor, 3, 2))
iBlue = Val("&H" & Right(sColor, 2))
VBHextoRGB = Format(CStr(iRed), "000") & Format(CStr(iGreen), "000") & Format(CStr(iBlue), "000")
On Error GoTo 0
Exit Function
VBHextoRGB_Error:
Select Case Err.Number
Case Else
Call ErrorHandler
End Select
End Function