Colours define way of life in our daily routine. There many
different colours we see across the globe. Your day begins
with sun rising with orange-red colour and perhaps ends with
black colour, with the middle day with the blue colour in
the wonderful skies. What made me to describe colors is, now,
let us see how computer defines and make use of colors in
its daily routine.
Computer can understand only ones and zeros. Combination of
0’s and 1’s, forms a human readable format or
pattern, which is nothing but, numbers. Human understanding
of the color is through a combination of numbers, which can
be programmatically applied through the three colours Red,
Green, Blue combination. Also, for computers, it’s a
special conversion number format can also decide the colour
code to be applied. This format is called as Hexadecimal equivalent.
In Visual Basic, there is a provision for converting the applied
color into hexadecimal number, which can be used in some other
applications such as paint, adobe photoshop etc.
Now, it is more convenient for the users, if they have, little
bit exposure to colour codes. Therefore, it is better to have
an application developed for users; just to know what colour
uses what code.
Now, we will look into application development. In Visual
basic, take a blank form and place picture box, vertical scroll
bars, 4 labels for Hexadecimal, Red, Green, Blue respectively
and place the controls as shown and then write the program
code as given below.
Write first lines of code for form’s load event as,
Private Sub Form_Load()
Picture1.BackColor = RGB(VScroll1.Value, VScroll2.Value, VScroll3.Value)
Label1.Caption = Hex(Picture1.BackColor)
Label1.Caption = Format(Label1.Caption, "000000")
Label2.Caption = VScroll1.Value
Label3.Caption = VScroll2.Value
Label4.Caption = VScroll3.Value
End Sub
|
The above code will apply a color to the background of a
picture box control which is generated from the three values
which is taken from the three vertical scroll boxes. The labels
are assigned with the values of these scroller changed values
and also hexadecimal equivalent value of the background colour
applied.
In Visual basic, we term every event as a subroutine, and
the subroutine can be called in another subroutine or in an
event.
Since, we required to do the same operation which is done
by the form’s load event, for the three vertical scroll
bars, we can just call the subroutine form_load which in turn
does what the scroll bars supposed to do when we change the
scroller.
Private Sub VScroll1_Change()
Call Form_Load
End Sub
|
Private Sub VScroll2_Change()
Call Form_Load
End Sub
|
Private Sub VScroll3_Change()
Call Form_Load
End Sub
|
Now, we just look into lines of code which enables copying
the hexadecimal value of present colour into the clipboard.
Private Sub Command2_Click()
Clipboard.Clear
Clipboard.SetText Label1.Caption
End Sub
|
Finally, the code for exit button goes here,
Private Sub Command1_Click()
End
End Sub
|
Now, run the program and enjoy playing around by keep changing the colours.
Click
here to download the zip file, colourbuilder.zip, and
unzip the contents and Run the program colorbuilder.exe. If
you have got Visual basic 6.0, you can also view the form
design and source code.