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