Click here to Skip to main content
Licence CPOL
First Posted 29 Sep 2005
Views 23,039
Bookmarked 15 times

Better ColorPicker

By | 6 Oct 2005 | Article
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

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:

'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:

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)

About the Author

Wouter Devinck

Web Developer

Belgium Belgium

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 6 Oct 2005
Article Copyright 2005 by Wouter Devinck
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid