0

Say if red = 200 and green = 190 and blue = 210 then where the mouse is

my problem is that red, green and blue can change each time but they will always be close to eachother ex. red=230,blue=250,green=240

I want to create an if statement that has a range

if (color.getRed()== 131 && color.getGreen() == 115 && color.getBlue() == 91)
{
robot.mousePress(MouseEvent.BUTTON1_MASK);
robot.mouseRelease(MouseEvent.BUTTON1_MASK);
System.out.println("click");
}

so if red green and blue are separated by like 20 points it does what's in the brackets.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1965081
  • 55
  • 1
  • 2
  • 8

3 Answers3

1

You could create some helper method for this.

private boolean inColorRange(int color1, int color2) {
    return Math.abs(color2-color1) <= 10;
}

This would return true if the colors are 10 or less apart.

You could rewrite your if to be something like this.

if (inColorRange(color.getRed(), color.getBlue()) && 
    inColorRange(color.getBlue(), color.getGreen()) {
    // Do something here if red and blue are within 10, and blue and
    // green are within 10
}
Tristan
  • 2,349
  • 16
  • 11
0

you can use subtraction operation to get the diff, and you can use Math.abs() to get diff always positive value

jmj
  • 237,923
  • 42
  • 401
  • 438
0
int delta = 20;

if(withinRange(color.getRed(), color.getGreen(), delta) &&
  withinRange(color.getRed(), color.getBlue(), delta) &&
  withinRange(color.getGreen(), color.getBlue(), delta)){
  robot.mousePress(MouseEvent.BUTTON1_MASK);
  robot.mouseRelease(MouseEvent.BUTTON1_MASK);
  System.out.println("click");
}


private boolean withinRange(int color1, int color2, int delta){
  return ((Math.abs((color1 - color2)) <= delta);
}
myqyl4
  • 301
  • 1
  • 4