In Visual Basic code, this could done as follows:
Const BlueMask = &HFF0000
Const GreenMask = &HFF00&
Const RedMask = &HFF
Dim BlueInverse As Long
Dim GreenInverse As Long
Dim RedInverse As Long
BlueInverse = _
((txtDisplay.BackColor Xor BlueMask) And BlueMask)
GreenInverse = _
((txtDisplay.BackColor Xor GreenMask) And GreenMask)
RedInverse = _
((txtDisplay.BackColor Xor RedMask) And RedMask)
txtDisplay.ForeColor = _
BlueInverse + GreenInverse + RedInverse
A very sharp-eyed reader might ask at this point, "Why didn't you just apply the operations from left to right? Why did you bother calculating First Part and Second Part and then Or'ing them together?"
For reasons similar to those in common arithmetic where A * (B + C) is not equal to (A * B) + C, you have to calculate the logical result as shown. This is formally known as the First Law of Distribution in symbolic logic. This article is already confusing enough without side tracking completely into a course on symbolic logic, so we won't go into this further right now. Programmers don't have to know this stuff very often, but when you need it, nothing else will do! A professional programmer should have this knowledge in their toolkit and every Computer Science department in the world teaches it. And since it's such an important topic, About Visual Basic is planning on a much more complete article about it in the future.
Next page >
The Second Bug and VB .NET > Page
1,
2,
3,
4
5,
6,
7