Java – reverse color

I have a user setting where they can choose the color of the alarm The alarm is the background color on the text or button But what's the problem? If they choose a dark blue, we have black letters. The contrast is not enough. You can't read it

I tried to do a function to get the opposite color, but not too much

Is there such a function?

Solution

I found that the best solution for me is to convert RGB value to YIQ value Since we are only interested in the luminance value (represented by Y), we need to make a separate calculation: y = (299 * r 587 * g 114 * b) / 1000 The Java code will 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;
}

As you can see, it only uses black or white according to the brightness of the original color The result looks very good to me The weight (299587114) is proportional to the sensitivity of the eye (or the sensitivity of the retina) relative to the corresponding color

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>