简体   繁体   English

魔术 COLORREF/RGB 值来确定何时使用浅色/深色文本

[英]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.多年前,在我丢失已久的 Charles Petzold 的 Windows 3.0 编程书籍副本中,记录了一个神奇的 COLORREF 或 RGB 值,您可以使用它来检查是否应该以浅色或深色绘制文本。 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:我不知道 COLORREF,但使用亮度作为阈值我得到了很好的结果:

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

with colours expressed as a decimal value between 0.0 and 1.0.颜色表示为 0.0 和 1.0 之间的十进制值。

If Y>=0.5 I considered the background "light" (and used dark text), if Y<0.5 I did the opposite.如果 Y>=0.5 我认为背景“亮”(并使用深色文本),如果 Y<0.5 我做相反的事情。

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.在我看来,绿色比红色和红色比蓝色更有助于亮度,这似乎是合乎逻辑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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