Library code snippets

Converting RGB to HEX colour codes

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

  1. 03 Dec 2005 at 02:30
    Thanks for sharing your code. Just made it more portable:

    Code:

    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.
  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Converting RGB to HEX colour codes.

Leave a comment

Sign in or Join us (it's free).

Ruan Meyer I have been working for about 5 years in the web design arena, my main area of interest is and will always be 3D animation and Environment Design, my website is almost up and running, and i'm going...
AddThis

Related discussion

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...

Want to stay in touch with what's going on? Follow us on twitter!