Raz
Raz

Reputation: 1

Compare a Color Code to a Color

package BotMenu;

import static java.lang.System.*;
import java.util.*;
import java.awt.*;
import java.awt.event.MouseEvent;

public class BotClass {

    public static void main(String[] Args){

        try {
            Robot robot = new Robot();
            Color color = robot.getPixelColor(20, 20);

            robot.mouseMove(20, 20);
            System.out.println("The code is   = " + color.getRGB());



        } catch (AWTException e) {

        }
    }
}

I'm finding a pixel on my screen, but how can I save PixelColor in a variable so I can do a if statement on it?

For example:

if ColorStuff = -239293 then stop

Upvotes: 0

Views: 78

Answers (1)

phflack
phflack

Reputation: 2727

Comparing a Color object to an int value has two straightforward approaches

A. Convert the Color to an int

Color c = new Color(255, 255, 0); //solid yellow
int i = 0xFFFFFF00; //int representation of solid yellow
if(c.getRGB() == i)
    System.out.println("yellow is yellow");

B. Convert the int to a Color

Color c = new Color(255, 0, 0, 127); //transparent red
int i = 0x7FFF0000; //int representation of transparent red
if(c.equals(new Color(i, true))) //the boolean argument indicates an alpha value
    System.out.println("red is very red");

Colors are stored in the format AARRGGBB in hexadecimal, Alpha Red Green Blue

Upvotes: 1

Related Questions