You are here:About>Computing & Technology>Visual Basic
About.comVisual Basic
Calculating a Contrasting Color Code
Part 3: The Second Solution Attempt
 More of this Feature
• Part 1: VBA - Calculating a Contrasting Color Code
• Part 2: The First 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

 

After completing this program, I shared it with a programming friend and he pointed out that there was a much easier way to do it! My friend suggested that it was both easier and more efficient to use logical operators and avoid converting to string variables. Specifically, he suggested this code. (Only the color green is illustrated -- red and blue would have similar code.)

Define a constant GreenMask=&hFF00. (But see the second bug below!)

Then the equivalent of (FF - YY) (that is, the hexadecimal value FF minus the hexadecimal value of the Green color) is given by:

(<color representation> Xor GreenMask) And GreenMask

The complete formula for calculating a hexadecimal remainder becomes:

<orig. color> = ((<orig. color> Xor GreenMask) And GreenMask) Or (<orig.color>) And Not GreenMask)

These logical operations might require a little explanation for some of us. But help is on the way. I'm planning a future article specifically focused on the fundamental topic of symbolic logic and Visual Basic programming. Until then, here's how this specific formula works.

First, recall the truth tables for And, Or, Not, and Xor.

Truth Tables

As an example using Green, suppose we start with the color (selected quite at random and displayed as an integer value) 8184057

This is equivalent to the Hex value, 7CE0F9 or

  • F9 - Red
  • E0 - Green
  • 7C - Blue

Next page > The First Bug > Page 1, 2, 3, 4 5, 6, 7
From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.