Introduction
In this article, you'll learn how a Color Code works and how you can make yours too. It is very useful to store small numbers and makes a very small file size. I've created the Color Code because I always wanted to create some sort of bar code. If you've always wanted to do so, this is your chance to!
Creating the creator
The things you'll need to create your generated and your decoder are the following:
- Microsoft Visual Studio (The year doesn't matter. I use 2010)
- .NET Framework
Let's start by creating a new project. You can name it whatever you want. Here are the form settings I use to make the program look good:
- Size of 438 x 165 Pixels
- StartPosition: CenterScreen
- MaximizeButton: False (Because we don't need it
- FormBorderStyle: FixedDialog
Now let's start programming
First, let's create the function ReturnRGBValue. What this function does it that it converts the given integer (from 0 to 9) into an RGB value.
Public Function ReturnRGBValue(ByVal Number As Integer) As Color
Dim Color As New Color
If Number = 1 Then
Color = Color.FromArgb(255, 255, 255)
ElseIf Number = 2 Then
Color = Color.FromArgb(245, 245, 245)
ElseIf Number = 3 Then
Color = Color.FromArgb(235, 235, 235)
ElseIf Number = 4 Then
Color = Color.FromArgb(225, 225, 225)
ElseIf Number = 5 Then
Color = Color.FromArgb(215, 215, 215)
ElseIf Number = 6 Then
Color = Color.FromArgb(205, 205, 205)
ElseIf Number = 7 Then
Color = Color.FromArgb(195, 195, 195)
ElseIf Number = 8 Then
Color = Color.FromArgb(185, 185, 185)
ElseIf Number = 9 Then
Color = Color.FromArgb(175, 175, 175)
ElseIf Number = 0 Then
Color = Color.FromArgb(165, 165, 165)
End If
Return Color
End Function
Now that we can get a color from a simple number, we need to create our bitmap!
This is the create function. Again, it's a function because it returns a bitmap image
Public Function Create(ByVal Number As Integer) As Bitmap
Dim One As String = "1"
Dim TempNumber = Number.ToString
TempNumber = TempNumber + One
Dim StringNumber = TempNumber.ToString
NumberLength = StringNumber.Length
Dim ColorCode As New Drawing.Bitmap((NumberLength), 1)
Dim GFX As Graphics = Graphics.FromImage(ColorCode)
For i As Integer = 1 To NumberLength
Dim CurrentNumber As Integer = StringNumber.Substring(i - 1, 1)
GFX.FillRectangle(New SolidBrush(ReturnRGBValue(CurrentNumber)), i, 0, 1, 1)
If i = (NumberLength + 1) Then
Exit For
End If
Next
Return ColorCode
End Function
Now that we've assembled our color code, it's time to save it and create a GUI. Let's start by making the GUI. I've made an image showing what you need to add. Use the names provided by the image for the textbox and the buttons.
Once they're all added, head back to Form1.vb tab
Add the following ontop of the page after Public Class Form1
Dim NumberLength As Integer
It's not been added in the Create() Function, because we need it for the next Sub
This sub is the one that's gonna save the bitmap where the user chose to have it saved
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Button2.Text = "..." Then
MsgBox("Place select where you want your Color Code to be saved")
Else
Dim BMP As New Drawing.Bitmap(19, 10)
BMP = Create(TextBox1.Text)
Dim NewBMP As New Drawing.Bitmap(NumberLength, 1)
NewBMP = BMP
NewBMP.Save(Button2.Text)
End If
End Sub
We can now save our bitmap, but we gotta let the user choose where he wants to save it to.
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim Save As New SaveFileDialog
Save.Filter = "PNG |*.png |JPG |*.jpg |GIF |*.gif"
Save.ShowDialog()
Button2.Text = Save.FileName
End Sub
We're almost done. To prevent an error from showing, because the ColorCode only handles 9 numbers and to make sure that the user will only add numbers, we need to add the following code (Note that the first sub has not been made by me, it's code I found online a while ago):
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
The last thing we need to do it to prevent the textbox from having more than 9 characters
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TextBox1.MaxLength = 9
End Sub
DONE!... well half done. We need to create our decoder in order to read the ColorCode.
Creating the decoder
First, make a new project. It order to make it look good, use the settings I've written at the top.
This time, we'll need to get an integer from a color, so add the following code:
Public Function ReturnIntValue(ByVal GivenColor As Color) As Integer
Dim R As Integer = GivenColor.R
Dim G As Integer = GivenColor.G
Dim B As Integer = GivenColor.B
Dim Int As Integer
If R = 255 And G = 255 And B = 255 Then
Int = 1
ElseIf R = 245 And G = 245 And B = 245 Then
Int = 2
ElseIf R = 235 And G = 235 And B = 235 Then
Int = 3
ElseIf R = 225 And G = 225 And B = 225 Then
Int = 4
ElseIf R = 215 And G = 215 And B = 215 Then
Int = 5
ElseIf R = 205 And G = 205 And B = 205 Then
Int = 6
ElseIf R = 195 And G = 195 And B = 195 Then
Int = 7
ElseIf R = 185 And G = 185 And B = 185 Then
Int = 8
ElseIf R = 175 And G = 175 And B = 175 Then
Int = 9
ElseIf R = 165 And G = 165 And B = 165 Then
Int = 0
End If
Return Int
End Function
We're almost done! Now, let's make our function that's gonna decode our Color Code
Public Function Decode(ByVal Image As String) As Integer
Dim BMP As New Drawing.Bitmap(Image)
Dim Length As Integer = BMP.Width
Dim AssembledIntegers As Integer
For i As Integer = 1 To Length
If i = Length Then
Exit For
End If
AssembledIntegers = AssembledIntegers & ReturnIntValue(BMP.GetPixel(i, 0))
If i = Length Then
Exit For
End If
Next
Return AssembledIntegers
End Function
Time to build our GUI! Again, I made an image to make it easier to visualise
Head back to Form1 tab, and add the following code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Open As New OpenFileDialog
Open.Multiselect = False
Open.Title = "Select the color code file"
Open.ShowDialog()
Button1.Text = Open.FileName
End Sub
And...
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If Button1.Text = "..." Then
MsgBox("Select a path first")
Else
TextBox1.Text = Decode(Button1.Text)
End If
End Sub
Done. If you've done it correctly, everything should work just fine.
Points of Interest
I sure did rage a bit when there were errors. Nothing really funny happened :(
History
- Fixed an error in the title