• Log in or sign up for an account here: https://www.sythe.org/login

Color Searching Help

pkwithpink

Apprentice
Banned
Joined
Nov 14, 2008
Sythe Gold
0
Messages
770
Color Searching Help
Hi I need help on how to search for a certain RGB color on screen and getting the coordinates of that color. Any help would be appreciated.
 
Color Searching Help
One option is to use: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html

Create a screen capture and work off of that. You just need to loop through all the pixels and such. The main issue you face is that it takes a long ass time and making it function well is quite hard. You need to be able to limit the search area so that it can find the color in a reasonable amount of time.
 
Color Searching Help
This is a primitive version of the current function I've got within a project. Latter abilities could include searching for a point with a specified tolerance etc. It also doesn't include the necessary error-catching that is really required in a complete application, so you'd best add that in if you're going to use it.

Note: This version also searches in a 'box' like format, I do have a spiral searching version which your welcome to request if you require it.

It utilises a BufferedImage named currentArea which is repeatedly updated through a thread. The currentArea contains the image-data of a portion of the screen which is selected by the user and retrieved using the java.awt.Robot class as specified above. (Or the entire screen if nothing is selected).

The function works by utilising the ability to request all the RGB-values of a BufferedImage into an array, this allows for far quicker searching for the specified colour than repeated calls to java.awt.Robot's individual pixel-request function.

Code:
/**
 * @param colour The colour to search for in the image.
 * @param area The area to search for the colour, a portion of the stored currentArea.
 * @return Returns the point in reference to the stored currentArea image if found, or 
 *              (-1,-1) if not found.
 */
public static Point FindColour(Color colour, Rectangle area) {
        int[] rgb = new int[area.width*area.height];
        currentArea.getRGB(area.x, area.y, area.width, area.height, rgb, 0, area.width);

        Point found = new Point(-1,-1);

        int searchColour = colour.getRGB();

        for(int i = 0; i < rgb.length; i++) {
            if(rgb[i] == searchColour) {
                int y = i / currentArea.getWidth();
                int x = i - (y*currentArea.getWidth());

                found = new Point(x + area.x,y + area.y);

                return found;
            }
        }

        return new Point(-1,-1);
}
 
Runescape Gamblingrunescape gambling
Back
Top