1. Home
  2. Computing & Technology
  3. Visual Basic
Calculating a Contrasting Color Code
Part 2: The First Solution Attempt
 More of this Feature
• Part 1: VBA - Calculating a Contrasting Color Code
• Part 3: The Second Solution Attempt
• Part 4: The First Bug
• Part 5: Symbolic Logic in Visual Basic
• Part 6: The Second Bug and VB .NET
• Part 7: Lessons Learned
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• Beginning Visual Basic
• Visual Basic 6
 
 Elsewhere on the Web
• A short intro to binary numbers
• Binary, Decimal, and Logic
• Color Differences
VB 6 versus VB .NET

 

As I thought about how to solve the problem, I recalled that the HTML color code is

&HXXYYZZ&

where (I thought - see below) XX is "red" from 00 to FF, YY is "green" and ZZ is "blue" in hexadecimal. (&H means hexadecimal and the trailing & indicates that the number is a Visual Basic Long data type.

I speculated that if you simply used the hexadecimal remainder for each of those numbers, you might get a good complementary color. In other words, create a new color code:

&HAABBCC&

where AA is FF - XX, BB is FF - YY and CC is FF - ZZ.

But programming that didn't turn out to be quite that simple. My initial assumption was that I had to work in decimal to actually calculate the individual color values. Working within that assumption, I came up with this program.

Private Sub Form_Load()
' System includes
'   a text box to display result: txtDisplay
'   two text boxes to display actual values of
'     Forecolor and Backcolor
'   a scroll bar to reset Forecolor and Backcolor
' Set scroll bar at zero
scrColor.Value = 0
' Set initial textbox properties
txtDisplay.BackColor = 16777215
txtDisplay.ForeColor = 0
' Display initial values
txtBack.Text = CStr(txtDisplay.BackColor)
txtFore.Text = CStr(txtDisplay.ForeColor)
End Sub

Private Sub scrColor_Change()
' When the scroll bar changes ...
' Calculate the value of the backcolor directly
' (this one is easy ... the Forecolor is more difficult)
txtDisplay.BackColor = _
   Int(16777215 * (scrColor.Value / 32767))
Dim BackNum As Long
Dim ForeNum As Long
' Zero based arrays simply make more sense!
Dim ModNum(0 To 2) As Long
ForeNum = 0
BackNum = 0
' Starting value to calculate Forecolor number
BackNum = txtDisplay.BackColor
' For each of the three color groups ...
For J = 0 To 2
' Get the value of the group by itself
ModNum(J) = BackNum Mod 256
' Adjust the value of the remainder of the number
BackNum = (BackNum - ModNum(J)) / 256
' Invert the number for the Forecolor
ModNum(J) = 255 - ModNum(J)
Next J
' Since Forecolor is decimal, build a decimal number
For J = 2 To 0 Step -1
ForeNum = (ForeNum * 256) + ModNum(J)
Next J
' Assign the value of Forecolor
txtDisplay.ForeColor = ForeNum
' Put the numbers in the display
txtBack.Text = CStr(txtDisplay.BackColor)
txtFore.Text = CStr(txtDisplay.ForeColor)
End Sub

This code is still a good example of conversion between decimal and hexadecimal. If you thoroughly understand this example, you will know the difference!

Next page > The Second Solution Attempt > Page 1, 2, 3, 4 5, 6, 7
Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.