check if color is dark or light using java

I based this code from Nir’s C# code (owner of NBD Tech). His solution was the best and accurate that I could find compared to other existing methods that checks if a Java Color object is considered dark or light. That is the only tricky part (and vital) in order to identify what foreground color (white or black) should be used depending on the background.

See image for sample:

And the Java code below.

The getBrightness() method returns a value from 0-255 where 0 is darkest and 255 is brightest. I purposely set my preferred threshold at 130 to indicate that the color be considered dark while any value above 130 is considered light. You can change your threshold to whatever value suits your taste.

Related Posts Plugin for WordPress, Blogger...

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *