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
Comments