PDA

View Full Version : How to get a point by colour?



iCURiP
12-08-2011, 06:21 AM
Hey, I'm trying to find out how to get a point by the colour of the pixel, could anyone help me out here? :)

Flight
12-08-2011, 06:31 AM
When you use the 'Pick a colour' tool on Simba you'll get a little popup box saying 'Color Picker History'. In the middle right-hand of that little box it'll have the color you just picked as well as the coordinates of where you picked it. Those coordinates are what you're looking for.

iCURiP
12-08-2011, 06:37 AM
When you use the 'Pick a colour' tool on Simba you'll get a little popup box saying 'Color Picker History'. In the middle right-hand of that little box it'll have the color you just picked as well as the coordinates of where you picked it. Those coordinates are what you're looking for.

Notice how i posted this in the java programming section ^.^ I'm not talking about simba, I'm asking how to find a Point by its colour in java.

For example...


Point getPointByColor(Color c){
return *Method to get point*;
}

Flight
12-08-2011, 06:50 AM
Notice how i posted this in the java programming section ^.^ I'm not talking about simba, I'm asking how to find a Point by its colour in java.

For example...

Oh pfft. :duh: Apologies, I didn't even read the section it was posted in. Heh, my bad.

iCURiP
12-08-2011, 06:58 AM
Oh pfft. :duh: Apologies, I didn't even read the section it was posted in. Heh, my bad.

Lol no problem mate.

YoHoJo
12-08-2011, 07:19 AM
HURRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
I'm a noob.

JAVA What's that?! This is SRL here! LOLZIES!

Good luck getting answer, sorry haha.

RISK
12-08-2011, 07:21 AM
Lol, YoHoJo. :P

I'm not experienced with Java, but is this what you are looking for? http://www.dreamincode.net/code/snippet4854.htm

Zyt3x
12-08-2011, 07:34 AM
HURRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
I'm a noob.

JAVA What's that?! This is SRL here! LOLZIES!

Good luck getting answer, sorry haha.Infracted for spam.
J/K I can't infract you :)

@OP: http://www.kodejava.org/examples/295.html first result of google-search "Java get color of pixel on screen"

iCURiP
12-08-2011, 07:40 AM
Lol, YoHoJo. :P

I'm not experienced with Java, but is this what you are looking for? http://www.dreamincode.net/code/snippet4854.htm

Na but thanks for trying, that is to find the color of a pixel, I'm trying to find the pixel of a color.

iCURiP
12-08-2011, 07:43 AM
Infracted for spam.
J/K I can't infract you :)

@OP: http://www.kodejava.org/examples/295.html first result of google-search "Java get color of pixel on screen"

Again thanks for trying but this is to find the colour of a pixel, i need to find a pixel which is of a certain colour.

Zyt3x
12-08-2011, 07:45 AM
Again thanks for trying but this is to find the colour of a pixel, i need to find a pixel which is of a certain colour.Yeah. I know :p
Loop through all the pixels on the screen, checking if the pixels are the color you want it to be, if it is then return that point.

iCURiP
12-08-2011, 08:03 AM
Yeah. I know :p
Loop through all the pixels on the screen, checking if the pixels are the color you want it to be, if it is then return that point.

I feel a bit of a nob but would u be able to help me out with that :P?

Everything i try is getting NPE's =/

YoHoJo
12-08-2011, 08:44 AM
We have some good java people here just wait a bit and one of them might see it and help!

iCURiP
12-08-2011, 08:46 AM
We have some good java people here just wait a bit and one of them might see it and help!

Sexy, Sexy.... Sexy.

I Realised my method currently does work but is unbelievably slow, it took about 20 secs to search for a colour that was at 0, 0, if anyone could help me with a fast method it would be brilliant.

KingKong
12-08-2011, 09:57 AM
Sexy, Sexy.... Sexy.

I Realised my method currently does work but is unbelievably slow, it took about 20 secs to search for a colour that was at 0, 0, if anyone could help me with a fast method it would be brilliant.

I think thats java's fault. Try doing it in C or something? And i don't think many people on this forum can help you in java related stuff(most people dont like it, including me). Try stackoverflow?

Richard
12-24-2011, 03:45 AM
This may be classed as gravedigging, but I saw this thread and had a little go at making a simple colour finder. It uses the Robot class to grab the whole desktop, and then uses this image to find certain points. Unlike the previous suggested methods, this does actually run very fast (on my PC at least). Also bear in mind I'm running at a resolution of 3840x1080, so I have a lot of pixels to sort through...

Source:


import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Pixel {

public static void main(String[] args) throws AWTException{
int[] rgb = {231, 231, 231};//rgb values to be used
Point[] result = getPixelsWithColour(rgb, captureScreen());
for (int i = 0; i < result.length; i++)
System.out.println(Integer.toString((int)result[i].getX()) + "," + Integer.toString((int)result[i].getY()));
System.out.println("Number of results: " + result.length);
}

//captures the full desktop
public static BufferedImage captureScreen() throws AWTException {
Robot robot = new Robot();
return robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSiz e()));
}

//returns the colour at the given point
public static int getColour(Point pixel, BufferedImage img){
return img.getRGB((int)pixel.getX(), (int)pixel.getY());
}

//returns 3 int values, the red, green and blue values of the colour given
public static int[] getRGB(int colour){
int[] temp = new int[3];
temp[0] = (colour & 0x00ff0000) >> 16;
temp[1] = (colour & 0x0000ff00) >> 8;
temp[2] = colour & 0x000000ff;
return temp;
}

//simplified getRGB
public static int[] getRGB(Point pixel, BufferedImage img){
return getRGB(getColour(pixel, img));
}

//returns an array of points on the screen with the RGB values given
public static Point[] getPixelsWithColour(int[] rgb, BufferedImage img){
ArrayList<Point> temp = new ArrayList<Point>();
for (int i = 1; i < img.getWidth(); i++)
for (int ii = 1; ii < img.getHeight(); ii++){
int[] tempi = getRGB(getColour(new Point(i, ii), img));
if (tempi[0] == rgb[0] && tempi[1] == rgb[1] && tempi[2] == rgb[2])
temp.add(new Point(i, ii));
}
Point[] tempReturn = new Point[temp.size()];
temp.toArray(tempReturn);
return tempReturn;
}

//prints the RGB values at the given point
public static void printRGBAt(Point pixel) throws AWTException{
System.out.println(getRGB(pixel, captureScreen())[0] + "," + getRGB(pixel, captureScreen())[1] + "," + getRGB(pixel, captureScreen())[2]);
}
}

The colours that are in there at the moment just happen to be something on my Skype :P

I threw in that last method so you could grab RGB values using this system.

EDIT: I actually just did a quick test on this, and it took 759ms to calculate (slower than I thought). Please bear in mine I have over 4 million pixels to deal with :P Also this returned 4287 pixels with the said colours. I guess the implementation of this would be on a far smaller area and would take a lot less time to calculate? If this is designed for Runescape, then it would take about a 16th of the time (estimation).

EDIT2: Looking at it again, it appears to be the captureScreen() method that takes the longest time to get, which makes sense. I look into better ways of grabbing all the pixels on the screen and their colours.

iCURiP
12-24-2011, 04:02 AM
This may be classed as gravedigging, but I saw this thread and had a little go at making a simple colour finder. It uses the Robot class to grab the whole desktop, and then uses this image to find certain points. Unlike the previous suggested methods, this does actually run very fast (on my PC at least). Also bear in mind I'm running at a resolution of 3840x1080, so I have a lot of pixels to sort through...

Source:


import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Pixel {

public static void main(String[] args) throws AWTException{
int[] rgb = {231, 231, 231};//rgb values to be used
Point[] result = getPixelsWithColour(rgb, captureScreen());
for (int i = 0; i < result.length; i++)
System.out.println(Integer.toString((int)result[i].getX()) + "," + Integer.toString((int)result[i].getY()));
System.out.println("Number of results: " + result.length);
}

//captures the full desktop
public static BufferedImage captureScreen() throws AWTException {
Robot robot = new Robot();
return robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSiz e()));
}

//returns the colour at the given point
public static int getColour(Point pixel, BufferedImage img){
return img.getRGB((int)pixel.getX(), (int)pixel.getY());
}

//returns 3 int values, the red, green and blue values of the colour given
public static int[] getRGB(int colour){
int[] temp = new int[3];
temp[0] = (colour & 0x00ff0000) >> 16;
temp[1] = (colour & 0x0000ff00) >> 8;
temp[2] = colour & 0x000000ff;
return temp;
}

//simplified getRGB
public static int[] getRGB(Point pixel, BufferedImage img){
return getRGB(getColour(pixel, img));
}

//returns an array of points on the screen with the RGB values given
public static Point[] getPixelsWithColour(int[] rgb, BufferedImage img){
ArrayList<Point> temp = new ArrayList<Point>();
for (int i = 1; i < img.getWidth(); i++)
for (int ii = 1; ii < img.getHeight(); ii++){
int[] tempi = getRGB(getColour(new Point(i, ii), img));
if (tempi[0] == rgb[0] && tempi[1] == rgb[1] && tempi[2] == rgb[2])
temp.add(new Point(i, ii));
}
Point[] tempReturn = new Point[temp.size()];
temp.toArray(tempReturn);
return tempReturn;
}

//prints the RGB values at the given point
public static void printRGBAt(Point pixel) throws AWTException{
System.out.println(getRGB(pixel, captureScreen())[0] + "," + getRGB(pixel, captureScreen())[1] + "," + getRGB(pixel, captureScreen())[2]);
}
}

The colours that are in there at the moment just happen to be something on my Skype :P

I threw in that last method so you could grab RGB values using this system.

EDIT: I actually just did a quick test on this, and it took 759ms to calculate (slower than I thought). Please bear in mine I have over 4 million pixels to deal with :P Also this returned 4287 pixels with the said colours. I guess the implementation of this would be on a far smaller area and would take a lot less time to calculate? If this is designed for Runescape, then it would take about a 16th of the time (estimation).

EDIT2: Looking at it again, it appears to be the captureScreen() method that takes the longest time to get, which makes sense. I look into better ways of grabbing all the pixels on the screen and their colours.
Thanks, but i fixed this ages ago lol, i was actually just about to upload a video showing my progress. (Not in this thread)

Richard
12-24-2011, 04:02 AM
Thanks, but i fixed this ages ago lol, i was actually just about to upload a video showing my progress. (Not in this thread)

Damn. Well it killed about half an hour at 3 in the morning :p How did you get it to work efficiently?