Advertisement
Eisenwave

org.thearaxgroup.colors.ColorsLib

Mar 11th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.89 KB | None | 0 0
  1. package org.thearaxgroup.colors;
  2.  
  3. import java.awt.Color;
  4.  
  5.  
  6. /**
  7.  * A class for dealing with java.awt.Color or an ARGB representation of colors.
  8.  * Contains methods for comparing colors, stacking them, initializing them and more.
  9.  * @author Jan "Headaxe" Schultke
  10.  *
  11.  */
  12. public final class Colors {
  13.    
  14.     private Colors() {}
  15.    
  16.     /**Returns a new color using the inputs red, green, blue, alpha.
  17.      *
  18.      * @param red
  19.      * @param green
  20.      * @param blue
  21.      * @param alpha
  22.      * @return new Color
  23.      */
  24.     public static Color asColorRGB(int red, int green, int blue, int alpha) {
  25.         Color color = new Color(red, green, blue, alpha);
  26.         return color;
  27.     }
  28.    
  29.     /**Returns a new color using the inputs red, green, blue.
  30.      *
  31.      * @param red
  32.      * @param green
  33.      * @param blue
  34.      * @return new Color
  35.      */
  36.     public static Color asColorRGB(int red, int green, int blue) {
  37.         Color color = new Color(red, green, blue, 255);
  38.         return color;
  39.     }
  40.    
  41.     /**Returns a new color using the inputs hue, saturation, brightness, alpha.
  42.      *
  43.      * @param hue
  44.      * @param saturation
  45.      * @param brightness
  46.      * @param alpha
  47.      * @return new Color
  48.      */
  49.     public static Color asColorHSB(int hue, int saturation, int brightness, int alpha) {
  50.         int color = Color.HSBtoRGB(hue, saturation, brightness);
  51.         color = (color & 0x00FFFFFF) | (alpha<<24);
  52.        
  53.         return new Color (color, true);
  54.     }
  55.    
  56.     /**Returns a new color using the inputs hue, saturation, brightness.
  57.      *
  58.      * @param hue
  59.      * @param saturation
  60.      * @param brightness
  61.      * @return new Color
  62.      */
  63.     public static Color asColorHSB(int hue, int saturation, int brightness) {
  64.         int color = Color.HSBtoRGB(hue, saturation, brightness);
  65.        
  66.         return new Color (color, true);
  67.     }
  68.    
  69.     private static Color blendColorsUnchecked(int c1, int c2, float weight1, float weight2) {
  70.         int[] argb1 = getComponents(c1), argb2 = getComponents(c2);
  71.         final int[] argb = new int[4];
  72.        
  73.         for (int i = 0; i < 4; i++) {
  74.             argb[i] = (int) (argb1[i]*weight1 + argb2[i]*weight2);
  75.         }
  76.        
  77.         return new Color(argb[1], argb[2], argb[3], argb[0]);
  78.     }
  79.    
  80.     /**
  81.      * Blends two colors based on their weighting.
  82.      *
  83.      * @param c1 the first color
  84.      * @param c2 the second color
  85.      * @param weight1 the weight of the first color
  86.      * @param weight2 the weight of the second color
  87.      * @return the new blended color
  88.      * @throws IllegalArgumentException if any weight is smaller than 0, NaN, positive infinity or negative infinity
  89.      */
  90.     public static Color blendColors(int c1, int c2, float weight1, float weight2) {
  91.         if (weight1 < 0 || Float.isInfinite(weight1) || Float.isNaN(weight1))
  92.             throw new IllegalArgumentException("invalid weight1");
  93.         if (weight2 < 0 || Float.isInfinite(weight2) || Float.isNaN(weight2))
  94.             throw new IllegalArgumentException("invalid weight2");
  95.        
  96.         float
  97.         weight_n1 = weight1 / (weight1+weight2),
  98.         weight_n2 = weight2 / (weight1+weight2);
  99.        
  100.         return blendColorsUnchecked(c1, c2, weight_n1, weight_n2);
  101.     }
  102.    
  103.     /**
  104.      * Blends two colors based on a ratio of importance.
  105.      *
  106.      * @param c1 the first color
  107.      * @param c2 the second color
  108.      * @param ratio the ratio between the two colors. Must be greater than 0 and smaller than infinity
  109.      * @return the new blended color
  110.      * @throws IllegalArgumentException if ratio is 0 or smaller; or NaN, positive infinity or negative infinity
  111.      */
  112.     public static Color blendColors(int c1, int c2, float ratio) {
  113.         if (ratio < 0 || Float.isInfinite(ratio) || Float.isNaN(ratio))
  114.             throw new IllegalArgumentException("invalid ratio");
  115.        
  116.         float
  117.         weight1 = (ratio >= 1) ? ratio : 1,
  118.         weight2 = (ratio >= 1) ? 1 : 1/ratio,
  119.         weight_n1 = weight1 / (weight1+weight2),
  120.         weight_n2 = weight2 / (weight1+weight2);
  121.        
  122.         return blendColorsUnchecked(c1, c2, weight_n1, weight_n2);
  123.     }
  124.    
  125.     /**
  126.      * Blends to colors with equal weighting.
  127.      * Calls:<br><br>{@code blendColorsUnchecked(c1, c2, 0.5f, 0.5f)}
  128.      *
  129.      * @param c1
  130.      * @param c2
  131.      * @return the new blended color
  132.      */
  133.     public static Color blendColors(int c1, int c2) {
  134.         return blendColorsUnchecked(c1,c2,0.5f,0.5f);
  135.     }
  136.    
  137.     /**
  138.      * Blends several colors with equal weighting.
  139.      *
  140.      * @param colors an array of argb integers.
  141.      * @return the new blended color
  142.      */
  143.     public static Color blendColors(int...colors) {
  144.         int[] argb = new int[4];
  145.        
  146.         for (int c : colors) {
  147.             int[] comp = getComponents(c);
  148.             for (int i = 0; i < 4; i++) {
  149.                 argb[i] += comp[i];
  150.             }
  151.         }
  152.        
  153.         for (int i = 0; i < 4; i++) {
  154.             argb[0] /= colors.length;
  155.         }
  156.        
  157.         return new Color(argb[1], argb[2], argb[3], argb[0]);
  158.     }
  159.    
  160.     /**
  161.      * Blends an array of colors making the assumption that the total weight of all colors equals 1.
  162.      *
  163.      * @param colors the array of colors
  164.      * @return the new Color
  165.      */
  166.     public static Color blendColorsUnchecked(WeightedColor...colors) {
  167.         Color[] newColors = new Color[colors.length];
  168.        
  169.         for (int i = 0; i<colors.length; i++) {
  170.             newColors[i] = multiply(colors[i].getRGB(), colors[i].getWeight());
  171.         }
  172.            
  173.         return add(newColors);
  174.     }
  175.    
  176.     /**
  177.      * Blends an array of colors making the assumption, that the total weight of all colors may not equal 1
  178.      *
  179.      * @param colors
  180.      * @return the new Color
  181.      */
  182.     public static Color blendColors(WeightedColor...colors) {
  183.         float total = 0;
  184.        
  185.         for (WeightedColor wc : colors) {
  186.             if (wc.weight < 0) throw new IllegalArgumentException("invalid color weight");
  187.            
  188.             total += wc.weight;
  189.         }
  190.        
  191.         for (WeightedColor wc : colors) {
  192.             wc.weight /= total;
  193.         }
  194.        
  195.         return blendColorsUnchecked(colors);
  196.     }
  197.    
  198.     /**
  199.      * Blends several colors with equal weighting. Calls:<br><br>
  200.      * {@code blendColors( asArrayARGB(colors) )}
  201.      *
  202.      * @param colors an array of argb integers.
  203.      * @return the new blended color
  204.      */
  205.     public static Color blendColors(Color...colors) {
  206.         return blendColors( asArrayARGB(colors) );
  207.     }
  208.    
  209.     /**
  210.      * Turns an array of colors into an array of ARGB integers.
  211.      *
  212.      * @param colors an array of colors
  213.      * @return an array of ARGB integers.
  214.      */
  215.     public static int[] asArrayARGB(Color...colors) {
  216.         int[] array = new int[colors.length];
  217.        
  218.         for (int i = 0; i<colors.length; i++) {
  219.             array[i] = colors[i].getRGB();
  220.         }
  221.        
  222.         return array;
  223.     }
  224.    
  225.     /**
  226.      * Returns the total difference between the components of two colors.
  227.      * @param c1 first color
  228.      * @param c2 second color
  229.      * @param transparency false if transparency is ignored
  230.      * @return difference between colors
  231.      */
  232.     public static int colorDifference(int c1, int c2, boolean transparency) {
  233.         int a1= (c1>>24) &255;
  234.         int r1= (c1>>16) &255;
  235.         int g1= (c1>>8)  &255;
  236.         int b1= c1       &255;
  237.        
  238.         int a2= (c2>>24) &255;
  239.         int r2= (c2>>16) &255;
  240.         int g2= (c2>>8)  &255;
  241.         int b2= c2       &255;
  242.        
  243.         if (transparency) {
  244.             return Math.abs(b2-b1) + Math.abs(g2-g1) + Math.abs(r2-r1) + Math.abs(a2-a1);
  245.         }
  246.         else {
  247.             return Math.abs(b2-b1) + Math.abs(g2-g1) + Math.abs(r2-r1);
  248.         }  
  249.     }
  250.    
  251.     /**
  252.      * Returns the total difference between the components of two colors.
  253.      * @param c1 first color
  254.      * @param c2 second color
  255.      * @param transparency false if transparency is ignored
  256.      * @return difference between colors
  257.      */
  258.     public static int colorDifference(Color c1, Color c2, boolean transparency) {
  259.         return colorDifference(c1.getRGB(), c2.getRGB(), transparency);
  260.     }
  261.    
  262.     /**
  263.      * Returns a new color assuming that
  264.      *
  265.      * @param value the value between 0 and 255
  266.      * @return the new Color
  267.      * @throws IllegalArgumentException if the value is not between 0 and 255
  268.      */
  269.     public static Color getGray(int value) {
  270.         if (value < 0 || value > 255) throw new IllegalArgumentException("Value out of range ("+value+")");
  271.        
  272.         return new Color(value, value, value);
  273.     }
  274.    
  275.     /**
  276.      * Returns a new color assuming that
  277.      *
  278.      * @param value the value between 0 and 1
  279.      * @return the new Color
  280.      * @throws IllegalArgumentException if the value is not between 0 and 1
  281.      */
  282.     public static Color getGray(double value) {
  283.         if (value < 0 || value > 1) throw new IllegalArgumentException("Value out of range ("+value+")");
  284.        
  285.         float f = (float)value;
  286.         return new Color(f, f, f);
  287.     }
  288.    
  289.     /**
  290.      * Returns a new color assuming that
  291.      *
  292.      * @param value the value between 0 and 1
  293.      * @return the new Color
  294.      * @throws IllegalArgumentException if the value is not between 0 and 1
  295.      */
  296.     public static Color getGray(float value) {
  297.         return new Color(value, value, value);
  298.     }
  299.    
  300.     public static Color setAlpha(int c, int value) {
  301.         int n = c >> 24;
  302.         c += (value - n) << 24;
  303.         return new Color(c, true);
  304.     }
  305.    
  306.     public static Color setRed(int c, int value) {
  307.         int n = (c >> 16) & 0xFF;
  308.         c += (value - n) << 16;
  309.         return new Color(c, true);
  310.     }
  311.    
  312.     public static Color setGreen(int c, int value) {
  313.         int n = (c >> 8) & 0xFF;
  314.         c += (value - n) << 8;
  315.         return new Color(c, true);
  316.     }
  317.    
  318.     public static Color setBlue(int c, int value) {
  319.         int n = c & 0xFF;
  320.         c += value - n;
  321.         return new Color(c, true);
  322.     }
  323.    
  324.     public static Color setAlpha(Color c, int value) {
  325.         return setAlpha(c.getRGB(), value);
  326.     }
  327.    
  328.     public static Color setRed(Color c, int value) {
  329.         return setRed(c.getRGB(), value);
  330.     }
  331.    
  332.     public static Color setGreen(Color c, int value) {
  333.         return setGreen(c.getRGB(), value);
  334.     }
  335.    
  336.     public static Color setBlue(Color c, int value) {
  337.         return setBlue(c.getRGB(), value);
  338.     }
  339.    
  340.     /**
  341.      * Returns a new color fully opaque version of the color
  342.      *
  343.      * @param c the color
  344.      * @return a new color with fully opacity
  345.      */
  346.     public static Color noTransparency(Color c) {
  347.         return new Color(c.getRGB(), false);
  348.     }
  349.  
  350.     /**
  351.      * Returns a new color fully opaque version of the color
  352.      *
  353.      * @param rgb the ARGB int representation of the color
  354.      * @return a new color with fully opacity
  355.      */
  356.     public static Color noTransparency(int rgb) {
  357.         return new Color(rgb, false);
  358.     }
  359.    
  360.     /**
  361.      * Adds the A, R, G and B values of an array of colors together.
  362.      *
  363.      * @param colors the array of colors
  364.      * @return a new color
  365.      */
  366.     public static Color add(int...colors) {
  367.         int r=0, g=0, b=0, a=0;
  368.         for (int color : colors) {
  369.             int[] array = getComponents(color);
  370.            
  371.             r += array[1];
  372.             g += array[2];
  373.             b += array[3];
  374.             a += array[0];
  375.         }
  376.        
  377.         return new Color(Math.min(255, r), Math.min(255, g), Math.min(255, b), Math.min(255, a));
  378.     }
  379.    
  380.     /**
  381.      * Adds the A, R, G and B values of an array of colors together.
  382.      *
  383.      * @param colors the array of colors
  384.      * @return a new color
  385.      */
  386.     public static Color add(Color...colors) {
  387.         return add( asArrayARGB(colors) );
  388.     }
  389.    
  390.     /**
  391.      * Multiplies all components of an ARGB color int with a factor.
  392.      *
  393.      * @param colors the array of colors
  394.      * @return a new color
  395.      * @throws IllegalArgumentException if the factor is smaller than 0
  396.      */
  397.     public static Color multiply(int rgb, float factor) {
  398.         if (factor < 0 ) throw new IllegalArgumentException("invalid factor "+factor);
  399.        
  400.         int[] argb = getComponents(rgb);
  401.        
  402.         for (int i = 0; i<4; i++) {
  403.             argb[i] = (int) Math.min(255, argb[i]*factor);
  404.         }
  405.        
  406.         return new Color(argb[1], argb[2], argb[3], argb[0]);
  407.     }
  408.    
  409.     /**
  410.      * Checks wether two colors are equal.
  411.      * @param c1 the RGB of the first color
  412.      * @param c2 the RGB of the second color
  413.      * @param transparency true if transparency is taken into consideration, else false
  414.      * @return true if the colors are equal, false if not
  415.      */
  416.     public static boolean colorsAreEqual(int c1, int c2, boolean transparency) {
  417.         if ( ((c1>>24) &0xFF) != (c2>>24) && transparency) return false;
  418.         if ( ((c1>>16) &0xFF) != ((c2>>16) &0xFF))         return false;
  419.         if ( ((c1>>8) &0xFF)  != ((c2>>8) &0xFF))          return false;
  420.         if ( (c1 &0xFF)       != (c2 &0xFF))               return false;
  421.         return true;
  422.     }
  423.    
  424.     /**
  425.      * Checks wether two colors are equal. Does not take transparency into consideration.
  426.      * @param c1 the RGB of the first color
  427.      * @param c2 the RGB of the second color
  428.      * @return true if the colors are equal, false if not
  429.      */
  430.     public static boolean colorsAreEqual(int c1, int c2) {
  431.         return colorsAreEqual(c1, c2, false);
  432.     }
  433.    
  434.     /**
  435.      * Checks wether two colors are equal.
  436.      * @param c1 the first color
  437.      * @param c2 the second color
  438.      * @param transparency true if transparency is taken into consideration, else false
  439.      * @return true if the colors are equal, false if not
  440.      */
  441.     public static boolean colorsAreEqual(Color c1, Color c2, boolean transparency) {
  442.         return colorsAreEqual(c1.getRGB(), c2.getRGB(), transparency);
  443.     }
  444.    
  445.     /**
  446.      * Checks wether two colors are equal. Does not take transparency into consideration.
  447.      * @param c1 the first color
  448.      * @param c2 the second color
  449.      * @return true if the colors are equal, false if not
  450.      */
  451.     public static boolean colorsAreEqual(Color c1, Color c2) {
  452.         return colorsAreEqual(c1.getRGB(), c2.getRGB());
  453.     }
  454.    
  455.     /**@param color
  456.      * @return Sum of color components
  457.      */
  458.     public static int colorSum(int color) {
  459.         int red =   (color>>16) &0xFF;
  460.         int green = (color>>8) &0xFF;
  461.         int blue =  color &0xFF;
  462.        
  463.         return red+green+blue;
  464.     }
  465.    
  466.     /**@param color
  467.      * @return Sum of color components
  468.      */
  469.     public static int colorSum(Color color) {
  470.         return color.getBlue()+color.getRed()+color.getGreen();
  471.     }
  472.    
  473.     /**Checks wether a Color is invisible
  474.      *
  475.      * @param color
  476.      * @return false if alpha equals 0, else true
  477.      */
  478.     public static boolean isInvisible(Color color) {
  479.         if (color.getAlpha() == 0) return true;
  480.         else return false;
  481.     }
  482.    
  483.     /**Checks wether a Color is invisible
  484.      *
  485.      * @param color
  486.      * @return false if alpha equals 0, else true
  487.      */
  488.     public static boolean isInvisible(int color) {
  489.         if (((color >> 24) & 0xFF) == 0) return true;
  490.         else return false;
  491.     }
  492.    
  493.     /**Checks wether a Color is invisible
  494.      *
  495.      * @param color
  496.      * @return false if alpha does not equal 255, else true
  497.      */
  498.     public static boolean isTransparent(int color) {
  499.         if (((color >> 24) & 0xFF) == 255) return false;
  500.         else return true;
  501.     }
  502.    
  503.     /**Checks wether a Color is invisible
  504.      *
  505.      * @param color
  506.     * @return false if alpha does not equal 255, else true
  507.      */
  508.     public static boolean isTransparent(Color color) {
  509.         if (color.getAlpha() == 255) return false;
  510.         else return true;
  511.     }
  512.    
  513.     /**Stacks two colors. Returned alpha value is calculated using the formula: SQRT(A1*A1+A2*A2)
  514.      *
  515.      * @param r1
  516.      * @param g1
  517.      * @param b1
  518.      * @param a1
  519.      * @param r2
  520.      * @param g2
  521.      * @param b2
  522.      * @param a2
  523.      * @return new Color
  524.      */
  525.     public static Color stackColors(int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2) {
  526.         final int alpha = (int) Math.min(255, Math.sqrt(a2*a2 + a1*a1) + 1);
  527.        
  528.         final int red =   (int) ( r1 + (r2 - r1) * (a2 /255F) );
  529.         final int green = (int) ( g1 + (g2 - g1) * (a2 /255F) );
  530.         final int blue =  (int) ( b1 + (b2 - b1) * (a2 /255F) );
  531.        
  532.         //VLogger.log(a1+" + "+a2+" = "+alpha);
  533.         return new Color(red, green, blue, alpha);
  534.     }
  535.    
  536.     /**Stacks two colors in ARGB representation. Returned alpha value is calculated using the formula: SQRT(A1*A1+A2*A2)
  537.      *
  538.      * @param bottom
  539.      * @param top
  540.      * @return new Color
  541.      */
  542.     public static Color stackColors(int bottom, int top) {
  543.         final int a1 = (bottom >> 24) & 0xFF;
  544.         final int r1 = (bottom >> 16) & 0xFF;
  545.         final int g1 = (bottom >> 8)  & 0xFF;
  546.         final int b1 = bottom         & 0xFF;
  547.        
  548.         final int a2 = (top >> 24) & 0xFF;
  549.         final int r2 = (top >> 16) & 0xFF;
  550.         final int g2 = (top >> 8)  & 0xFF;
  551.         final int b2 = top         & 0xFF;
  552.        
  553.         return stackColors(r1, g1, b1, a1, r2, g2, b2, a2);
  554.     }
  555.    
  556.     /**
  557.      * Splits up an ARGB int into its 4 components (alpha, red, green blue)
  558.      *
  559.      * @param rgb the argb value
  560.      * @return an array of bytes in ARGB order
  561.      */
  562.     public static byte[] asByteArrayARGB(int rgb) {
  563.         byte[] comp = new byte[4];
  564.        
  565.         comp[0] = (byte) ((rgb >> 24) & 0xFF);
  566.         comp[1] = (byte) ((rgb >> 16) & 0xFF);
  567.         comp[2] = (byte) ((rgb >> 8)  & 0xFF);
  568.         comp[3] = (byte) ( rgb        & 0xFF);
  569.        
  570.         return comp;
  571.     }
  572.    
  573.     /**
  574.      * Splits up an ARGB int into its 4 components (alpha, red, green blue)
  575.      *
  576.      * @param rgb the argb value
  577.      * @return an array of ints in ARGB order
  578.      */
  579.     public static int[] getComponents(int rgb) {
  580.         int[] comp = new int[4];
  581.        
  582.         comp[0] = (rgb >> 24) & 0xFF;
  583.         comp[1] = (rgb >> 16) & 0xFF;
  584.         comp[2] = (rgb >> 8)  & 0xFF;
  585.         comp[3] =  rgb        & 0xFF;
  586.        
  587.         return comp;
  588.     }
  589.    
  590.     /**Stacks two colors
  591.      *
  592.      * @param bottom
  593.      * @param top
  594.      * @return new Color
  595.      */
  596.     public static Color stackColors(Color bottom, Color top) {
  597.         return stackColors(bottom.getRGB(), top.getRGB());
  598.     }
  599.    
  600.     /**Adds amount to all color components of color
  601.      *
  602.      * @param color
  603.      * @param amount
  604.      * @return new Color
  605.      */
  606.     public static Color lightenColor(Color color, int amount) {
  607.        
  608.         int alpha = color.getAlpha();
  609.        
  610.         int red = Math.max(0, Math.min(255, color.getRed() + amount));
  611.         int green = Math.max(0, Math.min(255, color.getGreen() + amount));
  612.         int blue = Math.max(0, Math.min(255, color.getBlue() + amount));
  613.        
  614.         return new Color(red, green, blue, alpha);
  615.     }
  616.  
  617.  
  618. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement