1. Tech

Your suggestion is on its way!

An email with a link to:

http://visualbasic.about.com/od/usevb6/l/aa031503d.htm

was emailed to:

Thanks for sharing About.com with others!

Calculating a Contrasting Color Code
Part 4: The First Bug
 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 5: Symbolic Logic in Visual Basic
• Part 6: The Second Bug and VB .NET
• Part 7: Lessons Learned
 

Here's where I ran into the first interesting bug. Those of you who are really paying attention might have noticed that Microsoft, in their infinite wisdom, made the Foreground/Background code "Blue-Green-Red," not "Red-Green-Blue" like HTML and most everything else. This accounts for the requirement to count down rather than up in the second loop in the program above:

For J = 2 To 0 Step -1

In the VB 6 Help, it states, "you can specify a color as a hexadecimal number using this syntax:

&HBBGGRR&

The BB specifies the amount of blue, GG the amount of green, and RR the amount of red.")

Given this interesting Microsoft inconsistency in the representation of color in VB, you might be able to use the code in this article to convert from one Microsoft version of color coding to another some day.

Symbolic Logic Calculations in Binary

Getting back to the main topic, the only way I know to really understand the logical calculation is to understand it in binary. The hex value, 7CE0F9 is equal to the following number in binary (spaces are added for legibility):

...7 ...C ...E ...0 ...F ...9
0111 1100 1110 0000 1111 1001

Let's apply the formula to this.

0111 1100 1110 0000 1111 1001 Original Color code
0000 0000 1111 1111 0000 0000 GreenMask
0111 1100 0001 1111 1111 1001 Xor Result

Note that this leaves the Red and Blue values the same, but reverses the Green. Reversing the bits of a binary number is what Xor is usually used for.

0111 1100 0001 1111 1111 1001 Xor Result
0000 0000 1111 1111 0000 0000 GreenMask
0000 0000 0001 1111 0000 0000 And Result (First Part)

This gives us only the Green part of the previous result. Everything else is zero. The And operation is usually used to extract bits from a larger number.

0111 1100 1110 0000 1111 1001 Original Color code
1111 1111 0000 0000 1111 1111 Not GreenMask
0111 1100 0000 0000 1111 1001 And Result (Second Part)

This gives us the Blue and Red part of the color code and forces the Green part to zero. In the next step, Or is used to combine the inverse of the Green part along with ONLY the original Red and Blue part.

0000 0000 0001 1111 0000 0000 And Result (First Part)
0111 1100 0000 0000 1111 1001 And Result (Second Part)
0111 1100 0001 1111 1111 1001 Or Result

Notice now that we have identically the same Blue and Red values, but the Green value has been exactly reversed. If all we wanted to do was to reverse the Green, this would be great! Unfortunately, we want to reverse all three. But wait! The number we need to do that is one of the intermediate results in the formula. That, in fact is what the And Result (First Part) was.

If we apply the formula just down to that point for all three colors, we get (repeating for the other two colors):

Blue

0111 1100 1110 0000 1111 1001 Original Color code
1111 1111 0000 0000 0000 0000 BlueMask
0111 1100 0001 1111 1111 1001 Xor Result
1111 1111 0000 0000 0000 0000 BlueMask
0111 1100 0000 0000 0000 0000 And Result (First Part)

Red

0111 1100 1110 0000 1111 1001 Original Color code
0000 0000 0000 0000 1111 1111 RedMask
0111 1100 1110 0000 0000 0110 Xor Result
0000 0000 0000 0000 1111 1111 RedMask
0000 0000 0000 0000 0000 0110 And Result (First Part)

We can combine them back into a single number by simply adding them.

0111 1100 0000 0000 0000 0000 Blue
0000 0000 0001 1111 0000 0000 Green
0000 0000 0000 0000 0000 0110 Red
0111 1100 0001 1111 0000 0110 Final number
Next page > Symbolic Logic in Visual Basic > Page 1, 2, 3, 4 5, 6, 7
See More About

©2014 About.com. All rights reserved.