Library code snippets
Converting RGB to HEX colour codes
By Ruan Meyer, published on 28 Nov 2002
This is a simple color converter, that converts RGB colour codes to HEX. It uses textbox control to display the color, three text boxes for Red, Green and Blue values, also 3 sliders if the exact rgb color is not known. To use the code, you need is the folowing: 4 Textboxes (don't rename them) 3 sliders (numbered 0 to 255) and label4 is the hex output
Dim sR, sG, sB As String
Private Sub cmdClose_Click()
Unload Me
End
End Sub
Private Sub Form_Load()
sR = "FF"
sG = "FF"
sB = "FF"
Slider1.Value = 255
Slider2.Value = 255
Slider3.Value = 255
Text1.BackColor = RGB(255, 255, 255)
End Sub
Private Sub Slider1_Change()
Text1.BackColor = RGB(Slider1.Value, Slider2.Value, Slider3.Value)
hR = Hex(Slider1.Value)
If Len(hR) = 1 Then
sR = "0" & hR
Else
sR = hR
End If
WriteString
End Sub
Private Sub Slider2_Change()
Text1.BackColor = RGB(Slider1.Value, Slider2.Value, Slider3.Value)
hG = Hex(Slider2.Value)
If Len(hG) = 1 Then
sG = "0" & hG
Else
sG = hG
End If
WriteString
End Sub
Private Sub Slider3_Change()
Text1.BackColor = RGB(Slider1.Value, Slider2.Value, Slider3.Value)
hB = Hex(Slider3.Value)
If Len(hB) = 1 Then
sB = "0" & hB
Else
sB = hB
End If
WriteString
End Sub
Function WriteString()
Text2.Text = Slider1.Value
Text3.Text = Slider2.Value
Text4.Text = Slider3.Value
Label4.Caption = "#" & sR & sG & sB
End Function
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Text2.Text = "" Then
Text2.Text = "0"
ElseIf Not IsNumeric(Text2.Text) Then
Text2.Text = "255"
ElseIf Int(Text2.Text) > 255 Then
Text2.Text = "255"
End If
If Text3.Text = "" Then
Text3.Text = "0"
ElseIf Not IsNumeric(Text3.Text) Then
Text3.Text = "255"
ElseIf Int(Text3.Text) > 255 Then
Text3.Text = "255"
End If
If Text4.Text = "" Then
Text4.Text = "0"
ElseIf Not IsNumeric(Text4.Text) Then
Text4.Text = "255"
ElseIf Int(Text4.Text) > 255 Then
Text4.Text = "255"
End If
tR = Hex(Text2.Text)
tG = Hex(Text3.Text)
tB = Hex(Text4.Text)
Label4.Caption = "#" & tR & tG & tB
Text1.BackColor = RGB(Text2.Text, Text3.Text, Text4.Text)
Slider1.Value = Text2.Text
Slider2.Value = Text3.Text
Slider3.Value = Text4.Text
End If
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Text2.Text = "" Then
Text2.Text = "0"
ElseIf Not IsNumeric(Text2.Text) Then
Text2.Text = "255"
ElseIf Int(Text2.Text) > 255 Then
Text2.Text = "255"
End If
If Text3.Text = "" Then
Text3.Text = "0"
ElseIf Not IsNumeric(Text3.Text) Then
Text3.Text = "255"
ElseIf Int(Text3.Text) > 255 Then
Text3.Text = "255"
End If
If Text4.Text = "" Then
Text4.Text = "0"
ElseIf Not IsNumeric(Text4.Text) Then
Text4.Text = "255"
ElseIf Int(Text4.Text) > 255 Then
Text4.Text = "255"
End If
tR = Hex(Text2.Text)
tG = Hex(Text3.Text)
tB = Hex(Text4.Text)
Label4.Caption = "#" & tR & tG & tB
Text1.BackColor = RGB(Text2.Text, Text3.Text, Text4.Text)
Slider1.Value = Text2.Text
Slider2.Value = Text3.Text
Slider3.Value = Text4.Text
End If
End Sub
Private Sub Text4_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Text2.Text = "" Then
Text2.Text = "0"
ElseIf Not IsNumeric(Text2.Text) Then
Text2.Text = "255"
ElseIf Int(Text2.Text) > 255 Then
Text2.Text = "255"
End If
If Text3.Text = "" Then
Text3.Text = "0"
ElseIf Not IsNumeric(Text3.Text) Then
Text3.Text = "255"
ElseIf Int(Text3.Text) > 255 Then
Text3.Text = "255"
End If
If Text4.Text = "" Then
Text4.Text = "0"
ElseIf Not IsNumeric(Text4.Text) Then
Text4.Text = "255"
ElseIf Int(Text4.Text) > 255 Then
Text4.Text = "255"
End If
tR = Hex(Text2.Text)
tG = Hex(Text3.Text)
tB = Hex(Text4.Text)
Label4.Caption = "#" & tR & tG & tB
Text1.BackColor = RGB(Text2.Text, Text3.Text, Text4.Text)
Slider1.Value = Text2.Text
Slider2.Value = Text3.Text
Slider3.Value = Text4.Text
End If
End Sub
Related articles
Related discussion
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Public Module cColor
Public Function GetHexColor(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer)
Dim sR As String = "FF"
Dim sG As String = "FF"
Dim sB As String = "FF"
Dim hR As String = Microsoft.VisualBasic.Hex(R)
If hR.Length = 1 Then
sR = "0" & hR
Else
sR = hR
End If
Dim hG As String = Microsoft.VisualBasic.Hex(G)
If Len(hG) = 1 Then
sG = "0" & hG
Else
sG = hG
End If
Dim hB As String = Microsoft.VisualBasic.Hex(B)
If Len(hB) = 1 Then
sB = "0" & hB
Else
sB = hB
End If
Return "#" & sR & sG & sB
End Function
End Module
Cheers, McoreD.
This thread is for discussions of Converting RGB to HEX colour codes.