如何以编程方式选择对比度高的 RGB 颜色? [英] How to pick good contrast RGB colors programmatically?
问题描述
假设,在你的程序中:
color A
是我们随机选择的一种颜色
color A
is a color we randomly select
知道颜色A
,我怎样才能选择一个与颜色A
形成高对比度的color B
?
Knowing color A
, how can I pick a color B
that will be in high contrast with color A
?
问题可以进一步简化为:想象一下 2 个方块彼此相邻填充颜色.应该人眼可以清楚地看到颜色不一样"
The problem can be further reduced to: "imagine 2 squares filled with color next to one another. It should be unambiguously clear to a human eye that colors are not the same"
示例:
- 黑色 --> 白色
- 蓝色 --> 白色
推荐答案
Web Content Accessibility Guidelines (WCAG) 2.0 (http://www.w3.org/TR/2008/REC-WCAG20-20081211)
There is some information in the Web Content Accessibility Guidelines (WCAG) 2.0 (http://www.w3.org/TR/2008/REC-WCAG20-20081211)
对比度:http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
相对亮度:http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
有一个很好的例子在这个网站但他计算了两种颜色在哪里足够,而不是如何获得它们.
There's a good example in this site but he calculate where two colors are enough, not how to get them.
要选择对比度好的颜色,我会选择互补色:例如,选择随机颜色 A,将其转换为 HSV 空间,获得互补色.
To choose a color with good contrast, I'd go with complementary colors: for example, choose the random color A, transform it to a HSV space, get the complementary hue.
互补色调:将颜色从 RGB 转换为 HSV 后,互补色调将相差 180 度(或 0.5,在 0-1 归一化色调值中).这个网站有一些关于它的 PHP 内容
Complementary hue: after you transform color from RGB to HSV, complementary hue will be 180 degrees appart (or 0.5, in a 0-1 normalized hue value). This site has something about it in PHP
这篇关于如何以编程方式选择对比度高的 RGB 颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!