Java examples for 2D Graphics:Color
find All Color Within Tolerance
import java.awt.Color; import java.awt.Point; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.util.ArrayList; public class Main{ /**/*from w w w .jav a 2s . c o m*/ * * @param c * the desired color. * @param tolerance * - the rgb values which can vary. If c = new Color(200,200,200) * Such as tolerance = new Color(11,3,4) it could return anywhere * from (189,197,196) - (211,203,204) * * @return Point location array */ public static Point[] findAllColorWithinTolerance(Color c, Color tolerance) { ArrayList<Point> list = new ArrayList<Point>(); BufferedImage b = getScreen(); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { list.add(new Point(i, j)); } } } Point p[] = new Point[list.size()]; for (int i = 0; i < p.length; i++) { p[i] = list.get(i); } return p; } /** * * @param c * desired Color * @param b * the buffered image in which you would like to search * @param tolerance * - the rgb values which can vary. If c = new Color(200,200,200) * Such as tolerance = new Color(11,3,4) it could return anywhere * from (189,197,196) - (211,203,204) * * @return - Array of point locations for the color within the specified * tolerance within the buffered image */ public static Point[] findAllColorWithinTolerance(Color c, BufferedImage b, Color tolerance) { ArrayList<Point> list = new ArrayList<Point>(); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { list.add(new Point(i, j)); } } } Point p[] = new Point[list.size()]; for (int i = 0; i < p.length; i++) { p[i] = list.get(i); } return p; } /** * * @param c * desired Color * @param b * the buffered image in which you would like to search * @param t * - the rgb values which can vary. If c = new Color(200,200,200) * Such as t = 10 it could return anywhere from (190,190,190) - * (210,210,210) * * @return - Array of point locations for the color within the specified * tolerance within the buffered image */ public static Point[] findAllColorWithinTolerance(Color c, BufferedImage b, int t) { Color tolerance = new Color(t, t, t); ArrayList<Point> list = new ArrayList<Point>(); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { list.add(new Point(i, j)); } } } Point p[] = new Point[list.size()]; for (int i = 0; i < p.length; i++) { p[i] = list.get(i); } return p; } /** * * @param c * desired Color * @param r * the rectangle in which you would like to search for the * desired color within the specified tolerance. * @param tolerance * - the rgb values which can vary. If c = new Color(200,200,200) * Such as tolerance = new Color(11,3,4) it could return anywhere * from (189,197,196) - (211,203,204) * * @return - Array of point locations for the color within the specified * tolerance within the buffered image */ public static Point[] findAllColorWithinTolerance(Color c, Rectangle r, Color tolerance) { BufferedImage b = getScreenPart(r); ArrayList<Point> list = new ArrayList<Point>(); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { list.add(new Point(i, j)); } } } Point p[] = new Point[list.size()]; for (int i = 0; i < p.length; i++) { p[i] = list.get(i); } return p; } /** * * @param c * desired Color * @param r * the rectangle in which you would like to search for the * desired color within the specified tolerance. * @param t * - the rgb values which can vary. If c = new Color(200,200,200) * Such as t = 10 it could return anywhere from (190,190,190) - * (210,210,210) * * @return - Array of point locations for the color within the specified * tolerance within the buffered image */ public static Point[] findAllColorWithinTolerance(Color c, Rectangle r, int t) { BufferedImage b = getScreenPart(r); Color tolerance = new Color(t, t, t); ArrayList<Point> list = new ArrayList<Point>(); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { list.add(new Point(i, j)); } } } Point p[] = new Point[list.size()]; for (int i = 0; i < p.length; i++) { p[i] = list.get(i); } return p; } /** * @return Game Screen as BufferedImage. */ public static BufferedImage getScreen() { return Client.getScreen(); } /** * * @param color1 * the first color * @param color2 * the second color param t - the rgb values which can vary. If c * = new Color(200,200,200) Such as tolerance = 10) it could * return anywhere from (190,190,190) - (210,210,210) * @param t tolerance * @return true if the colors are within the tolerance */ public static boolean areColorsWithinTolerance(Color color1, Color color2, int t) { Color tolerance = new Color(t, t, t); return (color1.getRed() - color2.getRed() < tolerance.getRed() && color1 .getRed() - color2.getRed() > -tolerance.getRed()) && (color1.getBlue() - color2.getBlue() < tolerance .getBlue() && color1.getBlue() - color2.getBlue() > -tolerance .getBlue()) && (color1.getGreen() - color2.getGreen() < tolerance .getGreen() && color1.getGreen() - color2.getGreen() > -tolerance.getGreen()); } /** * * @param color1 * the first color * @param color2 * the second color param tolerance - the rgb values which can * vary. If c = new Color(200,200,200) Such as tolerance = new * Color(11,3,4) it could return anywhere from (189,197,196) - * (211,203,204) * @return true if the colors are within the tolerance */ public static boolean areColorsWithinTolerance(Color color1, Color color2, Color tolerance) { return (color1.getRed() - color2.getRed() < tolerance.getRed() && color1 .getRed() - color2.getRed() > -tolerance.getRed()) && (color1.getBlue() - color2.getBlue() < tolerance .getBlue() && color1.getBlue() - color2.getBlue() > -tolerance .getBlue()) && (color1.getGreen() - color2.getGreen() < tolerance .getGreen() && color1.getGreen() - color2.getGreen() > -tolerance.getGreen()); } /** * Gets the color at the specified point. * * @param p * - The point at which you wish desire a color. * @return The color at the point specified. */ public static Color getColor(Point p) { BufferedImage b = getScreen(); return new java.awt.Color(b.getRGB(p.x, p.y)); } /** * Gets the color at the specified coordinates. * * @param x * The x coordinate of the point at which you desire a color. * @param y * The y coordinate of the point at which you desire a color. * @return The color at the coordinates specified. */ public static Color getColor(int x, int y) { BufferedImage b = getScreen(); return new java.awt.Color(b.getRGB(x, y)); } /** * * @param area * the area's rectangle. * @return BufferedImage of the given rectangle. */ public static BufferedImage getScreenPart(Rectangle area) { if (area.x > 0 && area.x < 756 && area.x + area.width > 0 && area.x + area.width < 756) { if (area.y > 0 && area.y < 756 && area.y + area.height > 0 && area.y + area.height < 756) { return getScreen().getSubimage(area.x, area.y, area.width, area.height); } } return null; } }