5

I draw some text in a custom Label.

User can modify the label background, but the control shoud accomodate the text visibility in dependence of the specified background.

So, when the background is DARK I want to draw white foreground text, when its LIGHT I want BLACK foreground.

How to "understand" if the color is LIGHT or DARK?

serhio
  • 28,010
  • 62
  • 221
  • 374
  • 1
    http://stackoverflow.com/questions/97646/how-do-i-determine-darker-or-lighter-color-variant-of-a-given-color/97697#97697 – KMån Jun 21 '11 at 10:50
  • @KMån: I don't need a darker or lighter variant of a color. I need to detect if a given color is light or dark by itself. – serhio Jul 06 '11 at 08:01
  • You might want to accept your own answer. Using a predefined function is clearly better than re-inventing the wheel. – CodesInChaos Jul 06 '11 at 19:14

2 Answers2

3

A simple solution is to add the color componets of the background color and compare them to a threshold.

You might want to give blue a lower weight. 0.3*R+0.6*G+0.1*B is a common choice.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
2

use Color.GetBrightness()

serhio
  • 28,010
  • 62
  • 221
  • 374