Java examples for 2D Graphics:Color
find 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{ /**/*ww w . j ava 2s .com*/ * * @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 */ public static Point findColorWithinTolerance(Color c, Color tolerance) { BufferedImage b = getScreen(); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getWidth(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { return new Point(i, j); } } } return null; } /** * * @param c * the desired color. * @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 Point location */ public static Point findColorWithinTolerance(Color c, int t) { BufferedImage b = getScreen(); Color tolerance = new Color(t, t, t); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { return new Point(i, j); } } } return null; } /** * * @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) * @param b * - the buffered image in which you would like to search for the * color specified within tolerance * @return Point location */ public static Point findColorWithinTolerance(Color c, Color tolerance, BufferedImage b) { for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { return new Point(i, j); } } } return null; } /** * * @param c * the desired color. * @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) * @param b * - the buffered image in which you would like to search for the * color specified within tolerance * @return Point location */ public static Point findColorWithinTolerance(Color c, int t, BufferedImage b) { Color tolerance = new Color(t, t, t); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { return new Point(i, j); } } } return null; } /** * * @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) * @param r * - the rectangle on the screen in which you would like to * search for the color specified within tolerance * @return Point location */ public static Point findColorWithinTolerance(Color c, Color tolerance, Rectangle r) { BufferedImage b = getScreenPart(r); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { return new Point(i, j); } } } return null; } /** * * @param c * the desired color. * @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) * @param r * - the rectangle on the screen in which you would like to * search for the color specified within tolerance * @return Point location */ public static Point findColorWithinTolerance(Color c, int t, Rectangle r) { Color tolerance = new Color(t, t, t); BufferedImage b = getScreenPart(r); for (int i = 0; i < b.getWidth(); i++) { for (int j = 0; j < b.getHeight(); j++) { if (areColorsWithinTolerance(getColor(i, j), c, tolerance)) { return new Point(i, j); } } } return null; } /** * @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; } }