@Nonnull public Color[] pixelsAt(int x, int y, int width, int height) { int[] rgb = getImage().getRGB(x, y, width, height, null, 0, width); Color[] pixels = new Color[rgb.length]; for (int i = 0; i < rgb.length; i++) { pixels[i] = new Color(rgb[i]); } return pixels; }
@Nonnull public Color pixelAt(int x, int y) { return new Color(getImage().getRGB(x, y)); }
@Nullable public static Color fromHex(@Nonnull CharSequence sequence) { String s = sequence.toString(); if (!s.isEmpty()) { if (s.charAt(0) == '#') { s = s.substring(1); } else if (s.startsWith("0x")) { s = s.substring(2); } else { return null; } if (BaseConverter.isEncodable(s, BaseConverter.HEXADECIMAL)) { return new Color(BaseConverter.encodeToInt(s, BaseConverter.HEXADECIMAL)); } } return null; }
@Nonnull public static Color createDirect(@Nonnull Color mainColor, int alpha) { return new Color(mainColor.getRed(), mainColor.getGreen(), mainColor.getBlue(), alpha); } }
@Nonnull public Color brighten(double scale) { scale += 1.0D; /* Se a cor for BLACK, então (0 * qualquer escala) continua 0. Logo, min: 0.05. */ double r = Math.max(0.05D, getR()); double g = Math.max(0.05D, getG()); double b = Math.max(0.05D, getB()); return new Color(r * scale, g * scale, b * scale, getA()); }
@Nonnull public Color darken(double scale) { scale = 1.0D - scale; /* Se a cor for BLACK, então (0 * qualquer escala) continua 0. Logo, min: 0.05. */ double r = Math.max(0.05D, getR()); double g = Math.max(0.05D, getG()); double b = Math.max(0.05D, getB()); return new Color(r * scale, g * scale, b * scale, getA()); }
@Nonnull public Color inverse() { return new Color(255 - getRed(), 255 - getGreen(), 255 - getBlue(), 255 - getAlpha()); }
@Override public void applyFilter(@Nonnull BufferedImage image, @Nonnull FilterMask mask) { Color[] grayMatrix = new Color[256]; int width = image.getWidth(); int height = image.getHeight(); int[][] luminance = new int[width][height]; for (int i = 0; i < grayMatrix.length; i++) { grayMatrix[i] = new Color(i, i, i); } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (mask.accepts(x, y)) { int pixel = image.getRGB(x, y); int red = pixel >> 16 & 0xFF; int green = pixel >> 8 & 0xFF; int blue = pixel & 0xFF; luminance[x][y] = (int) (getRedLuminance() * red + getGreenLuminance() * green + getBlueLuminance() * blue); } } } for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { if (mask.accepts(x, y)) { int grayY = luminance[x - 1][y - 1] + 2 * luminance[x - 1][y] + luminance[x - 1][y + 1] - luminance[x + 1][y - 1] - 2 * luminance[x + 1][y] - luminance[x + 1][y + 1]; int magnitude = 255 - ImageWorker.truncate(Math.abs(grayY)); image.setRGB(x, y, grayMatrix[magnitude].hashCode()); } } } }