org.tbee.swing
Class ColorUtil

java.lang.Object
  extended by org.tbee.swing.ColorUtil

public class ColorUtil
extends Object

Some Color Utils methods a.o. convert a HTML Color string to Color and vice versa.

Version:
$Revision: 1.10 $
Author:
$Author: toeukpap $

Constructor Summary
ColorUtil()
           
 
Method Summary
static Color alterAlpha(Color color, float alpha)
          Make a new version of the color but change the alpha component
static Color alterAlpha(Color color, int alpha)
          Make a new version of the color but change the alpha component
static Color alterTransparency(Color color, int alpha)
          Return the same color but with an altered transparency
static Color blend(Color color1, Color color2)
          Make an even blend between two colors.
static Color blend(Color color1, Color color2, double ratio)
          Blend two colors.
static Color brighter(Color color, double percentage)
          Creates a new Color that is a brighter version of this Color.
static Color brighterOrDarker(Color color, double percentage)
          Make a darker color brighter and vice versa
static Color change(Color color, double factor)
          Creates a new Color that is a scalar version of this Color.
static double colorDistance(Color color1, Color color2)
          Return the "distance" between two colors.
static double colorDistance(double[] color1, double[] color2)
          Return the "distance" between two colors.
static double colorDistance(double r1, double g1, double b1, double r2, double g2, double b2)
          Return the "distance" between two colors.
static Color darker(Color color, double percentage)
          Creates a new Color that is a darker version of this Color.
static Color getAlternatingColor(Color color)
          Determine an alternating color for e.g.
static String getHexName(Color color)
          Return the hex name of a specified color.
static Color getOppositeColor(Color color)
          Determine the opposite color but with the same alpha.
static boolean isDark(Color color)
          Check if a color is more dark than light.
static boolean isDark(double r, double g, double b)
          Check if a color is more dark than light.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColorUtil

public ColorUtil()
Method Detail

change

public static Color change(Color color,
                           double factor)
Creates a new Color that is a scalar version of this Color.


brighter

public static Color brighter(Color color,
                             double percentage)
Creates a new Color that is a brighter version of this Color.

This method applies an arbitrary scale factor to each of the three RGB components of this Color to create a brighter version of this Color. Although brighter and darker are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.

Returns:
a new Color object that is a brighter version of this Color.
See Also:
Color.brighter()

darker

public static Color darker(Color color,
                           double percentage)
Creates a new Color that is a darker version of this Color.

This method applies an arbitrary scale factor to each of the three RGB components of this Color to create a darker version of this Color. Although brighter and darker are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.

Returns:
a new Color object that is a darker version of this Color.
See Also:
Color.darker()

brighterOrDarker

public static Color brighterOrDarker(Color color,
                                     double percentage)
Make a darker color brighter and vice versa

Parameters:
color -
percentage -
Returns:

blend

public static Color blend(Color color1,
                          Color color2,
                          double ratio)
Blend two colors.

Parameters:
color1 - First color to blend.
color2 - Second color to blend.
ratio - Blend ratio. 0.5 will give even blend, 1.0 will return color1, 0.0 will return color2 and so on.
Returns:
Blended color.

blend

public static Color blend(Color color1,
                          Color color2)
Make an even blend between two colors.

Parameters:
c1 - First color to blend.
c2 - Second color to blend.
Returns:
Blended color.

getHexName

public static String getHexName(Color color)
Return the hex name of a specified color.

Parameters:
color - Color to get hex name of.
Returns:
Hex name of color: "rrggbb".

colorDistance

public static double colorDistance(double r1,
                                   double g1,
                                   double b1,
                                   double r2,
                                   double g2,
                                   double b2)
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.

Parameters:
r1, - g1, b1 First color.
r2, - g2, b2 Second color.
Returns:
Distance bwetween colors.

colorDistance

public static double colorDistance(double[] color1,
                                   double[] color2)
Return the "distance" between two colors.

Parameters:
color1 - First color [r,g,b].
color2 - Second color [r,g,b].
Returns:
Distance bwetween colors.

colorDistance

public static double colorDistance(Color color1,
                                   Color color2)
Return the "distance" between two colors.

Parameters:
color1 - First color.
color2 - Second color.
Returns:
Distance between colors.

isDark

public static boolean isDark(double r,
                             double g,
                             double b)
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.

Parameters:
r,g,b - Color to check.
Returns:
True if this is a "dark" color, false otherwise.

isDark

public static boolean isDark(Color color)
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.

Parameters:
color - Color to check.
Returns:
True if this is a "dark" color, false otherwise.

getAlternatingColor

public static Color getAlternatingColor(Color color)
Determine an alternating color for e.g. list or table rows. Use the parameter and returned colors for odd and even rows.

Parameters:
color -
Returns:

alterAlpha

public static Color alterAlpha(Color color,
                               int alpha)
Make a new version of the color but change the alpha component

Parameters:
color -
alpha -
Returns:

alterAlpha

public static Color alterAlpha(Color color,
                               float alpha)
Make a new version of the color but change the alpha component

Parameters:
color -
alpha -
Returns:

getOppositeColor

public static Color getOppositeColor(Color color)
Determine the opposite color but with the same alpha.

Parameters:
color -
Returns:

alterTransparency

public static Color alterTransparency(Color color,
                                      int alpha)
Return the same color but with an altered transparency

Parameters:
color -
Returns:


Copyright © 2011 KnowledgePlaza. All Rights Reserved.