Link to home
Start Free TrialLog in
Avatar of riceman0
riceman0

asked on

How to tell if a color is light or dark


Hey, what's the "right" way to tell if a color is on the dark side or on the light side.  I'm asking becasue based on a System.Drawing.Color, I want to pick a text color, white or black, that contrasts with that background color.  In MSPaint, there is something called "luminence" but I'm not sure how it's calculated from RGB...

Thanks.
Avatar of strickdd
strickdd
Flag of United States of America image

I'm not sure, but you might start by comparing the HEX values. Then see if the value of each pair of characters is > or <= 0F.
Avatar of riceman0
riceman0

ASKER


What is the significance of the value 0F?
since the encoding of a color in Hex ranges from 000000 to FFFFFF, the middle value would be 0F0F0F i believe.

Well, half of FF is 80. Also, FF0000 would be sort of a bright red, dark red is 800000, one of the pairs are >, two are <.  What's the rule?  And a System.Drawing.Color gives you R, G, and B properties of type Byte, no need for hex.

I'm sure I could trial-and-error some rules (e.g., any component >= 80 --> bright), but I was wondering what the real way was, some real accepted formula for brightness.  For example from mspaint there appears to be a transformation between RGB and HSL (hue, saturation, and luminesence)?  Anybody know it?

ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks good, thank you.