如何判断颜色是否为绿色? [英] How to Judge if a Color is Green?

查看:354
本文介绍了如何判断颜色是否为绿色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定rgb代码是否为绿色的公式是什么?这是一个尝试:

What is the formula for deciding if an rgb code is green? Here is an attempt:

public boolean isGreen(Color color){
        return (color.getGreen() > 0.8*(color.getBlue()) && color.getBlue() > color.getRed());
    }

也就是说,如果rgb的绿色值明显大于蓝色,并且蓝色大于红色,则返回true.对其准确性进行了测试.

That is, return true if the green value of rgb is considerably greater than the blue, and the blue is greater than the red. A test was conducted on its accuracy.

方法:一个JavaFX程序每6秒生成132个球,每个球的rgba值(在这种情况下,alpha不相关)

Methods: A JavaFX program generated 132 orbs every 6 seconds, each with an rgba (alpha is irrelevant in this case) value of

(Math.random(), Math.random(), Math.random(), Math.random())

程序打印出 isGreen(randomColor())的结果.

然后,作者将结果划分为四个象限之一:

Then, the author tallied the results in one of four quadrants:

  1. isGreen()返回true,但颜色在感知上不是绿色
  2. isGreen()返回true,并且颜色感知为绿色.
  3. isGreen()返回false,但是颜色在感觉上是绿色的.
  4. isGreen()返回false,并且颜色不是可感知的绿色.
  1. isGreen() returns true, but the color is perceptually not green
  2. isGreen() returns true, and the color is perceptually green.
  3. isGreen() returns false, but the color is perceptually green.
  4. isGreen() returns false, and the color is not perceptually green.

如果颜色不是绿色,则可以令人满意地预测:正确的颜色为72/99,即73%.足够了;同样,如果程序错误地将颜色预测为绿色,而不是错误地放弃绿色,则对程序更有意义.

It predicted satisfactorily if a color was not green: 72/99 correctly, or 73%. That is enough; also, it is more consequential to the program if it wrongly predicts a color as green, rather than incorrectly discarding a green color.

相反,它无法预测颜色是否为绿色.它正确地预测了13/31,或42%.这是关键部分,并且不起作用.58%的误报并不令人满意.

In contrast, it cannot predict whether a color is green. It predicted 13/31 correctly, or 42%. This is the crucial part, and it does not work. It is not satisfactory to have 58% false positives.

一个准确的公式将不胜感激!

An accurate formula would be much appreciated!

以下是说明当前结果的图表:

Here is a diagram to elucidate the current results:

推荐答案

问题已解决!

这是该方法中的一个简单的数学错误:

It was a simple mathematical mistake in the method:

public boolean isGreen(Color color){
        return (color.getGreen() > 0.8*(color.getBlue()) && color.getBlue() > color.getRed());
    }

color.getGreen()>0.8 *(color.getBlue()实际上可以允许蓝色多于绿色,并且 color.getBlue()> color.getRed()可以允许多于绿色.变成红色而不是绿色,偶尔出现紫色.

color.getGreen() > 0.8*(color.getBlue() could actually allow there to be more blue than green, and with that, color.getBlue() > color.getRed() could allow there to be more red than green, resulting in the occasional purple.

正确的方法是

public boolean isGreen(Color color){
        return ((color.getBlue() < 0.8*color.getGreen()) 
        && (color.getRed() < .64 * color.getGreen()));
    }

您需要比较的不是确保最小的绿色,而是确保最大的蓝色和最大的红色.以前,将绿色与蓝色作为基础进行比较,将红色与蓝色作为基础进行比较,而正确的方法是将蓝色和红色与绿色作为基础进行比较.

What you need to compare is not ensuring the minimum green, but ensuring the maximum blue, and the max red. Previously, green was compared with blue as the base and red with blue as the base, whereas the correct way is to compare blue and red to a green base.

这篇关于如何判断颜色是否为绿色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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