X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fgui%2Fmap%2FMapUtils.java;h=49d94676f77c70cc137fa1d204a11dffcd4eea27;hp=78206a3956959f6126ed1568a29893a61ce76e36;hb=7f5ed2be62905bd37717376dc22d09e5ea7edb4d;hpb=b361869e590bbca32664c16ac24d6296926594a5 diff --git a/tim/prune/gui/map/MapUtils.java b/tim/prune/gui/map/MapUtils.java index 78206a3..49d9467 100644 --- a/tim/prune/gui/map/MapUtils.java +++ b/tim/prune/gui/map/MapUtils.java @@ -1,7 +1,10 @@ package tim.prune.gui.map; +import java.awt.Color; +import java.awt.image.BufferedImage; + /** - * Class to manage coordinate conversions for maps + * Class to manage coordinate conversions and other stuff for maps */ public abstract class MapUtils { @@ -49,4 +52,50 @@ public abstract class MapUtils double n = Math.PI * (1 - 2 * inY); return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))); } + + /** + * Tests whether there are any dark pixels in the image within the specified x,y rectangle + * @param inImage image to test + * @param inX left X coordinate + * @param inY bottom Y coordinate + * @param inWidth width of rectangle + * @param inHeight height of rectangle + * @param inTextColour colour of text + * @return true if the rectangle overlaps stuff too close to the given colour + */ + public static boolean overlapsPoints(BufferedImage inImage, int inX, int inY, + int inWidth, int inHeight, Color inTextColour) + { + // each of the colour channels must be further away than this to count as empty + final int BRIGHTNESS_LIMIT = 80; + final int textRGB = inTextColour.getRGB(); + final int textLow = textRGB & 255; + final int textMid = (textRGB >> 8) & 255; + final int textHigh = (textRGB >> 16) & 255; + try + { + // loop over x coordinate of rectangle + for (int x=0; x> 8) & 255; + int pixHigh = (pixelColor >> 16) & 255; + //int fourthBit = (pixelColor >> 24) & 255; // alpha ignored + // If colours are too close in any channel then it's an overlap + if (Math.abs(pixLow-textLow) < BRIGHTNESS_LIMIT || + Math.abs(pixMid-textMid) < BRIGHTNESS_LIMIT || + Math.abs(pixHigh-textHigh) < BRIGHTNESS_LIMIT) {return true;} + } + } + } + catch (NullPointerException e) { + // ignore null pointers, just return false + } + return false; + } }