Java 在限制范围内循环通过所有RGB组合

Java 在限制范围内循环通过所有RGB组合,java,for-loop,colors,combinations,rgb,Java,For Loop,Colors,Combinations,Rgb,我目前正在使用以下代码循环图像中的像素,并返回像素的坐标,RGB值与if语句中定义的相同: outerloop: for (int y = 0; y < image.getHeight(); y = y + 1) { for (int x = 0; x < image.getWidth(); x = x + 1) { Color mycolor = new Color(image.getRGB(x, y));

我目前正在使用以下代码循环图像中的像素,并返回像素的坐标,RGB值与if语句中定义的相同:

outerloop:
        for (int y = 0; y < image.getHeight(); y = y + 1) {
            for (int x = 0; x < image.getWidth(); x = x + 1) {
                Color mycolor = new Color(image.getRGB(x, y));
                int red = mycolor.getRed();
                int green = mycolor.getGreen();
                int blue = mycolor.getBlue();               

                if (red == 183 & green == 86 & blue == 182){
                    System.out.println(x,y);                    
                    break outerloop;
                }
            }
        }
outerloop:
对于(int y=0;y
现在的问题是,RGB值在应用程序中每次都变化很小,因此我试图为当前恒定的RGB值添加一种“容差”。例如,在一种情况下,红色可以是185,绿色可以是89,蓝色可以是相同的(182)


我知道我可以在if语句中使用OR(| |)函数定义所有条件,但这需要大量代码,有没有更简单的解决方案?例如,将正公差定义为常量,并在该公差范围内的所有RGB值组合中循环?

在公差范围内的所有颜色排列中循环将非常缓慢:假设您的公差为
+/-5
,这将需要检查1331种不同的颜色(
11红色*11绿色*11蓝色


只需将条件
red==183
更改为类似
Math.abs(red-183)
(红色>=183-公差| |红色与其检查您的值是否显式等于一组数字,不如检查它们是否在某个范围内。您可以使用类似于
(180
if(Math.abs(红色-183)
等?转换为HSV是否更有意义?饱和度大致相同,或者至少可以忽略。我想转换会很慢。
int TOLERANCE = 3;
boolean in_range(int value, int setpt) {
    return abs(value-setpt) <= TOLERANCE;
}
int R_SETPT = 183;
int G_SETPT = 86;
int B_SETPT = 182;

if (in_range(red, R_SETPT) &
    in_range(green, G_SETPT) &
    in_range(blue, B_SETPT)) {
    // etc.