Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / GDI+

Better ColorPicker

2.90/5 (19 votes)
6 Oct 2005CPOL 1   149  
An article on creating a better color picker.

Sample Image - ColorPicker.gif

Introduction

This is a better version of the ColorPicker included with .NET 1.1. It has more flexibility, more colors...

Using the code

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _
   As System.EventArgs) Handles Button1.Click
    Me.BackColor = ShowColorPicker(Me.BackColor)
End Sub

Points of Interest

This is an interesting combination of different technologies, for example, drawing (used to draw the palette), mouse-events and coordinates...

The system to generate the 216 Web-save colors:

VB
'Generate the colors
Function palette_colors_load()
    Dim RGB, R, G, B As String
    For i As Integer = 1 To 18
        For i2 As Integer = 1 To 12
            Select Case i
                Case 1, 12, 13
                    R = "FF"
                Case 2, 11, 14
                    R = "CC"
                Case 3, 10, 15
                    R = "99"
                Case 4, 9, 16
                    R = "66"
                Case 5, 8, 17
                    R = "33"
                Case 6, 7, 18
                    R = "00"
            End Select
            Select Case i2
                Case 6, 7
                    G = "FF"
                Case 5, 8
                    G = "CC"
                Case 4, 9
                    G = "99"
                Case 3, 10
                    G = "66"
                Case 2, 11
                    G = "33"
                Case 1, 12
                    G = "00"
            End Select
            If i2 <= 6 Then
                Select Case i
                    Case Is <= 6
                        B = "FF"
                    Case Is <= 12
                        B = "CC"
                    Case Is <= 18
                        B = "99"
                End Select
            Else
                Select Case i
                    Case Is <= 6
                        B = "66"
                    Case Is <= 12
                        B = "33"
                    Case Is <= 18
                        B = "00"
                End Select
            End If
            RGB = "#" & R & G & B
            palette_colors(((i - 1) * 12) + i2)._name = RGB
            palette_colors(((i - 1) * 12) + i2)._color = _
                ColorTranslator.FromHtml(RGB)
            palette_colors(((i - 1) * 12) + i2)._x = i2
            palette_colors(((i - 1) * 12) + i2)._y = i
        Next
    Next
End Function

System for the manual input of colors:

VB
Private Sub txtRed_KeyPress(ByVal sender As Object, ByVal e _
   As System.Windows.Forms.KeyPressEventArgs) Handles txtRed.KeyPress
    If Not (IsNumeric(e.KeyChar.ToString) Or Asc(e.KeyChar) = 8) _
       Then e.Handled = True
    'asc(8) = backspace
End Sub

Private Sub txtGreen_KeyPress(ByVal sender As Object, ByVal e _
   As System.Windows.Forms.KeyPressEventArgs) Handles txtGreen.KeyPress
    If Not (IsNumeric(e.KeyChar.ToString) Or Asc(e.KeyChar) = 8) _
       Then e.Handled = True
    'asc(8) = backspace
End Sub

Private Sub txtBlue_KeyPress(ByVal sender As Object, ByVal e _
   As System.Windows.Forms.KeyPressEventArgs) Handles txtBlue.KeyPress
    If Not (IsNumeric(e.KeyChar.ToString) Or Asc(e.KeyChar) = 8) _
       Then e.Handled = True
    'asc(8) = backspace
End Sub

Private Sub txtRed_TextChanged(ByVal sender As Object, ByVal e _
   As System.EventArgs) Handles txtRed.TextChanged
    If CInt(txtRed.Text) > 255 Then
        MsgBox("Fill in a value between 0 and 255", _
           MsgBoxStyle.Information)
        txtRed.Text = trbRed.Value.ToString
    Else
        trbRed.Value = CInt(txtRed.Text)
    End If
End Sub

Private Sub txtGreen_TextChanged(ByVal sender As Object, ByVal e _
   As System.EventArgs) Handles txtGreen.TextChanged
    If CInt(txtGreen.Text) > 255 Then
        MsgBox("Fill in a value between 0 and 255", _
           MsgBoxStyle.Information)
        txtGreen.Text = trbGreen.Value.ToString
    Else
        trbGreen.Value = CInt(txtGreen.Text)
    End If
End Sub

Private Sub txtBlue_TextChanged(ByVal sender As Object, ByVal e _
   As System.EventArgs) Handles txtBlue.TextChanged
    If CInt(txtBlue.Text) > 255 Then
        MsgBox("Fill in a value between 0 and 255", _
           MsgBoxStyle.Information)
        txtBlue.Text = trbBlue.Value.ToString
    Else
        trbBlue.Value = CInt(txtBlue.Text)
    End If
End Sub

History

  • 29-09-2005: First release.
  • 06-10-2005: Second release.
    • Added: HBS
    • Added: Changeable RGB values

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)