How to Judge if a Color is Green?

二次信任 提交于 2021-02-20 02:50:50

问题


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());
    }

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.

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())

The program printed the result of isGreen(randomColor()).

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

  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.

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.

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:


回答1:


Question Resolved!

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() 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.

The correct way to do this is

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.



来源:https://stackoverflow.com/questions/66163312/how-to-judge-if-a-color-is-green

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!