Question

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.

Était-ce utile?

La solution

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
}

Autres conseils

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

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);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top