Hi, I do not have much experience working with Colors.Now I want make a logic in my applet to determine is the given background color is a bright color or a dark color and I have to change the foreground color accordingly. Could you tell is it possible using java.awt.Color (jdk 1.1).
I just want to know is there any body call my bean's Getter and Setter methods with "Please" in front - My favorite quip from Bugzilla
Do something like myApplet.getBackground() to get the background Color. Or you might have to do that on some specific Component.
Once you have the right background color, use the getBlue(), getRed() and getGreen() methods to get the value (0-255) for each of the color components. How you use them to determine a good foreground color is up to you.
You could just add them together. If the R+G+B total is less than 384, then the background could be characterized as "dark", so use white as the foreground color. If the total is >= 384, then it's light and you should write in black.
Or maybe you could do that on a color component-by-component basis. For each of R, G, and B: if the background value is < 128 then make that component of the foreground color 255, else make that component of the foreground color 0.
Experiment to see what works in your application.
Ryan [ April 22, 2005: Message edited by: Ryan McGuire ]
Joined: May 03, 2001
Thanks a lot but it looks like 128,128,128 is also bit dark isn't it ?
Joined: Feb 18, 2005
It's also a bit light. IMHO it's still light enough to make black the "right" foreground color.
Of course (127,127,127) grey is only slightly darker, but my "algorithm" would suggest that you draw/write in white on it. Maybe 128 isn't the best threshold. Try some other values.
You could set up a table that has two columns with black and white printing, respectively, and rows that have different shades of grey for the background. Pick the row where the black and white printing are equally easy/difficult to read, and that will show what to set the threshold value at. (Or you could say that 128 is good enough and not waste your time.)
You've got to draw the line (no pun intended) somewhere.