Color Coding in VBA (1 Viewer)

majid.pervaiz

Registered User.
Local time
Today, 13:28
Joined
Oct 15, 2012
Messages
110
dear friends - if i want to change the color of a text box to light grey based on when someone click save let's say .. how do we write the color combination in vba code
 
tricky.

A colour is a combination of red green blue

rgb(x,y,z) is a function provided in vba.

Black is colour 0, which is rgb(0,0,0)

White is a large value which is rgb(255,255,255), which I think is 16777215
(255 * 256^2)+(255x256^1)+255

so grey is a half way value
Mid Grey is rgb(128,128,128) (which is 8421504)

It's a fiddly calculation to work out the exact colour from the RGB values, and it's also fiddly to work out the RGB constituents of a large number.

Note that some apps have the colours the other way, so the numbers evaluate asBGR,rather than RGB


so your vba is controlname.backcolor = rgb(128,128,128)

lower numbers are darker, lighter numbers are higher, and changing one colour on its own will give a hue corresponding to the colour mix.
 
I see two parts to your question.

Part I - what color constant to use. In VBA you can present a decimal, octal, or hexadecimal constant. Hex constants are prefixed by ampersand-h and can be as many digits as needed up to 8 digits (for a long integer) or 16 (if you have a quad integer).

Most people set their displays to 16 million colors which is 24 bits. In this format, you can break up your colors into Red, Green, & Blue parts very easily. If you have a different color depth for your display, this does not work. Fortunately, these days most displays WILL handle this, and the default for LED and LCD will be 16 million colors.

Here is solid red expressed as 3 bytes: &h0000FF, which is decimal 255 in the low-order byte and nothing in the other bytes. This is solid green: &H00FF00; this is solid blue: &H0FF0000 (and the leading 0 is to prevent something called "sign extension".) If you mix solid red, solid green, and solid blue, you get solid white, which is &H0FFFFFF, six bytes of 255 in each byte (plus the guard against sign extension).

How do you get gray? Gray is just dimmer white. All of these hex values are gray, but different levels of brightness. &H0EEEEEE (still fairly bright), &H0808080 (about 50% gray), &H010101 (the dimmest possible gray that isn't truly black.) By now you have noticed the pattern. If you just express three digit-pairs of the same value in hexadecimal, you get some shade of gray - and unlike the popular novel, you can have 255 shades of gray.

Part II - WHEN do you do this? Well, the answer is "in some event code" - which leads to the question, WHICH event? And THAT depends on when you want this color change. I did something similar for my bound forms. I ran a "formatter" subroutine that compared the .Value and .OldValue for any bound field/control pairing. The event that fired my particular color change was the .LostFocus routine for the given control. Before I left that control, I checked to see if (.Value = .OldValue) and if not, the control's .BackColor got changed from white to (in my case) a shade of pink. The .ForeColor also changed, to a dark shade of maroon. EDIT: This only works this way for BOUND controls. You need a different approach for unbound controls.

If you are going to do this, you need to call your color formatter subroutine from TWO places - one for the .LostFocus routine and one (for each control) from the FormCurrent routine, so that the controls will be reset to white background or whatever else you were using at the time.


One last color lesson: How do you do "pink" or "maroon" colors? Answer: Start with knowing that white + red = pink, black + red = maroon. So you take a shade of gray, like &H0B0B0B0, and add red to it to get &H0B0B0FF. Or you take black (&H000000) and add less than the full amount of red. Like &H000020 or &H000040, two dark reds.
 
Last edited:
Now you know the theory, please see the attached for a simple colour converter utility

attachment.php


Use the sliders to adjust values. The colour shown is updated automatically

EDIT: The latest version is available at:
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom