反向对立的颜色 [英] Reverse opposing colors

查看:117
本文介绍了反向对立的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户安装在那里他们可以选择警报的颜色。警报是上的文本或按钮的背景颜色。但是,问题就来了,因为如果他们选择深蓝色,我们有黑色字母不是足够的对比度,则无法读取。

I have a user setup where they can choose the colors of the alerts. the Alert is the background color on a text or button. But the problem comes in that if they select a dark blue and we have black letters the contrast isnt enough and you cannot read it.

我试图做一个函数来获取反对立的颜色,但还没有得到太远。

I have tried to make a function to get the reverse opposing color but havent got too far.

有没有这样的功能?

推荐答案

我发现,对我来说,最好的解决办法是将RGB值成的 YIQ 值。由于我们只关心亮度值(重新用Y psented $ P $),有一个单一的计算来完成: Y =(299 * R + 587 * G + 114 * B)/ 1000 。 Java的$ C $下,将是这样的:

I found that the best solution for me is to convert the RGB values into YIQ values. As we are only interested in the brightness value (represented by Y), there is one single calculation to be done: Y = (299*R + 587*G + 114*B)/1000. The Java code for that would look like this:

public static Color getContrastColor(Color color) {
  double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000;
  return y >= 128 ? Color.black : Color.white;
}

您可以看到,它只是决定使用黑色或白色的,仍按原颜色的亮度。而结果的作品非常漂亮在我看来。的权重(299,587,114)是成正比的眼睛的灵敏度(或相当的视网膜的灵敏度),以相应的颜色。

You can see that it simply decides to use black or white, based upon the brightness of the original color. And the result works very nice in my opinion. The weights (299, 587, 114) are proportional to the sensitivity of the eyes (or rather the sensitivity of the retina) to the corresponding color.

这篇关于反向对立的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆