View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Helmut Meukel Helmut Meukel is offline
external usenet poster
 
Posts: 49
Default Color Definition

"Paul W Smith" schrieb im Newsbeitrag
...
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.