Click here to Skip to main content
15,885,365 members
Articles / Multimedia / GDI+

Simple Web and RGB Color Picker Utility

Rate me:
Please Sign up or sign in to vote.
3.70/5 (10 votes)
11 Oct 20065 min read 52.4K   584   19  
The article addresses the construction of a simple utility application useful for selecting an RGB or web color.
Public Class frmColorPicker

#Region "Declarations"

    Private mRed As Integer
    Private mGreen As Integer
    Private mBlue As Integer

#End Region


    Private Sub frmColorPicker_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        mRed = 0
        mGreen = 0
        mBlue = 0
        pnlColor.BackColor = System.Drawing.Color.FromArgb(mRed, mGreen, mBlue)
        txtWebColor.Text = GetWebColor()
        txtRGB.Text = "RGB(" & mRed & ", " & mGreen & ", " & mBlue & ")"

    End Sub


    Private Sub tbarRed_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbarRed.Scroll

        txtRed.Text = tbarRed.Value
        mRed = tbarRed.Value
        pnlColor.BackColor = System.Drawing.Color.FromArgb(mRed, mGreen, mBlue)
        txtWebColor.Text = GetWebColor()
        txtRGB.Text = "RGB(" & mRed & ", " & mGreen & ", " & mBlue & ")"

    End Sub



    Private Sub tbarGreen_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbarGreen.Scroll

        txtGreen.Text = tbarGreen.Value
        mGreen = tbarGreen.Value
        pnlColor.BackColor = System.Drawing.Color.FromArgb(mRed, mGreen, mBlue)
        txtWebColor.Text = GetWebColor()
        txtRGB.Text = "RGB(" & mRed & ", " & mGreen & ", " & mBlue & ")"

    End Sub



    Private Sub tbarBlue_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbarBlue.Scroll

        txtBlue.Text = tbarBlue.Value
        mBlue = tbarBlue.Value
        pnlColor.BackColor = System.Drawing.Color.FromArgb(mRed, mGreen, mBlue)
        txtWebColor.Text = GetWebColor()
        txtRGB.Text = "RGB(" & mRed & ", " & mGreen & ", " & mBlue & ")"

    End Sub



    Private Function GetWebColor() As String

        Dim clr As Color
        clr = Color.FromArgb(mRed, mGreen, mBlue)

        Dim wc As String = ColorTranslator.ToHtml(clr).ToString()
        Return wc

    End Function



    Private Sub txtRed_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRed.KeyPress

        If Char.IsNumber(e.KeyChar) Or e.KeyChar = ControlChars.Back Then
            'ok, its a number so let it go
        Else
            e.KeyChar = Nothing
        End If
    End Sub



    Private Sub txtRed_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRed.TextChanged

        Try
            If (Convert.ToInt32(txtRed.Text) >= 0 And Convert.ToInt32(txtRed.Text) <= 255) Then
                tbarRed.Value = Convert.ToInt32(txtRed.Text)
                mRed = tbarRed.Value
                pnlColor.BackColor = System.Drawing.Color.FromArgb(mRed, mGreen, mBlue)
                txtWebColor.Text = GetWebColor()
                txtRGB.Text = "RGB(" & mRed & ", " & mGreen & ", " & mBlue & ")"
            Else
                ' input last valid number
                txtRed.Text = mRed.ToString()
            End If
        Catch ex As Exception
            'do nothing
        End Try

    End Sub



    Private Sub txtGreen_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtGreen.KeyPress

        If Char.IsNumber(e.KeyChar) Or e.KeyChar = ControlChars.Back Then
            'ok, its a number so let it go
        Else
            e.KeyChar = Nothing
        End If

    End Sub



    Private Sub txtGreen_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtGreen.TextChanged

        Try
            If (Convert.ToInt32(txtGreen.Text) >= 0 And Convert.ToInt32(txtGreen.Text) <= 255) Then
                tbarGreen.Value = Convert.ToInt32(txtGreen.Text)
                mGreen = tbarGreen.Value
                pnlColor.BackColor = System.Drawing.Color.FromArgb(mRed, mGreen, mBlue)
                txtWebColor.Text = GetWebColor()
                txtRGB.Text = "RGB(" & mRed & ", " & mGreen & ", " & mBlue & ")"
            Else
                ' input last valid number
                txtGreen.Text = mGreen.ToString()
            End If
        Catch ex As Exception
            'do nothing
        End Try
    End Sub



    Private Sub txtBlue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBlue.KeyPress

        If Char.IsNumber(e.KeyChar) Or e.KeyChar = ControlChars.Back Then
            'ok, its a number so let it go
        Else
            e.KeyChar = Nothing
        End If

    End Sub



    Private Sub txtBlue_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBlue.TextChanged

        Try
            If (Convert.ToInt32(txtBlue.Text) >= 0 And Convert.ToInt32(txtBlue.Text) <= 255) Then
                tbarBlue.Value = Convert.ToInt32(txtBlue.Text)
                mBlue = tbarBlue.Value
                pnlColor.BackColor = System.Drawing.Color.FromArgb(mRed, mGreen, mBlue)
                txtWebColor.Text = GetWebColor()
                txtRGB.Text = "RGB(" & mRed & ", " & mGreen & ", " & mBlue & ")"
            Else
                ' input last valid number
                txtBlue.Text = mBlue.ToString()
            End If
        Catch ex As Exception
            'do nothing
        End Try

    End Sub



    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Application.Exit()

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions