Color Definition

P

Paul W Smith

How do I convert a color Long - 8210719, RGB(31,73,125) or #1F49FD to
something I can use to format the backcolor of a form control?

White = &H80000005& - what sort of value is this? How do I convert any of
the above color references to this format?
 
H

Helmut Meukel

Paul W Smith said:
How do I convert a color Long - 8210719, RGB(31,73,125) or #1F49FD to
something I can use to format the backcolor of a form control?

White = &H80000005& - what sort of value is this? How do I convert any of
the above color references to this format?

Paul,

do you really understand how Windows uses colors?
I don't think so.

Windows is using color constants for various system colors:
Window Background = &H80000005&
Window Text = &H80000008&
Button Face = &H8000000F&
Desktop = &H80000001&
....

These values are always the same regardless of the actual color.
There is an API function to get the actual rgb color for the constant.
If you want a control or a form look on every system like all other
forms/controls, then assign the appropriate color constants to
the color properties. Windows will use this constants and assign
the correct colors to your form/control according to the color
scheme the user has selected.

Color values are usually written as hex numbers.
&H shows VBA the string is really a hex number.

The values for each component go from 0 to 255 (=&HFF).
BTW, joel got it wrong, a RGB value has the components in
reverse order: BBGGRR. Irritating, isn't it?

If you have red, green and blue values of 200, 120 and 60
these are written as hex numbers &HC8, &H78, &H3C
RGB(200, 120, 60) returns 3963080 and
Hex(3963080) returns "3C78C8"
you can code
MyForm.BackColor = 3963080
or
MyForm.BackColor = &H3C78C8&

BTW, the trailing & tells VBA to treat the value as a Long.

To get intermediate shades between a start color and a end
color, split the color value into its red, green and blue values.
Then get the step value for each component:
redstep = (redstart - redend) / numberShades
use Double for the step, not Integer or Long
add the step value to the start value, ...

One thing to add: the human eye may see same step values
different, depending on color and intensity. Usually it can't
see small differences between very dark colors.

I use this Sub to split color values into red green and blue:
Public Sub RGB2RedGreenBlue(ByVal RGBColor As Long, R As Long, G As Long, B
As Long)
R& = RGBColor& And &HFF&
G& = (RGBColor& And &HFF00&) \ &H100&
B& = (RGBColor& And &HFF0000) \ &H10000
End Sub

HTH.

Helmut.
 
P

Peter T

Hello Helmut,

A great explanation but just this bit -
To get intermediate shades between a start color and a end
color, split the color value into its red, green and blue values.

Whilst that might work for a very small set of colours the way is do to it
is like this -
RGB to HSL
Then calculate a set of equally spaced L values between 0 to 1 (though 0 & 1
will be black & white), then do
HSL to RGB with the original H & S values and each of the incremented L
values

FWIW, although that approach theoretically returns an equally spaced set of
colour shades they may not be "perceived" as being equally separated. The
human eye doesn't perceive colour and shade differences linearly. It is
possible to devise a subjective 3D "colour space" and work out non linear
colour tone and/or shade differences from the model. But that'll be beyond
the requirements for most typical requirements here.

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top