如何以编程方式计算两种颜色之间的对比度?

编程入门 行业动态 更新时间:2024-10-09 21:24:11
本文介绍了如何以编程方式计算两种颜色之间的对比度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

很直观,以黄色和白色:

Pretty straight-forward, take yellow and white:

back_color = {r:255,g:255,b:255}; //white text_color = {r:255,g:255,b:0}; //yellow

上帝地球上的普遍常数的物理定律,不能在白色背景上阅读,但蓝色文本可以?

What law of physics on God's Earth of universal constants, makes the fact that yellow text can't be read on white backgrounds but blue text can?

为了我的可定制的小部件,我尝试了所有可能的颜色模型,

For the sake of my customizable widget I tried all possible color models that I found conversions functions for; neither can say that green can be on white and yellow can't, based on just numerical comparisons.

我看了Adsense(这是由所有互联网的Budda创建的) )并猜测他们做了什么,他们做预设和颜色单元距离计算。我不能这样做。

I looked at Adsense (which is created by the Budda of all Internet) and guess what they did, they made presets and color cells distance calculations. I can't to do that. My users have the right to pick even the most retina-inflammatory, unaesthetic combinations, as long as the text can still be read.

推荐答案

因此,白色将具有100%的亮度,黄色将具有89%。同时,绿色也只有59%。 11%的差异几乎是41%的差异的4倍!

Therefore white will have 100% luminance and yellow will have 89%. At the same time green have as small as 59%. 11% difference is almost 4 times lower then 41% difference!

甚至是(#00ff00 )适合阅读大量的文本。

And even lime (#00ff00) is not good for reading large amount of texts.

对于良好的对比度颜色,亮度应至少相差50%。

IMHO for good contrast colors' brightness should differ at least for 50%. And this brightness should be measured as converted to grayscale.

upd :最近找到了一个综合工具在网络上,它按顺序使用 w3文档 阈值可以从#1.4 这是更高级的东西的计算方式。

upd: Recently found a comprehensive tool for that on the web which in order uses formula from w3 document Threshold values could be taken from #1.4 Here're calculations for this more advanced thing.

function luminanace(r, g, b) { var a = [r,g,b].map(function(v) { v /= 255; return (v <= 0.03928) ? v / 12.92 : Math.pow( ((v+0.055)/1.055), 2.4 ); }); return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722; } (luminanace(255, 255, 255) + 0.05) / (luminanace(255, 255, 0) + 0.05); // 1.074 for yellow (luminanace(255, 255, 255) + 0.05) / (luminanace(0, 0, 255) + 0.05); // 8.592 for blue // minimal recommended contrast ratio is 4.5 or 3 for larger font-sizes

更多推荐

如何以编程方式计算两种颜色之间的对比度?

本文发布于:2023-11-28 21:54:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种   对比度   颜色   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!