智能颜色检测
我们正在开发一个医疗调度系统,该系统允许用户根据特定状态定义要在日历上显示的颜色。我们遇到的问题是,如果用户选择深色背景颜色并且我们使用深色字体,则该字体不会显示。同样,如果他们选择浅色,而我们使用浅色字体。由于多种原因,我们不希望用户设置两种颜色。
但是,我们想知道颜色的十六进制代码是否有特定的模式。也许如果它小于给定值,则应使用深色字体,否则应使用浅色字体?
那么,是否有一种智能方法可以根据用户选择的背景颜色以编程方式选择字体颜色?
艾米
We are working on a medical scheduling system that allows users to define colors to show on the calendar based on a certain status. The issue we have is that if the user picks a dark background color and we are using a dark font, the font doesn't show. Likewise if they pick a light color and we are using a light font. For a variety of reasons, we don't want to have the user setting both colors.
But, we wonder if there is a particular pattern to the hex codes of colors. Perhaps if it is less than a given value, a dark font should be used and a light font used otherwise?
So, is there an intelligent way to programatically pick the font color based on the user's choice of background color?
Amy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用十六进制代码以数学方式计算两种颜色之间的对比度。有了这些信息,问题就在于哪种颜色的文本(白色或黑色)与所选的背景颜色具有更大的对比度。该网站展示了如何用 PHP 或 Javascript 编写该代码,但它可以轻松地适应其他语言。
http://24ways.org/2010/calculated-color-contrast
You can use the hex codes to mathematically calculate the contrast between two colors. With that information the question is just which color of text (white or black) has more contrast with the chosen background color. This site shows how to write that code in PHP or Javascript, but it could easily be adapted to other languages.
http://24ways.org/2010/calculating-color-contrast
颜色亮度: http://www.webmasterworld.com/forum88/9769.htm
但是实际上,为什么不直接反转背景并使用结果颜色作为前景呢?
Color brightness: http://www.webmasterworld.com/forum88/9769.htm
But, actually, why not to just invert background and use resulting color is a foreground?
对于你的第一个问题,我相信颜色代码的十六进制值采用 RGB 格式。这意味着第一个字节是颜色的红色量;第二个字节是颜色的绿色量;第三个字节是颜色中蓝色的量。
如果您想尝试猜测字体颜色,您可以尝试看看它与 #FFFFFF 有多接近,如果小于 #777777,则使用深色。
无论如何,我不是颜色方面的专家,所以我不能保证这个答案的正确性。
For your first question, the hex values of color codes are in RGB format I believe. This means that the first byte is the amount of red-ness in the color; the second byte is the amount of green-ness in the color; and the third byte is the amount of blue-ness in the color.
If you wanted to try and guess a font color, you could try and see how close to #FFFFFF it is, and use a dark color if it is less than #777777.
I'm not a expert in colors by any means, so I won't guarantee the correctness of this answer.
如果您想要的只是一个基于未知背景颜色的基本白色/黑色脚蹼,那么答案实际上非常简单。
Andy 的亮度较差:
然后在背景亮度约为 0.37 时翻转:
我在这里详细讨论它: https://stackoverflow.com/a/69869976/10315269
If all you want is a basic white/black color flipper, based on an unknown background color, the answer's actually very simple.
Andy's down and dirty luminance:
Then flip when the background luminance is about 0.37:
I discuss it at length here: https://stackoverflow.com/a/69869976/10315269
试试这个功能;
Try this function;