Advertisement
Eisenwave

ColorsLib.class

Jan 22nd, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.42 KB | None | 0 0
  1. package org.thearaxgroup.colors;
  2. import java.awt.Color;
  3.  
  4. /**
  5.  * A class for dealing with java.awt.Color or an ARGB representation of colors.
  6.  * Contains methods for comparing colors, stacking them, initializing them and more.
  7.  * @author Jan "Headaxe" Schultke
  8.  *
  9.  */
  10. public final class Colors {
  11.    
  12.     private Colors() {}
  13.    
  14.     /**
  15.      * Returns a new negative color. Transparency remains the same.
  16.      * @param color
  17.      * @return a new negative color with unchanged transparency.
  18.      */
  19.     public static Color negativeColor(int color) {
  20.         int alpha = 255 - (color >> 24) &0xFF;
  21.         int red =   255 - (color >> 16) &0xFF;
  22.         int green = 255 - (color >> 8) &0xFF;
  23.         int blue =  255 - color &0xFF;
  24.        
  25.         return new Color(red, green, blue, alpha);
  26.     }
  27.    
  28.     /**
  29.      * Returns a new negative color. Transparency remains the same.
  30.      * @param color
  31.      * @return a new negative color with unchanged transparency.
  32.      */
  33.     public static Color negativeColor(Color color) {
  34.         return negativeColor(color.getRGB());
  35.     }
  36.    
  37.     /**
  38.      * Splits a color into an array of 4 bytes, ordered from the most significant byte to the least significant one.
  39.      * @param color
  40.      * @return a big endian byte array of length 4, starting at alpha and ending at blue
  41.      */
  42.     public static byte[] splitColor(int color) {
  43.         byte[] result = new byte[4];
  44.         result[0] = (byte) ((color >> 24) &0xFF);
  45.         result[1] = (byte) ((color >> 16) &0xFF);
  46.         result[2] = (byte) ((color >> 8) &0xFF);
  47.         result[3] = (byte) (color &0xFF);
  48.        
  49.         return result;
  50.     }
  51.    
  52.     /**
  53.      * Splits a color into an array of 4 bytes, ordered from the most significant byte to the least significant one.
  54.      * @param color
  55.      * @return a big endian byte array of length 4, starting at alpha and ending at blue
  56.      */
  57.     public static byte[] splitColor(Color color) {
  58.         return splitColor(color.getRGB());
  59.     }
  60.    
  61.     /**Returns a new color using the inputs red, green, blue, alpha.
  62.      *
  63.      * @param red
  64.      * @param green
  65.      * @param blue
  66.      * @param alpha
  67.      * @return new Color
  68.      */
  69.     public static Color asColorRGB(int red, int green, int blue, int alpha) {
  70.         Color color = new Color(red, green, blue, alpha);
  71.         return color;
  72.     }
  73.    
  74.     /**Returns a new color using the inputs red, green, blue.
  75.      *
  76.      * @param red
  77.      * @param green
  78.      * @param blue
  79.      * @return new Color
  80.      */
  81.     public static Color asColorRGB(int red, int green, int blue) {
  82.         Color color = new Color(red, green, blue, 255);
  83.         return color;
  84.     }
  85.    
  86.     /**Returns a new color using the inputs hue, saturation, brightness, alpha.
  87.      *
  88.      * @param hue
  89.      * @param saturation
  90.      * @param brightness
  91.      * @param alpha
  92.      * @return new Color
  93.      */
  94.     public static Color asColorHSB(int hue, int saturation, int brightness, int alpha) {
  95.         int color = Color.HSBtoRGB(hue, saturation, brightness);
  96.         color = (color & 0x00FFFFFF) | (alpha<<24);
  97.        
  98.         return new Color (color, true);
  99.     }
  100.    
  101.     /**Returns a new color using the inputs hue, saturation, brightness.
  102.      *
  103.      * @param hue
  104.      * @param saturation
  105.      * @param brightness
  106.      * @return new Color
  107.      */
  108.     public static Color asColorHSB(int hue, int saturation, int brightness) {
  109.         int color = Color.HSBtoRGB(hue, saturation, brightness);
  110.        
  111.         return new Color(color, false);
  112.     }
  113.    
  114.     /**
  115.      * Returns the total difference between the components of two colors.
  116.      * @param c1 first color
  117.      * @param c2 second color
  118.      * @param transparency false if transparency is ignored
  119.      * @return difference between colors
  120.      */
  121.     public static int colorDifference(int c1, int c2, boolean transparency) {
  122.         int a1= (c1>>24) &255;
  123.         int r1= (c1>>16) &255;
  124.         int g1= (c1>>8)  &255;
  125.         int b1= c1       &255;
  126.        
  127.         int a2= (c2>>24) &255;
  128.         int r2= (c2>>16) &255;
  129.         int g2= (c2>>8)  &255;
  130.         int b2= c2       &255;
  131.        
  132.         if (transparency) {
  133.             return Math.abs(b2-b1) + Math.abs(g2-g1) + Math.abs(r2-r1) + Math.abs(a2-a1);
  134.         }
  135.         else {
  136.             return Math.abs(b2-b1) + Math.abs(g2-g1) + Math.abs(r2-r1);
  137.         }  
  138.     }
  139.    
  140.     /**
  141.      * Returns the total difference between the components of two colors.
  142.      * @param c1 first color
  143.      * @param c2 second color
  144.      * @param transparency false if transparency is ignored
  145.      * @return difference between colors
  146.      */
  147.     public static int colorDifference(Color c1, Color c2, boolean transparency) {
  148.         return colorDifference(c1.getRGB(), c2.getRGB(), transparency);
  149.     }
  150.    
  151.     /**
  152.      * Checks wether two colors are equal.
  153.      * @param c1 the RGB of the first color
  154.      * @param c2 the RGB of the second color
  155.      * @param transparency true if transparency is taken into consideration, else false
  156.      * @return true if the colors are equal, false if not
  157.      */
  158.     public static boolean colorsAreEqual(int c1, int c2, boolean transparency) {
  159.         if ( (c1&0xFF) != (c2>>24) && transparency) return false;
  160.         if ( ((c1>>8) &0xFF)  != ((c2>>24) &0xFF) ) return false;
  161.         if ( ((c1>>16) &0xFF) != ((c2>>24) &0xFF) ) return false;
  162.         if ( ((c1>>24) &0xFF) != ((c2>>24) &0xFF) ) return false;
  163.         return true;
  164.     }
  165.    
  166.     /**
  167.      * Checks wether two colors are equal. Does not take transparency into consideration.
  168.      * @param c1 the RGB of the first color
  169.      * @param c2 the RGB of the second color
  170.      * @return true if the colors are equal, false if not
  171.      */
  172.     public static boolean colorsAreEqual(int c1, int c2) {
  173.         if ( ((c1>>8) &0xFF)  != ((c2>>24) &0xFF) ) return false;
  174.         if ( ((c1>>16) &0xFF) != ((c2>>24) &0xFF) ) return false;
  175.         if ( ((c1>>24) &0xFF) != ((c2>>24) &0xFF) ) return false;
  176.         return true;
  177.     }
  178.    
  179.     /**
  180.      * Checks wether two colors are equal.
  181.      * @param c1 the first color
  182.      * @param c2 the second color
  183.      * @param transparency true if transparency is taken into consideration, else false
  184.      * @return true if the colors are equal, false if not
  185.      */
  186.     public static boolean colorsAreEqual(Color c1, Color c2, boolean transparency) {
  187.         return colorsAreEqual(c1.getRGB(), c2.getRGB(), transparency);
  188.     }
  189.    
  190.     /**
  191.      * Checks wether two colors are equal. Does not take transparency into consideration.
  192.      * @param c1 the first color
  193.      * @param c2 the second color
  194.      * @return true if the colors are equal, false if not
  195.      */
  196.     public static boolean colorsAreEqual(Color c1, Color c2) {
  197.         return colorsAreEqual(c1.getRGB(), c2.getRGB());
  198.     }
  199.    
  200.     /**@param color
  201.      * @return Sum of color components
  202.      */
  203.     public static int colorSum(int color) {
  204.         int red =   (color>>16) &0xFF;
  205.         int green = (color>>8) &0xFF;
  206.         int blue =  color &0xFF;
  207.        
  208.         return red+green+blue;
  209.     }
  210.    
  211.     /**@param color
  212.      * @return Sum of color components
  213.      */
  214.     public static int colorSum(Color color) {
  215.         return color.getBlue()+color.getRed()+color.getGreen();
  216.     }
  217.    
  218.     /**Checks wether a Color is invisible
  219.      *
  220.      * @param color
  221.      * @return false if alpha equals 0, else true
  222.      */
  223.     public static boolean isInvisible(Color color) {
  224.         if (color.getAlpha() == 0) return true;
  225.         else return false;
  226.     }
  227.    
  228.     /**Checks wether a Color is invisible
  229.      *
  230.      * @param color
  231.      * @return false if alpha equals 0, else true
  232.      */
  233.     public static boolean isInvisible(int color) {
  234.         if (((color >> 24) & 0xFF) == 0) return true;
  235.         else return false;
  236.     }
  237.    
  238.     /**Checks wether a Color is invisible
  239.      *
  240.      * @param color
  241.      * @return false if alpha does not equal 255, else true
  242.      */
  243.     public static boolean isTransparent(int color) {
  244.         if (((color >> 24) & 0xFF) == 255) return false;
  245.         else return true;
  246.     }
  247.    
  248.     /**Checks wether a Color is invisible
  249.      *
  250.      * @param color
  251.     * @return false if alpha does not equal 255, else true
  252.      */
  253.     public static boolean isTransparent(Color color) {
  254.         if (color.getAlpha() == 255) return false;
  255.         else return true;
  256.     }
  257.    
  258.     /**Stacks two colors. Returned alpha value is calculated using the formula: SQRT(A1*A1+A2*A2)
  259.      *
  260.      * @param r1
  261.      * @param g1
  262.      * @param b1
  263.      * @param a1
  264.      * @param r2
  265.      * @param g2
  266.      * @param b2
  267.      * @param a2
  268.      * @return new Color
  269.      */
  270.     public static Color stackColors(int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2) {
  271.         final int alpha = (int) Math.min(255, Math.sqrt(a2*a2 + a1*a1) + 1);
  272.        
  273.         final int red =   (int) ( r1 + (r2 - r1) * (a2 /255F) );
  274.         final int green = (int) ( g1 + (g2 - g1) * (a2 /255F) );
  275.         final int blue =  (int) ( b1 + (b2 - b1) * (a2 /255F) );
  276.        
  277.         return new Color(red, green, blue, alpha);
  278.     }
  279.    
  280.     /**Stacks two colors in ARGB representation. Returned alpha value is calculated using the formula: SQRT(A1*A1+A2*A2)
  281.      *
  282.      * @param bottom
  283.      * @param top
  284.      * @return new Color
  285.      */
  286.     public static Color stackColors(int bottom, int top) {
  287.         final int a1 = (bottom >> 24) & 0xFF;
  288.         final int r1 = (bottom >> 16) & 0xFF;
  289.         final int g1 = (bottom >> 8)  & 0xFF;
  290.         final int b1 = bottom         & 0xFF;
  291.        
  292.         final int a2 = (top >> 24) & 0xFF;
  293.         final int r2 = (top >> 16) & 0xFF;
  294.         final int g2 = (top >> 8)  & 0xFF;
  295.         final int b2 = top         & 0xFF;
  296.        
  297.         return stackColors(r1, g1, b1, a1, r2, g2, b2, a2);
  298.     }
  299.    
  300.     /**Stacks two colors
  301.      *
  302.      * @param bottom
  303.      * @param top
  304.      * @return new Color
  305.      */
  306.     public static Color stackColors(Color bottom, Color top) {
  307.         return stackColors(bottom.getRGB(), top.getRGB());
  308.     }
  309.    
  310.     /**Adds amount to all color components of color
  311.      *
  312.      * @param color
  313.      * @param amount
  314.      * @return new Color
  315.      */
  316.     public static Color lightenColor(Color color, int amount) {
  317.        
  318.         int alpha = color.getAlpha();
  319.        
  320.         int red = Math.max(0, Math.min(255, color.getRed() + amount));
  321.         int green = Math.max(0, Math.min(255, color.getGreen() + amount));
  322.         int blue = Math.max(0, Math.min(255, color.getBlue() + amount));
  323.        
  324.         return new Color(red, green, blue, alpha);
  325.     }
  326.  
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement