Common color utilities : Color « 2D Graphics GUI « Java






Common color utilities

        

/*
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 * MA  02111-1307, USA.
 */
//package no.geosoft.cc.color.ui;



import java.awt.Color;



/**
 * Common color utilities.
 * 
 * @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class ColorUtil
{
  /**
   * Blend two colors.
   * 
   * @param color1  First color to blend.
   * @param color2  Second color to blend.
   * @param ratio   Blend ratio. 0.5 will give even blend, 1.0 will return
   *                color1, 0.0 will return color2 and so on.
   * @return        Blended color.
   */
  public static Color blend (Color color1, Color color2, double ratio)
  {
    float r  = (float) ratio;
    float ir = (float) 1.0 - r;

    float rgb1[] = new float[3];
    float rgb2[] = new float[3];    

    color1.getColorComponents (rgb1);
    color2.getColorComponents (rgb2);    

    Color color = new Color (rgb1[0] * r + rgb2[0] * ir, 
                             rgb1[1] * r + rgb2[1] * ir, 
                             rgb1[2] * r + rgb2[2] * ir);
    
    return color;
  }


  
  /**
   * Make an even blend between two colors.
   * 
   * @param c1     First color to blend.
   * @param c2     Second color to blend.
   * @return       Blended color.
   */
  public static Color blend (Color color1, Color color2)
  {
    return ColorUtil.blend (color1, color2, 0.5);
  }
  


  /**
   * Make a color darker.
   * 
   * @param color     Color to make darker.
   * @param fraction  Darkness fraction.
   * @return          Darker color.
   */
  public static Color darker (Color color, double fraction)
  {
    int red   = (int) Math.round (color.getRed()   * (1.0 - fraction));
    int green = (int) Math.round (color.getGreen() * (1.0 - fraction));
    int blue  = (int) Math.round (color.getBlue()  * (1.0 - fraction));

    if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
    if (green < 0) green = 0; else if (green > 255) green = 255;
    if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;    

    int alpha = color.getAlpha();

    return new Color (red, green, blue, alpha);
  }

  

  /**
   * Make a color lighter.
   * 
   * @param color     Color to make lighter.
   * @param fraction  Darkness fraction.
   * @return          Lighter color.
   */
  public static Color lighter (Color color, double fraction)
  {
    int red   = (int) Math.round (color.getRed()   * (1.0 + fraction));
    int green = (int) Math.round (color.getGreen() * (1.0 + fraction));
    int blue  = (int) Math.round (color.getBlue()  * (1.0 + fraction));

    if (red   < 0) red   = 0; else if (red   > 255) red   = 255;
    if (green < 0) green = 0; else if (green > 255) green = 255;
    if (blue  < 0) blue  = 0; else if (blue  > 255) blue  = 255;    

    int alpha = color.getAlpha();

    return new Color (red, green, blue, alpha);
  }


  
  /**
   * Return the hex name of a specified color.
   * 
   * @param color  Color to get hex name of.
   * @return       Hex name of color: "rrggbb".
   */
  public static String getHexName (Color color)
  {
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();
    
    String rHex = Integer.toString (r, 16);
    String gHex = Integer.toString (g, 16);
    String bHex = Integer.toString (b, 16);        

    return (rHex.length() == 2 ? "" + rHex : "0" + rHex) +
           (gHex.length() == 2 ? "" + gHex : "0" + gHex) +
           (bHex.length() == 2 ? "" + bHex : "0" + bHex);
  }



  /**
   * Return the "distance" between two colors. The rgb entries are taken
   * to be coordinates in a 3D space [0.0-1.0], and this method returnes
   * the distance between the coordinates for the first and second color.
   * 
   * @param   r1, g1, b1  First color.
   * @param   r2, g2, b2  Second color.
   * @return  Distance bwetween colors.
   */
  public static double colorDistance (double r1, double g1, double b1,
                                      double r2, double g2, double b2)
  {
    double a = r2 - r1;
    double b = g2 - g1;
    double c = b2 - b1;
    
    return Math.sqrt (a*a + b*b + c*c);
  }
  
  
  
  /**
   * Return the "distance" between two colors.
   * 
   * @param color1  First color [r,g,b].
   * @param color2  Second color [r,g,b].
   * @return        Distance bwetween colors.
   */
  public static double colorDistance (double[] color1, double[] color2)
  {
    return ColorUtil.colorDistance (color1[0], color1[1], color1[2],
                                    color2[0], color2[1], color2[2]);
  }


  
  /**
   * Return the "distance" between two colors.
   * 
   * @param color1  First color.
   * @param color2  Second color.
   * @return        Distance between colors.
   */
  public static double colorDistance (Color color1, Color color2)
  {
    float rgb1[] = new float[3];
    float rgb2[] = new float[3];    

    color1.getColorComponents (rgb1);
    color2.getColorComponents (rgb2);    

    return ColorUtil.colorDistance (rgb1[0], rgb1[1], rgb1[2],
                                    rgb2[0], rgb2[1], rgb2[2]);
  }


  
  /**
   * Check if a color is more dark than light. Useful if an entity of
   * this color is to be labeled: Use white label on a "dark" color and
   * black label on a "light" color.
   *
   * @param r,g,b  Color to check.
   * @return       True if this is a "dark" color, false otherwise.
   */
  public static boolean isDark (double r, double g, double b)
  {
    // Measure distance to white and black respectively
    double dWhite = ColorUtil.colorDistance (r, g, b, 1.0, 1.0, 1.0);
    double dBlack = ColorUtil.colorDistance (r, g, b, 0.0, 0.0, 0.0);

    return dBlack < dWhite;
  }



  /**
   * Check if a color is more dark than light. Useful if an entity of
   * this color is to be labeled: Use white label on a "dark" color and
   * black label on a "light" color.
   *
   * @param color  Color to check.
   * @return       True if this is a "dark" color, false otherwise.
   */
  public static boolean isDark (Color color)
  {
    float r = color.getRed()   / 255.0f;
    float g = color.getGreen() / 255.0f;
    float b = color.getBlue()  / 255.0f;

    return isDark (r, g, b);
  }
}


           
         
    
    
    
    
    
    
    
  








Related examples in the same category

1.Color class is used to work with colors in Java 2D
2.Color Utilities: common color operations
3.Color Difference
4.Rainbow ColorRainbow Color
5.XOR colorXOR color
6.Color Gradient
7.Drawing with Color
8.Color fading animation
9.140 colors - defined for X Window System listed in O'Reilly html pocket reference 87pp
10.Color Util
11.Color Factory
12.An efficient color quantization algorithm
13.Utility for checking colors given either hexa or natural language string descriptions.
14.Derives a color by adding the specified offsets to the base color's hue, saturation, and brightness values
15.Map colors into names and vice versa.
16.Converts a given string into a color.
17.If the color is equal to one of the defined constant colors, that name is returned instead.
18.Converts the String representation of a color to an actual Color object.
19.Returns blue-yellow-red color scale
20.Returns green-yellow-red-black color scale
21.Returns black-red-yellow-green color scale
22.Returns color based on 0-9 scale ranging from green to yellow
23.Returns color based on 0-9 scale ranging from yellow to red
24.Returns color based on 0-9 scale ranging from black to green
25.Returns n-dimensional array of colors for given nx3 integer array of RGB values
26.Web color enum
27.Utility class for managing resources such as colors, fonts, images, etc.
28.Make a color transparent
29.Return a Color object given a string representation of it
30.Return a string representation of a color
31.Serializes a color to its HTML markup (e.g. "#ff0000" for red)
32.Parses a java.awt.Color from an HTML color string in the form '#RRGGBB' where RR, GG, and BB are the red, green, and blue bytes in hexadecimal form
33.Performs a somewhat subjective analysis of a color to determine how dark it looks to a user
34.Lightens a color by a given amount
35.Darkens a color by a given amount
36.Blend two colors
37.Utility for working with natively-ordered integer-packed RGBA-format colours.
38.HSV to RGB
39.A widget to manipulate an RGBA colour.