tags:

views:

312

answers:

1

Years ago, in my long lost copy of Charles Petzold's Windows 3.0 Programming book, there was a magic COLORREF or RGB value documented that you could use to check whether you should draw text in a light colour or a dark colour. E.g. if the background colour was below this value, then use black text, if it was higher, use white text. Does anyone know/remember what this magic value is?

+3  A: 

I can't tell about COLORREF but I've got good results using the luminance as threshold:

     Y= 0.3 * R + 0.59 * G + 0.11 * B

with colours expressed as a decimal value between 0.0 and 1.0.

If Y>=0.5 I considered the background "light" (and used dark text), if Y<0.5 I did the opposite.

I remember I also used other formulas including the simple mean:

     L = (R+G+B)/3

but I didn't like the result. It seems logical to me that Green contributes to lightness more than Red and Red more than Blue.

Remo.D
Calculating the luminance works perfectly. I will find that magic COLORREF value one day though... :)
Rob