Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for color highlighting

Status
Not open for further replies.

andy0923

Programmer
Jul 26, 2002
65
US
Doe anyone know of code or algorithm to help calculate an RGB or color number for the following:?

Given a foreground of black and a background of white, yellow is a suitable highlight color, therefor a constant can be used in code.

However if different grids have different fore and bkground colors, using a constant to hightlight could obliterate the text.

I am looking for a way to calculate a conditional highlight color, either RGB or long number, based on the particular fore and back colors of that grid. Is there a way to do this? Would there be a way through manipulating bits?

I am assuming if there were, it would have to calculate a new fore and back color number.

Thanks
 
In foxtools fll you have a function to get the r,g and b values and then can compute with these values and put them together to a new RGB color.

But the best contrast of some highlight color to the text color is always best either with black text, if the highlight color is light, or with white text, if the highlight color is dark. So I'd rather put the problem the other way around and nail the foreground color to either white or black depending on the highlight color, instead of computing a highlight color depending on the normal fore- and backcolor.

The only thing you need to compute is the brightness of the highlightcolor (eg as average of R,G and B value) and then decide if text color should be black or white.

Bye, Olaf.
 

Andy,

A programmer on one of my courses once suggested that, the best way to find a colour that contrasts another colour is to subtract the second colour from "white", which is rgb(255,255,255).

So, given a colour nColor, the best contrasting colour would be:

nContrast = RGB(255,255,255) - nColor

However, I'm personally dubious that this would work reliably. But it might be worth experimenting along those lines.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi Mike,

your doubts are correct.

Simply try nColor = RGB(128,128,128) and you end up with RGB(127,127,127) which has a veeeery bad contrast.

Bye, Olaf.
 
Rather than figuring this out yourself, use GetSysColor() to pull your colors from the user's current settings.

Tamar
 
Thanks all, as always. This is an intellectual excersise for me so I will do some more thinking and research. The more I'm told I cant, the more I want to find the solution.

Using the system colors would be the most practical, I guess.

If I find another solution, I'll let you guys know.
 
By the way, Mike - your solution was precisely along the lines of what I meant. Like you said, unfortunately, it doesnt work as reliably as we want, so I'll keep plugging.
 
If you feel strongly about doing this manually, take a look at the DrawMode property for some ideas about how to mix your own colors. (I don't think you'd actually use DrawMode, but it does give some insight into color mixing.)

Tamar
 
Andy,
Like you, i too cannot resist the temptation to google some of the topics here and think sometimes when i should probably be resting or something...i found

This web page contains three basic guidelines for making effective color choices that work for nearly everyone. Following the guidelines are explanations of the three perceptual attributes of color -- hue, lightness and saturation -- as they are used by vision scientists.

Which makes for interesting reading. Im sure there is a formula there somewhere!
 
thanks for your help , White as well as your understanding - I browsed through it and did not note any formulas or algorithms.


My choices are looking more and more like either
(1) sticking to a limited number of grid colors

or

(2) spending hours creating a mammoth if-then chart with just simple hues.

Again, this is just an academic excersise.
 
Hi white605, hi Andy,

correct: A color representation with brightness, hue and saturation is surely the way to go to maximise contrast.

But the thing is, the human eye mostly reacts to brightness, the color information is less important for contrast. That's why finding maximum contrast to any color always ends at either black or white. If you maximise the brightness difference of a given color and the color searched, you either end up with a brightness of 0 or 100%, and at that brightness levels there is only black and white.

So it's really good to fix text colors to black or white only, then you may use any color as highlight color. bright colors for black texts and dark colors for white text.

The only other thin you could is to redefine your task to find coulors that have sufficient contrast to a given text color. It doesn't have to be the maximum contrast to be readable.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top