简体   繁体   中英

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

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. Eg 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM