[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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.
0
riceman0
Asked:
riceman0
  • 3
  • 3
1 Solution
 
strickddCommented:
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.
0
 
riceman0Author Commented:

What is the significance of the value 0F?
0
 
strickddCommented:
since the encoding of a color in Hex ranges from 000000 to FFFFFF, the middle value would be 0F0F0F i believe.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
riceman0Author Commented:

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?

0
 
strickddCommented:
You could use System.Drawing.Color.FromArgb().GetBrightness    fill in the FromArgd() with the porper arguments in your case.
0
 
riceman0Author Commented:
Looks good, thank you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now