@Nonnull @Override protected Object[] stringValues() { return new Object[] { hashCode() }; }
@Nonnull @Override public Object serialize(@Nonnull Color value) { return "#" + value.toHash(); } };
@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()); }
@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()); } } } }
@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; }
public double getG() { return getGreen() / 255.0D; }
public double getB() { return getBlue() / 255.0D; }
public double getR() { return getRed() / 255.0D; }
@Nonnull public Color darken() { return darken(0.3D); }
@Nonnull public Color brighten() { return brighten(0.3D); }
@Nullable @Override public Color deserialize(@Nonnull Class<Color> requestedType, @Nonnull Object value) { return Color.fromHex(value.toString()); }
@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 pixelAt(int x, int y) { return new Color(getImage().getRGB(x, y)); }
@Nonnull public java.awt.Color getAwtColor() { return cachedAwtColor != null ? cachedAwtColor : (cachedAwtColor = new java.awt.Color(hashCode(), true)); }
@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 String toBasicHash() { String hash = toHash(); if (hash.length() == 8) { return hash.substring(2); } return hash; }