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 negative color. Transparency remains the same.
- * @param color
- * @return a new negative color with unchanged transparency.
- */
- public static Color negativeColor(int color) {
- int alpha = 255 - (color >> 24) &0xFF;
- int red = 255 - (color >> 16) &0xFF;
- int green = 255 - (color >> 8) &0xFF;
- int blue = 255 - color &0xFF;
- return new Color(red, green, blue, alpha);
- }
- /**
- * Returns a new negative color. Transparency remains the same.
- * @param color
- * @return a new negative color with unchanged transparency.
- */
- public static Color negativeColor(Color color) {
- return negativeColor(color.getRGB());
- }
- /**
- * Splits a color into an array of 4 bytes, ordered from the most significant byte to the least significant one.
- * @param color
- * @return a big endian byte array of length 4, starting at alpha and ending at blue
- */
- public static byte[] splitColor(int color) {
- byte[] result = new byte[4];
- result[0] = (byte) ((color >> 24) &0xFF);
- result[1] = (byte) ((color >> 16) &0xFF);
- result[2] = (byte) ((color >> 8) &0xFF);
- result[3] = (byte) (color &0xFF);
- return result;
- }
- /**
- * Splits a color into an array of 4 bytes, ordered from the most significant byte to the least significant one.
- * @param color
- * @return a big endian byte array of length 4, starting at alpha and ending at blue
- */
- public static byte[] splitColor(Color color) {
- return splitColor(color.getRGB());
- }
- /**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, false);
- }
- /**
- * 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);
- }
- /**
- * 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&0xFF) != (c2>>24) && transparency) return false;
- if ( ((c1>>8) &0xFF) != ((c2>>24) &0xFF) ) return false;
- if ( ((c1>>16) &0xFF) != ((c2>>24) &0xFF) ) return false;
- if ( ((c1>>24) &0xFF) != ((c2>>24) &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) {
- if ( ((c1>>8) &0xFF) != ((c2>>24) &0xFF) ) return false;
- if ( ((c1>>16) &0xFF) != ((c2>>24) &0xFF) ) return false;
- if ( ((c1>>24) &0xFF) != ((c2>>24) &0xFF) ) return false;
- return true;
- }
- /**
- * 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) );
- 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);
- }
- /**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