File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Color is bright or dark Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Color is bright or dark" Watch "Color is bright or dark" New topic
Author

Color is bright or dark

Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
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
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Sure.

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 ]
Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
Thanks a lot
but it looks like 128,128,128 is also bit dark isn't it ?
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
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.

Ryan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Color is bright or dark
 
Similar Threads
SCJP Certified.
Shhh! Choose your favorite colour
JBuilder 8 background color
Generating list of colours.
Why Do I Fail to Change the Color of Text Using .css?