1. Home
  2. Computing & Technology
  3. Visual Basic
Calculating a Contrasting Color Code
Part 5: Symbolic Logic in Visual Basic
 More of this Feature
• Part 1: VBA - Calculating a Contrasting Color Code
• Part 2: The First Solution Attempt
• Part 3: The Second Solution Attempt
• Part 4: The First Bug
• 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

 

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
Explore Visual Basic
By Category
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.