Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magic COLORREF/RGB value to determine when to use light/dark text

Tags:

c++

c

windows

rgb

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?

like image 363
Rob Avatar asked Feb 16 '10 14:02

Rob


1 Answers

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.

like image 94
Remo.D Avatar answered Oct 23 '22 05:10

Remo.D