由于系统(如网站),允许用户自定义背景色为某些部分,但不是字体颜色(保留的选项,以最小数),有没有办法以编程方式确定一个光或黑字体颜色是必要的吗?
Given a system (a website for instance) that lets a user customize the background color for some section but not the font color (to keep number of options to a minimum), is there a way to programmatically determine if a "light" or "dark" font color is necessary?
我敢肯定,有一些算法,但我不知道足够的颜色,亮度等,以数字出来我自己。
I'm sure there is some algorithm, but I don't know enough about colors, luminosity, etc to figure it out on my own.
推荐答案
我遇到过类似的问题。我必须找到在选择对比字体颜色,显示在colorscales /热图文本标签的好方法。它必须是通用的方法和产生的颜色必须是好看,这意味着简单的发电互补色不是很好的解决方案 - 有时它产生的怪,非常密集的颜色是很难看和读
I encountered similar problem. I had to find a good method of selecting contrastive font color to display text labels on colorscales/heatmaps. It had to be universal method and generated color had to be "good looking", which means that simple generating complementary color was not good solution - sometimes it generated strange, very intensive colors that were hard to watch and read.
长时间的测试,并试图解决这个问题之后,我发现最好的办法是选择白色字体的黑暗的颜色,黑色字体为光明的色彩。
After long hours of testing and trying to solve this problem, I found out that the best solution is to select white font for "dark" colors, and black font for "bright" colors.
下面是函数的例子我正在使用C#:
Here's an example of function I am using in C#:
Color ContrastColor(Color color)
{
int d = 0;
// Counting the perceptive luminance - human eye favors green color...
double a = 1 - ( 0.299 * color.R + 0.587 * color.G + 0.114 * color.B)/255;
if (a < 0.5)
d = 0; // bright colors - black font
else
d = 255; // dark colors - white font
return Color.FromArgb(d, d, d);
}
这是测试了许多不同的colorscales(彩虹,灰度,热,冰,和许多其他),并是唯一的万能的方法,我发现了。
This was tested for many various colorscales (rainbow, grayscale, heat, ice, and many others) and is the only "universal" method I found out.
修改
改变计算公式 A
来感知亮度 - 它确实看起来更好!已经实现了它在我的软件,看上去很不错。
Edit
Changed the formula of counting a
to "perceptive luminance" - it really looks better! Already implemented it in my software, looks great.
编辑2 @WebSeed提供该算法的一个伟大的工作示例: HTTP://$c$cpen.io/WebSeed/full/ pvgqEq /
Edit 2 @WebSeed provided a great working example of this algorithm: http://codepen.io/WebSeed/full/pvgqEq/
相关推荐
最新文章