Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.thearaxgroup.colors;
- import java.awt.Color;
- /**
- * A class for dealing with java.awt.Color or an ARGB representation of colors.
- * Contains methods for comparing colors, stacking them, initializing them and more.
- * @author Jan "Headaxe" Schultke
- *
- */
- public final class Colors {
- private Colors() {}
- /**Returns a new color using the inputs red, green, blue, alpha.
- *
- * @param red
- * @param green
- * @param blue
- * @param alpha
- * @return new Color
- */
- public static Color asColorRGB(int red, int green, int blue, int alpha) {
- Color color = new Color(red, green, blue, alpha);
- return color;
- }
- /**Returns a new color using the inputs red, green, blue.
- *
- * @param red
- * @param green
- * @param blue
- * @return new Color
- */
- public static Color asColorRGB(int red, int green, int blue) {
- Color color = new Color(red, green, blue, 255);
- return color;
- }
- /**Returns a new color using the inputs hue, saturation, brightness, alpha.
- *
- * @param hue
- * @param saturation
- * @param brightness
- * @param alpha
- * @return new Color
- */
- public static Color asColorHSB(int hue, int saturation, int brightness, int alpha) {
- int color = Color.HSBtoRGB(hue, saturation, brightness);
- color = (color & 0x00FFFFFF) | (alpha<<24);
- return new Color (color, true);
- }
- /**Returns a new color using the inputs hue, saturation, brightness.
- *
- * @param hue
- * @param saturation
- * @param brightness
- * @return new Color
- */
- public static Color asColorHSB(int hue, int saturation, int brightness) {
- int color = Color.HSBtoRGB(hue, saturation, brightness);
- return new Color (color, true);
- }
- private static Color blendColorsUnchecked(int c1, int c2, float weight1, float weight2) {
- int[] argb1 = getComponents(c1), argb2 = getComponents(c2);
- final int[] argb = new int[4];
- for (int i = 0; i < 4; i++) {
- argb[i] = (int) (argb1[i]*weight1 + argb2[i]*weight2);
- }
- return new Color(argb[1], argb[2], argb[3], argb[0]);
- }
- /**
- * Blends two colors based on their weighting.
- *
- * @param c1 the first color
- * @param c2 the second color
- * @param weight1 the weight of the first color
- * @param weight2 the weight of the second color
- * @return the new blended color
- * @throws IllegalArgumentException if any weight is smaller than 0, NaN, positive infinity or negative infinity
- */
- public static Color blendColors(int c1, int c2, float weight1, float weight2) {
- if (weight1 < 0 || Float.isInfinite(weight1) || Float.isNaN(weight1))
- throw new IllegalArgumentException("invalid weight1");
- if (weight2 < 0 || Float.isInfinite(weight2) || Float.isNaN(weight2))
- throw new IllegalArgumentException("invalid weight2");
- float
- weight_n1 = weight1 / (weight1+weight2),
- weight_n2 = weight2 / (weight1+weight2);
- return blendColorsUnchecked(c1, c2, weight_n1, weight_n2);
- }
- /**
- * Blends two colors based on a ratio of importance.
- *
- * @param c1 the first color
- * @param c2 the second color
- * @param ratio the ratio between the two colors. Must be greater than 0 and smaller than infinity
- * @return the new blended color
- * @throws IllegalArgumentException if ratio is 0 or smaller; or NaN, positive infinity or negative infinity
- */
- public static Color blendColors(int c1, int c2, float ratio) {
- if (ratio < 0 || Float.isInfinite(ratio) || Float.isNaN(ratio))
- throw new IllegalArgumentException("invalid ratio");
- float
- weight1 = (ratio >= 1) ? ratio : 1,
- weight2 = (ratio >= 1) ? 1 : 1/ratio,
- weight_n1 = weight1 / (weight1+weight2),
- weight_n2 = weight2 / (weight1+weight2);
- return blendColorsUnchecked(c1, c2, weight_n1, weight_n2);
- }
- /**
- * Blends to colors with equal weighting.
- * Calls:<br><br>{@code blendColorsUnchecked(c1, c2, 0.5f, 0.5f)}
- *
- * @param c1
- * @param c2
- * @return the new blended color
- */
- public static Color blendColors(int c1, int c2) {
- return blendColorsUnchecked(c1,c2,0.5f,0.5f);
- }
- /**
- * Blends several colors with equal weighting.
- *
- * @param colors an array of argb integers.
- * @return the new blended color
- */
- public static Color blendColors(int...colors) {
- int[] argb = new int[4];
- for (int c : colors) {
- int[] comp = getComponents(c);
- for (int i = 0; i < 4; i++) {
- argb[i] += comp[i];
- }
- }
- for (int i = 0; i < 4; i++) {
- argb[0] /= colors.length;
- }
- return new Color(argb[1], argb[2], argb[3], argb[0]);
- }
- /**
- * Blends an array of colors making the assumption that the total weight of all colors equals 1.
- *
- * @param colors the array of colors
- * @return the new Color
- */
- public static Color blendColorsUnchecked(WeightedColor...colors) {
- Color[] newColors = new Color[colors.length];
- for (int i = 0; i<colors.length; i++) {
- newColors[i] = multiply(colors[i].getRGB(), colors[i].getWeight());
- }
- return add(newColors);
- }
- /**
- * Blends an array of colors making the assumption, that the total weight of all colors may not equal 1
- *
- * @param colors
- * @return the new Color
- */
- public static Color blendColors(WeightedColor...colors) {
- float total = 0;
- for (WeightedColor wc : colors) {
- if (wc.weight < 0) throw new IllegalArgumentException("invalid color weight");
- total += wc.weight;
- }
- for (WeightedColor wc : colors) {
- wc.weight /= total;
- }
- return blendColorsUnchecked(colors);
- }
- /**
- * Blends several colors with equal weighting. Calls:<br><br>
- * {@code blendColors( asArrayARGB(colors) )}
- *
- * @param colors an array of argb integers.
- * @return the new blended color
- */
- public static Color blendColors(Color...colors) {
- return blendColors( asArrayARGB(colors) );
- }
- /**
- * Turns an array of colors into an array of ARGB integers.
- *
- * @param colors an array of colors
- * @return an array of ARGB integers.
- */
- public static int[] asArrayARGB(Color...colors) {
- int[] array = new int[colors.length];
- for (int i = 0; i<colors.length; i++) {
- array[i] = colors[i].getRGB();
- }
- return array;
- }
- /**
- * Returns the total difference between the components of two colors.
- * @param c1 first color
- * @param c2 second color
- * @param transparency false if transparency is ignored
- * @return difference between colors
- */
- public static int colorDifference(int c1, int c2, boolean transparency) {
- int a1= (c1>>24) &255;
- int r1= (c1>>16) &255;
- int g1= (c1>>8) &255;
- int b1= c1 &255;
- int a2= (c2>>24) &255;
- int r2= (c2>>16) &255;
- int g2= (c2>>8) &255;
- int b2= c2 &255;
- if (transparency) {
- return Math.abs(b2-b1) + Math.abs(g2-g1) + Math.abs(r2-r1) + Math.abs(a2-a1);
- }
- else {
- return Math.abs(b2-b1) + Math.abs(g2-g1) + Math.abs(r2-r1);
- }
- }
- /**
- * Returns the total difference between the components of two colors.
- * @param c1 first color
- * @param c2 second color
- * @param transparency false if transparency is ignored
- * @return difference between colors
- */
- public static int colorDifference(Color c1, Color c2, boolean transparency) {
- return colorDifference(c1.getRGB(), c2.getRGB(), transparency);
- }
- /**
- * Returns a new color assuming that
- *
- * @param value the value between 0 and 255
- * @return the new Color
- * @throws IllegalArgumentException if the value is not between 0 and 255
- */
- public static Color getGray(int value) {
- if (value < 0 || value > 255) throw new IllegalArgumentException("Value out of range ("+value+")");
- return new Color(value, value, value);
- }
- /**
- * Returns a new color assuming that
- *
- * @param value the value between 0 and 1
- * @return the new Color
- * @throws IllegalArgumentException if the value is not between 0 and 1
- */
- public static Color getGray(double value) {
- if (value < 0 || value > 1) throw new IllegalArgumentException("Value out of range ("+value+")");
- float f = (float)value;
- return new Color(f, f, f);
- }
- /**
- * Returns a new color assuming that
- *
- * @param value the value between 0 and 1
- * @return the new Color
- * @throws IllegalArgumentException if the value is not between 0 and 1
- */
- public static Color getGray(float value) {
- return new Color(value, value, value);
- }
- public static Color setAlpha(int c, int value) {
- int n = c >> 24;
- c += (value - n) << 24;
- return new Color(c, true);
- }
- public static Color setRed(int c, int value) {
- int n = (c >> 16) & 0xFF;
- c += (value - n) << 16;
- return new Color(c, true);
- }
- public static Color setGreen(int c, int value) {
- int n = (c >> 8) & 0xFF;
- c += (value - n) << 8;
- return new Color(c, true);
- }
- public static Color setBlue(int c, int value) {
- int n = c & 0xFF;
- c += value - n;
- return new Color(c, true);
- }
- public static Color setAlpha(Color c, int value) {
- return setAlpha(c.getRGB(), value);
- }
- public static Color setRed(Color c, int value) {
- return setRed(c.getRGB(), value);
- }
- public static Color setGreen(Color c, int value) {
- return setGreen(c.getRGB(), value);
- }
- public static Color setBlue(Color c, int value) {
- return setBlue(c.getRGB(), value);
- }
- /**
- * Returns a new color fully opaque version of the color
- *
- * @param c the color
- * @return a new color with fully opacity
- */
- public static Color noTransparency(Color c) {
- return new Color(c.getRGB(), false);
- }
- /**
- * Returns a new color fully opaque version of the color
- *
- * @param rgb the ARGB int representation of the color
- * @return a new color with fully opacity
- */
- public static Color noTransparency(int rgb) {
- return new Color(rgb, false);
- }
- /**
- * Adds the A, R, G and B values of an array of colors together.
- *
- * @param colors the array of colors
- * @return a new color
- */
- public static Color add(int...colors) {
- int r=0, g=0, b=0, a=0;
- for (int color : colors) {
- int[] array = getComponents(color);
- r += array[1];
- g += array[2];
- b += array[3];
- a += array[0];
- }
- return new Color(Math.min(255, r), Math.min(255, g), Math.min(255, b), Math.min(255, a));
- }
- /**
- * Adds the A, R, G and B values of an array of colors together.
- *
- * @param colors the array of colors
- * @return a new color
- */
- public static Color add(Color...colors) {
- return add( asArrayARGB(colors) );
- }
- /**
- * Multiplies all components of an ARGB color int with a factor.
- *
- * @param colors the array of colors
- * @return a new color
- * @throws IllegalArgumentException if the factor is smaller than 0
- */
- public static Color multiply(int rgb, float factor) {
- if (factor < 0 ) throw new IllegalArgumentException("invalid factor "+factor);
- int[] argb = getComponents(rgb);
- for (int i = 0; i<4; i++) {
- argb[i] = (int) Math.min(255, argb[i]*factor);
- }
- return new Color(argb[1], argb[2], argb[3], argb[0]);
- }
- /**
- * Checks wether two colors are equal.
- * @param c1 the RGB of the first color
- * @param c2 the RGB of the second color
- * @param transparency true if transparency is taken into consideration, else false
- * @return true if the colors are equal, false if not
- */
- public static boolean colorsAreEqual(int c1, int c2, boolean transparency) {
- if ( ((c1>>24) &0xFF) != (c2>>24) && transparency) return false;
- if ( ((c1>>16) &0xFF) != ((c2>>16) &0xFF)) return false;
- if ( ((c1>>8) &0xFF) != ((c2>>8) &0xFF)) return false;
- if ( (c1 &0xFF) != (c2 &0xFF)) return false;
- return true;
- }
- /**
- * Checks wether two colors are equal. Does not take transparency into consideration.
- * @param c1 the RGB of the first color
- * @param c2 the RGB of the second color
- * @return true if the colors are equal, false if not
- */
- public static boolean colorsAreEqual(int c1, int c2) {
- return colorsAreEqual(c1, c2, false);
- }
- /**
- * Checks wether two colors are equal.
- * @param c1 the first color
- * @param c2 the second color
- * @param transparency true if transparency is taken into consideration, else false
- * @return true if the colors are equal, false if not
- */
- public static boolean colorsAreEqual(Color c1, Color c2, boolean transparency) {
- return colorsAreEqual(c1.getRGB(), c2.getRGB(), transparency);
- }
- /**
- * Checks wether two colors are equal. Does not take transparency into consideration.
- * @param c1 the first color
- * @param c2 the second color
- * @return true if the colors are equal, false if not
- */
- public static boolean colorsAreEqual(Color c1, Color c2) {
- return colorsAreEqual(c1.getRGB(), c2.getRGB());
- }
- /**@param color
- * @return Sum of color components
- */
- public static int colorSum(int color) {
- int red = (color>>16) &0xFF;
- int green = (color>>8) &0xFF;
- int blue = color &0xFF;
- return red+green+blue;
- }
- /**@param color
- * @return Sum of color components
- */
- public static int colorSum(Color color) {
- return color.getBlue()+color.getRed()+color.getGreen();
- }
- /**Checks wether a Color is invisible
- *
- * @param color
- * @return false if alpha equals 0, else true
- */
- public static boolean isInvisible(Color color) {
- if (color.getAlpha() == 0) return true;
- else return false;
- }
- /**Checks wether a Color is invisible
- *
- * @param color
- * @return false if alpha equals 0, else true
- */
- public static boolean isInvisible(int color) {
- if (((color >> 24) & 0xFF) == 0) return true;
- else return false;
- }
- /**Checks wether a Color is invisible
- *
- * @param color
- * @return false if alpha does not equal 255, else true
- */
- public static boolean isTransparent(int color) {
- if (((color >> 24) & 0xFF) == 255) return false;
- else return true;
- }
- /**Checks wether a Color is invisible
- *
- * @param color
- * @return false if alpha does not equal 255, else true
- */
- public static boolean isTransparent(Color color) {
- if (color.getAlpha() == 255) return false;
- else return true;
- }
- /**Stacks two colors. Returned alpha value is calculated using the formula: SQRT(A1*A1+A2*A2)
- *
- * @param r1
- * @param g1
- * @param b1
- * @param a1
- * @param r2
- * @param g2
- * @param b2
- * @param a2
- * @return new Color
- */
- public static Color stackColors(int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2) {
- final int alpha = (int) Math.min(255, Math.sqrt(a2*a2 + a1*a1) + 1);
- final int red = (int) ( r1 + (r2 - r1) * (a2 /255F) );
- final int green = (int) ( g1 + (g2 - g1) * (a2 /255F) );
- final int blue = (int) ( b1 + (b2 - b1) * (a2 /255F) );
- //VLogger.log(a1+" + "+a2+" = "+alpha);
- return new Color(red, green, blue, alpha);
- }
- /**Stacks two colors in ARGB representation. Returned alpha value is calculated using the formula: SQRT(A1*A1+A2*A2)
- *
- * @param bottom
- * @param top
- * @return new Color
- */
- public static Color stackColors(int bottom, int top) {
- final int a1 = (bottom >> 24) & 0xFF;
- final int r1 = (bottom >> 16) & 0xFF;
- final int g1 = (bottom >> 8) & 0xFF;
- final int b1 = bottom & 0xFF;
- final int a2 = (top >> 24) & 0xFF;
- final int r2 = (top >> 16) & 0xFF;
- final int g2 = (top >> 8) & 0xFF;
- final int b2 = top & 0xFF;
- return stackColors(r1, g1, b1, a1, r2, g2, b2, a2);
- }
- /**
- * Splits up an ARGB int into its 4 components (alpha, red, green blue)
- *
- * @param rgb the argb value
- * @return an array of bytes in ARGB order
- */
- public static byte[] asByteArrayARGB(int rgb) {
- byte[] comp = new byte[4];
- comp[0] = (byte) ((rgb >> 24) & 0xFF);
- comp[1] = (byte) ((rgb >> 16) & 0xFF);
- comp[2] = (byte) ((rgb >> 8) & 0xFF);
- comp[3] = (byte) ( rgb & 0xFF);
- return comp;
- }
- /**
- * Splits up an ARGB int into its 4 components (alpha, red, green blue)
- *
- * @param rgb the argb value
- * @return an array of ints in ARGB order
- */
- public static int[] getComponents(int rgb) {
- int[] comp = new int[4];
- comp[0] = (rgb >> 24) & 0xFF;
- comp[1] = (rgb >> 16) & 0xFF;
- comp[2] = (rgb >> 8) & 0xFF;
- comp[3] = rgb & 0xFF;
- return comp;
- }
- /**Stacks two colors
- *
- * @param bottom
- * @param top
- * @return new Color
- */
- public static Color stackColors(Color bottom, Color top) {
- return stackColors(bottom.getRGB(), top.getRGB());
- }
- /**Adds amount to all color components of color
- *
- * @param color
- * @param amount
- * @return new Color
- */
- public static Color lightenColor(Color color, int amount) {
- int alpha = color.getAlpha();
- int red = Math.max(0, Math.min(255, color.getRed() + amount));
- int green = Math.max(0, Math.min(255, color.getGreen() + amount));
- int blue = Math.max(0, Math.min(255, color.getBlue() + amount));
- return new Color(red, green, blue, alpha);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement