Page 1 of 1

Color Detector Rate Topic: -----

#1 rusoaica  Icon User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 217
  • View blog
  • Posts: 686
  • Joined: 10-March 12

Posted 16 October 2014 - 03:31 PM

Color Detector

In this tutorial i will show you how to create a tool that is very useful for graphicians, designers, web masters, programmers, etc. I will show you how to create a program that can detect the color details of the pixel currently pointed at by the mouse cursor.
When a graphician artist works with colors, he is mainly interested in three things: HEX value, RGB value and, eventually, the color name. For this, we will create a form named Detector, on which we will add:

  • 6 TextBoxes
  • 3 GroupBoxes
  • 7 Labels
  • 2 Buttons
  • 1 Timer

Their position, text and names is indicated in the picture:

Posted Image

The timer is named Checker, its Interval is set to 10 milliseconds and its Enabled property is set to False. All the TextBoxes are ReadOnly, and the TextBox named ShowColor is MultiLine.

Lets switch to code. First, add a new Class file to your Project and name it Colors. We use it to store the color names of the known colors. Inside it, write this code:

Public Class Colors
    Public Property _Name As String
    Public Property _HEX As String

    Public Sub New(ByVal HEX As String, ByVal Name As String)
        _Name = Name
        _HEX = HEX
    End Sub
End Class

So, what we did was to create a class that has two properties: the hexadecimal value of the color and its name.
Now, switch to the Detector form and type this code:

Imports System.Runtime.InteropServices

Public Class Detector
    <DllImport("user32.dll")> _
    Private Shared Function GetDC(hwnd As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function ReleaseDC(hwnd As IntPtr, hdc As IntPtr) As Int32
    End Function

    <DllImport("gdi32.dll")> _
    Private Shared Function GetPixel(hdc As IntPtr, nXPos As Integer, nYPos As Integer) As UInteger
    End Function

    Dim KnownColors As New List(Of Colors)
    Dim Paused As Boolean

    Private Sub Detector_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KnownColors.Add(New Colors("#F0F8FF", "Alice Blue")) 'using X11 Color Scheme
        KnownColors.Add(New Colors("#FAEBD7", "Antique White"))
        KnownColors.Add(New Colors("#00FFFF", "Aqua"))
        KnownColors.Add(New Colors("#7FFFD4", "Aquamarine"))
        KnownColors.Add(New Colors("#F0FFFF", "Azure"))
        KnownColors.Add(New Colors("#F5F5DC", "Beige"))
        KnownColors.Add(New Colors("#FFE4C4", "Biscue"))
        KnownColors.Add(New Colors("#000000", "Black"))
        KnownColors.Add(New Colors("#FFEBCD", "Blanchet Almond"))
        KnownColors.Add(New Colors("#0000FF", "Blue"))
        KnownColors.Add(New Colors("#8A2BE2", "Blue Violet"))
        KnownColors.Add(New Colors("#A52A2A", "Brown"))
        KnownColors.Add(New Colors("#DEB887", "Burlywood"))
        KnownColors.Add(New Colors("#5F9EA0", "Cadet Blue"))
        KnownColors.Add(New Colors("#7FFF00", "Chartreuse"))
        KnownColors.Add(New Colors("#D2691E", "Chocolate"))
        KnownColors.Add(New Colors("#FF7F50", "Coral"))
        KnownColors.Add(New Colors("#6495ED", "Cornflower"))
        KnownColors.Add(New Colors("#FFF8DC", "Cornsilk"))
        KnownColors.Add(New Colors("#DC143C", "Crimson"))
        KnownColors.Add(New Colors("#00FFFF", "Cian"))
        KnownColors.Add(New Colors("#00008B", "Dark Blue"))
        KnownColors.Add(New Colors("#008B8B", "Dark Cian"))
        KnownColors.Add(New Colors("#B8860B", "Dark Goldenrod"))
        KnownColors.Add(New Colors("#A9A9A9", "Dark Gray"))
        KnownColors.Add(New Colors("#006400", "Dark Green"))
        KnownColors.Add(New Colors("#BDB76B", "Dark Khaki"))
        KnownColors.Add(New Colors("#8B008B", "Dark Magenta"))
        KnownColors.Add(New Colors("#556B2F", "Dark Olive Green"))
        KnownColors.Add(New Colors("#FF8C00", "Dark Orange"))
        KnownColors.Add(New Colors("#9932CC", "Dark Orchild"))
        KnownColors.Add(New Colors("#8B0000", "Dark Red"))
        KnownColors.Add(New Colors("#E9967A", "Dark Salmon"))
        KnownColors.Add(New Colors("#8FBC8F", "Dark Sea Green"))
        KnownColors.Add(New Colors("#483D8B", "Dark Slate Blue"))
        KnownColors.Add(New Colors("#2F4F4F", "Dark Slate Gray"))
        KnownColors.Add(New Colors("#00CED1", "Dark Turquoise"))
        KnownColors.Add(New Colors("#9400D3", "Dark Violet"))
        KnownColors.Add(New Colors("#FF1493", "Deep Pink"))
        KnownColors.Add(New Colors("#00BFFF", "Deep Sky Blue"))
        KnownColors.Add(New Colors("#696969", "Dim Gray"))
        KnownColors.Add(New Colors("#1E90FF", "Dodger Blue"))
        KnownColors.Add(New Colors("#B22222", "Firebrick"))
        KnownColors.Add(New Colors("#FFFAF0", "Floral White"))
        KnownColors.Add(New Colors("#228B22", "Forest Green"))
        KnownColors.Add(New Colors("#FF00FF", "Fuchsia"))
        KnownColors.Add(New Colors("#DCDCDC", "Gainsboro"))
        KnownColors.Add(New Colors("#F8F8FF", "Ghost White"))
        KnownColors.Add(New Colors("#FFD700", "Gold"))
        KnownColors.Add(New Colors("#DAA520", "Goldenrod"))
        KnownColors.Add(New Colors("#BEBEBE", "Gray"))
        KnownColors.Add(New Colors("#808080", "Web Gray"))
        KnownColors.Add(New Colors("#00FF00", "Green"))
        KnownColors.Add(New Colors("#008000", "Web Green"))
        KnownColors.Add(New Colors("#ADFF2F", "Green Yellow"))
        KnownColors.Add(New Colors("#F0FFF0", "Honeydew"))
        KnownColors.Add(New Colors("#FF69B4", "Hot Pink"))
        KnownColors.Add(New Colors("#CD5C5C", "Indian Red"))
        KnownColors.Add(New Colors("#4B0082", "Indigo"))
        KnownColors.Add(New Colors("#FFFFF0", "Ivory"))
        KnownColors.Add(New Colors("#F0E68C", "Khaki"))
        KnownColors.Add(New Colors("#E6E6FA", "Lavender"))
        KnownColors.Add(New Colors("#FFF0F5", "Lavender Blush"))
        KnownColors.Add(New Colors("#7CFC00", "Lawn Green"))
        KnownColors.Add(New Colors("#FFFACD", "Lemon Chiffon"))
        KnownColors.Add(New Colors("#ADD8E6", "Light Blue"))
        KnownColors.Add(New Colors("#F08080", "Light Coral"))
        KnownColors.Add(New Colors("#E0FFFF", "Light Cyan"))
        KnownColors.Add(New Colors("#FAFAD2", "Light Goldenrod"))
        KnownColors.Add(New Colors("#D3D3D3", "Light Gray"))
        KnownColors.Add(New Colors("#90EE90", "Light Green"))
        KnownColors.Add(New Colors("#FFB6C1", "Light Pink"))
        KnownColors.Add(New Colors("#FFA07A", "Light Salmon"))
        KnownColors.Add(New Colors("#20B2AA", "Light Sea Green"))
        KnownColors.Add(New Colors("#87CEFA", "Light Sky Blue"))
        KnownColors.Add(New Colors("#778899", "Light Slate Gray"))
        KnownColors.Add(New Colors("#B0C4DE", "Light Steel Blue"))
        KnownColors.Add(New Colors("#FFFFE0", "Light Yellow"))
        KnownColors.Add(New Colors("#00FF00", "Lime"))
        KnownColors.Add(New Colors("#32CD32", "Lime Green"))
        KnownColors.Add(New Colors("#FAF0E6", "Limen"))
        KnownColors.Add(New Colors("#FF00FF", "Magenta"))
        KnownColors.Add(New Colors("#B03060", "Maroon"))
        KnownColors.Add(New Colors("#7F0000", "Web Maroon"))
        KnownColors.Add(New Colors("#66CDAA", "Medium Aquamarine"))
        KnownColors.Add(New Colors("#0000CD", "Medium Blue"))
        KnownColors.Add(New Colors("#BA55D3", "Medium Orchid"))
        KnownColors.Add(New Colors("#9370DB", "Medium Purple"))
        KnownColors.Add(New Colors("#3CB371", "Medium Sea Green"))
        KnownColors.Add(New Colors("#7B68EE", "Medium Slate Blue"))
        KnownColors.Add(New Colors("#00FA9A", "Medium Spring Green"))
        KnownColors.Add(New Colors("#48D1CC", "Medium Turquoise"))
        KnownColors.Add(New Colors("#C71585", "Medium Violet Red"))
        KnownColors.Add(New Colors("#191970", "Midnight Blue"))
        KnownColors.Add(New Colors("#F5FFFA", "Mint Cream"))
        KnownColors.Add(New Colors("#FFE4E1", "Misty Rose"))
        KnownColors.Add(New Colors("#FFE4B5", "Moccasin"))
        KnownColors.Add(New Colors("#FFDEAD", "Navajo White"))
        KnownColors.Add(New Colors("#000080", "Navy Blue"))
        KnownColors.Add(New Colors("#FDF5E6", "Old Lace"))
        KnownColors.Add(New Colors("#808000", "Olive"))
        KnownColors.Add(New Colors("#6B8E23", "Olive Drab"))
        KnownColors.Add(New Colors("#FFA500", "Orange"))
        KnownColors.Add(New Colors("#FF4500", "Orange Red"))
        KnownColors.Add(New Colors("#DA70D6", "Orchid"))
        KnownColors.Add(New Colors("#EEE8AA", "Pale Goldenrod"))
        KnownColors.Add(New Colors("#98FB98", "Pale Green"))
        KnownColors.Add(New Colors("#AFEEEE", "Pale Turquoise"))
        KnownColors.Add(New Colors("#DB7093", "Pale Violet Red"))
        KnownColors.Add(New Colors("#FFEFD5", "Papaya Whip"))
        KnownColors.Add(New Colors("#FFDAB9", "Peach Puff"))
        KnownColors.Add(New Colors("#CD853F", "Peru"))
        KnownColors.Add(New Colors("#FFC0CB", "Pink"))
        KnownColors.Add(New Colors("#DDA0DD", "Plum"))
        KnownColors.Add(New Colors("#B0E0E6", "Powder Blue"))
        KnownColors.Add(New Colors("#A020F0", "Purple"))
        KnownColors.Add(New Colors("#7F007F", "Web Purple"))
        KnownColors.Add(New Colors("#663399", "Rebecca Purple"))
        KnownColors.Add(New Colors("#FF0000", "Red"))
        KnownColors.Add(New Colors("#BC8F8F", "Rosy Brown"))
        KnownColors.Add(New Colors("#4169E1", "Royal Blue"))
        KnownColors.Add(New Colors("#8B4513", "Saddle Brown"))
        KnownColors.Add(New Colors("#FA8072", "Salmon"))
        KnownColors.Add(New Colors("#F4A460", "Sandy Brown"))
        KnownColors.Add(New Colors("#2E8B57", "Sea Green"))
        KnownColors.Add(New Colors("#FFF5EE", "Seashell"))
        KnownColors.Add(New Colors("#A0522D", "Sienna"))
        KnownColors.Add(New Colors("#C0C0C0", "Silver"))
        KnownColors.Add(New Colors("#87CEEB", "Sky Blue"))
        KnownColors.Add(New Colors("#6A5ACD", "Slate Blue"))
        KnownColors.Add(New Colors("#708090", "Slate Gray"))
        KnownColors.Add(New Colors("#FFFAFA", "Snow"))
        KnownColors.Add(New Colors("#00FF7F", "Spring Green"))
        KnownColors.Add(New Colors("#4682B4", "Steel Blue"))
        KnownColors.Add(New Colors("#D2B48C", "Tan"))
        KnownColors.Add(New Colors("#008080", "Teal"))
        KnownColors.Add(New Colors("#D8BFD8", "Thistle"))
        KnownColors.Add(New Colors("#FF6347", "Tomato"))
        KnownColors.Add(New Colors("#40E0D0", "Turquoise"))
        KnownColors.Add(New Colors("#EE82EE", "Violet"))
        KnownColors.Add(New Colors("#F5DEB3", "Wheat"))
        KnownColors.Add(New Colors("#FFFFFF", "White"))
        KnownColors.Add(New Colors("#F5F5F5", "White Smoke"))
        KnownColors.Add(New Colors("#FFFF00", "Yellow"))
        KnownColors.Add(New Colors("#9ACD32", "Yellow Green"))
        Checker.Enabled = True
    End Sub

    Private Sub Checker_Tick(sender As Object, e As EventArgs) Handles Checker.Tick
        If Not Paused Then
            Dim CurrentColor As Color = GetPixelColor(Cursor.Position.X, Cursor.Position.Y)
            LocationX.Text = Cursor.Position.X
            LocationY.Text = Cursor.Position.Y
            R.Text = CurrentColor.R
            G.Text = CurrentColor.G
            B.Text = CurrentColor.B
            For i As Integer = 0 To KnownColors.Count - 1
                If ColorTranslator.ToHtml(CurrentColor) = KnownColors(i)._HEX Then
                    ColorName.Text = KnownColors(i)._Name
                    Exit For
                    ColorName.Text = "Not found"
                End If
            Hex.Text = ColorTranslator.ToHtml(CurrentColor)
            ShowColor.BackColor = CurrentColor
        End If
    End Sub

    Public Function GetPixelColor(x As Integer, y As Integer) As System.Drawing.Color
        Dim hdc As IntPtr = GetDC(IntPtr.Zero)
        Dim pixel As UInteger = GetPixel(hdc, x, y)
        ReleaseDC(IntPtr.Zero, hdc)
        Dim color__1 As Color = Color.FromArgb(CInt(pixel And &HFF), CInt(pixel And &HFF00) >> 8, CInt(pixel And &HFF0000) >> 16)
        Return color__1
    End Function

    Private Sub Detector_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F5 Then
            Paused = Not Paused
        ElseIf e.KeyCode = Keys.F6 Then
        ElseIf e.KeyCode = Keys.F7 Then
            Clipboard.SetText("RGB (" & R.Text & ", " & G.Text & ", " & B.Text & ")")
        End If
    End Sub

    Private Sub Help_Click(sender As Object, e As EventArgs) Handles Help.Click
        Checker.Enabled = False
        MessageBox.Show("Press F5 to freeze/unfreeze the current color" & vbNewLine & "Press F6 to copy the current HEX value" & vbNewLine & "Press F7 to copy the current RGB value")
        Checker.Enabled = True
    End Sub

    Private Sub Close_Click(sender As Object, e As EventArgs) Handles Close.Click
    End Sub
End Class

At this point, the program should work. Now, lets explain what it does:

First, we display the X and Y coordinates of the mouse cursor, so we know what pixel is being pointed at:

LocationX.Text = Cursor.Position.X
LocationY.Text = Cursor.Position.Y

Next, we need to display the RGB values of that pixel. To do this, we have created a function that will get the color of the pixel hoovered by the mouse cursor: GetPixelColor(x As Integer, y As Integer). This function uses a function named GetPixel, which lives inside gdi32.dll library and returns the RGB value of a pixel at the specified coordinates (in our case, the mouse cursor position). So, using this line:

Dim CurrentColor As Color = GetPixelColor(Cursor.Position.X, Cursor.Position.Y)

we create a color variable that stores the color under the cursor. After that, it's a simple job to display its RGB values:

R.Text = CurrentColor.R
G.Text = CurrentColor.G
B.Text = CurrentColor.B

Moving to the next point, we are displaying the color name, if the color is a known color. For this, in the Load event of our form, we create a list that will store all the known color names:

Dim KnownColors As New List(Of Colors)
KnownColors.Add(New Colors("#F0F8FF", "Alice Blue"))

Notice that we have created a List of Colors objects (as pointed above, the Colors class has two properties: name and hexadecimal value). We will use it to iterate through its elements and search for the HEX value of the current color. If the HEX value is in the list, we get the color name and display it; if it doesn't exist, we simply display "Not Found":

            For i As Integer = 0 To KnownColors.Count - 1
                If ColorTranslator.ToHtml(CurrentColor) = KnownColors(i)._HEX Then
                    ColorName.Text = KnownColors(i)._Name
                    Exit For
                    ColorName.Text = "Not found"
                End If

Another feature we want to display is the hexadecimal code of the current color:

Hex.Text = ColorTranslator.ToHtml(CurrentColor)

Lastly, because the pixel under the mouse cursor is so small that we can't see it, we will display its color as the background color of a TextBox:

ShowColor.BackColor = CurrentColor

There are other three additional functions that we might find quite useful: the ability to freeze and unfreeze the current color, as well as a fast way of copying the current RGB or HEX values into clipboard. We do this using the functional keys F5, F6 and F7, and we put the code in the KeyDown event of our form:

        If e.KeyCode = Keys.F5 Then
            Paused = Not Paused
        ElseIf e.KeyCode = Keys.F6 Then
        ElseIf e.KeyCode = Keys.F7 Then
            Clipboard.SetText("RGB (" & R.Text & ", " & G.Text & ", " & B.Text & ")")
        End If

It's pretty obvious that when we press the F6 and F7 keys while the form has focus, we are copying into clipboard the RGB or HEX values. But what about the F5 key?
We have declared a class-level variable named Paused, and when we press the F5 key we toggle its value between True and False. This is helpful because we have this code in our Timer Tick event:

If Not Paused Then

So, the informations of the colors will only be updated if the Paused variable is set to False. This way we can quickly "lock" the current values of a color hoovered by the cursor.

As homework assignment, i'll leave it to you to add a CheckBox that will be checked when the current color is a "Web Safe" color, and unchecked when it's not ;)/>

Is This A Good Question/Topic? 1
  • +

Page 1 of 1