prefuse.util
Class ColorLib

java.lang.Object
  extended by prefuse.util.ColorLib

public class ColorLib
extends java.lang.Object

Library routines for processing color values. The standard color representation used by prefuse is to store each color as single primitive integer value, using 32 bits to represent 4 8-bit color channels: red, green, blue, and alpha (transparency). An alpha value of 0 indicates complete transparency while a maximum value (255) indicated complete opacity. The layout of the bit is as follows, moving from most significant bit on the left to least significant bit on the right:

 AAAAAAAARRRRRRRRGGGGGGGBBBBBBBB
 

This class also maintains methods for mapping these values to actual Java Color instances; a cache is maintained for quick-lookups, avoiding the need to continually allocate new Color instances.

Finally, this class also contains routine for creating color palettes for use in visualization.

Author:
jeffrey heer

Field Summary
static float[] CATEGORY_HUES
          Default palette of category hues.
static int DEFAULT_MAP_SIZE
          The default length of a color palette if its size is not otherwise specified.
static char HEX_PREFIX
           
 
Constructor Summary
ColorLib()
           
 
Method Summary
static int alpha(int color)
          Get the alpha component of the given color.
static int blue(int color)
          Get the blue component of the given color.
static int brighter(int c)
          Get a brighter shade of an input color.
static void clearCache()
          Clear the Color object cache.
static int color(java.awt.Color c)
          Get the color code for the given Color instance.
static int darker(int c)
          Get a darker shade of an input color.
static int desaturate(int c)
          Get a desaturated shade of an input color.
static int getCacheLookupCount()
          Get the number of cache lookups to the Color object cache.
static int getCacheMissCount()
          Get the number of cache misses to the Color object cache.
static int[] getCategoryPalette(int size)
          Returns a color palette of given size tries to provide colors appropriate as category labels.
static int[] getCategoryPalette(int size, float s1, float s2, float b, float a)
          Returns a color palette of given size tries to provide colors appropriate as category labels.
static java.awt.Color getColor(float r, float g, float b)
          Get a Java Color object for the given red, green, and blue values as floating point numbers in the range 0-1.0.
static java.awt.Color getColor(float r, float g, float b, float a)
          Get a Java Color object for the given red, green, blue, and alpha values as floating point numbers in the range 0-1.0.
static java.awt.Color getColor(int rgba)
          Get a Java Color object for the given color code value.
static java.awt.Color getColor(int r, int g, int b)
          Get a Java Color object for the given red, green, and blue values.
static java.awt.Color getColor(int r, int g, int b, int a)
          Get a Java Color object for the given red, green, and blue values.
static int[] getCoolPalette()
          Returns a color palette of default size that uses a "cool", blue-heavy color scheme.
static int[] getCoolPalette(int size)
          Returns a color palette that uses a "cool", blue-heavy color scheme.
static java.awt.Color getGrayscale(int v)
          Get a Java Color object for the given grayscale value.
static int[] getGrayscalePalette()
          Returns a color palette of default size that ranges from white to black through shades of gray.
static int[] getGrayscalePalette(int size)
          Returns a color palette of specified size that ranges from white to black through shades of gray.
static int[] getHotPalette()
          Returns a color map of default size that moves from black to red to yellow to white.
static int[] getHotPalette(int size)
          Returns a color map that moves from black to red to yellow to white.
static int[] getHSBPalette()
          Returns a color palette of default size that cycles through the hues of the HSB (Hue/Saturation/Brightness) color space at full saturation and brightness.
static int[] getHSBPalette(int size, float s, float b)
          Returns a color palette of given size that cycles through the hues of the HSB (Hue/Saturation/Brightness) color space.
static int[] getInterpolatedPalette(int c1, int c2)
          Returns a color palette of default size that ranges from one given color to the other.
static int[] getInterpolatedPalette(int size, int c1, int c2)
          Returns a color palette of given size that ranges from one given color to the other.
static int gray(int v)
          Get the color code for the given grayscale value.
static int gray(int v, int a)
          Get the color code for the given grayscale value.
static int green(int color)
          Get the green component of the given color.
static int hex(java.lang.String hex)
          Parse a hexadecimal String as a color code.
static int hsb(float h, float s, float b)
          Get the color code for the given hue, saturation, and brightness values, translating from HSB color space to RGB color space.
static int hsba(float h, float s, float b, float a)
          Get the color code for the given hue, saturation, and brightness values, translating from HSB color space to RGB color space.
static int interp(int c1, int c2, double frac)
          Interpolate between two color values by the given mixing proportion.
static int red(int color)
          Get the red component of the given color.
static int rgb(int r, int g, int b)
          Get the color code for the given red, green, and blue values.
static int rgba(float r, float g, float b, float a)
          Get the color code for the given red, green, blue, and alpha values as floating point numbers in the range 0-1.0.
static int rgba(int r, int g, int b, int a)
          Get the color code for the given red, green, blue, and alpha values.
static int saturate(int c, float saturation)
          Set the saturation of an input color.
static int setAlpha(int c, int alpha)
          Set the alpha component of the given color.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEX_PREFIX

public static final char HEX_PREFIX
See Also:
Constant Field Values

CATEGORY_HUES

public static final float[] CATEGORY_HUES
Default palette of category hues.


DEFAULT_MAP_SIZE

public static final int DEFAULT_MAP_SIZE
The default length of a color palette if its size is not otherwise specified.

See Also:
Constant Field Values
Constructor Detail

ColorLib

public ColorLib()
Method Detail

rgb

public static int rgb(int r,
                      int g,
                      int b)
Get the color code for the given red, green, and blue values.

Parameters:
r - the red color component (in the range 0-255)
g - the green color component (in the range 0-255)
b - the blue color component (in the range 0-255)
Returns:
the integer color code

gray

public static int gray(int v)
Get the color code for the given grayscale value.

Parameters:
v - the grayscale value (in the range 0-255, 0 is black and 255 is white)
Returns:
the integer color code

gray

public static int gray(int v,
                       int a)
Get the color code for the given grayscale value.

Parameters:
v - the grayscale value (in the range 0-255, 0 is black and 255 is white)
a - the alpha (transparency) value (in the range 0-255)
Returns:
the integer color code

hex

public static int hex(java.lang.String hex)
Parse a hexadecimal String as a color code. The color convention is the same as that used in webpages, with two-decimal hexadecimal numbers representing RGB color values in the range 0-255. A single '#' character may be included at the beginning of the String, but is not required. For example '#000000' is black, 'FFFFFF' is white, '0000FF' is blue, and '#FFFF00' is orange. Color values may also include transparency (alpha) values, ranging from 00 (fully transparent) to FF (fully opaque). If included, alpha values should come first in the string. For example, "#770000FF" is a translucent blue.

Parameters:
hex - the color code value as a hexadecimal String
Returns:
the integer color code for the input String

hsb

public static int hsb(float h,
                      float s,
                      float b)
Get the color code for the given hue, saturation, and brightness values, translating from HSB color space to RGB color space.

Parameters:
h - the hue value (in the range 0-1.0). This represents the actual color hue (blue, green, purple, etc).
s - the saturation value (in the range 0-1.0). This represents "how much" of the color is included. Lower values can result in more grayed out or pastel colors.
b - the brightness value (in the range 0-1.0). This represents how dark or light the color is.
Returns:
the integer color code

hsba

public static int hsba(float h,
                       float s,
                       float b,
                       float a)
Get the color code for the given hue, saturation, and brightness values, translating from HSB color space to RGB color space.

Parameters:
h - the hue value (in the range 0-1.0). This represents the actual color hue (blue, green, purple, etc).
s - the saturation value (in the range 0-1.0). This represents "how much" of the color is included. Lower values can result in more grayed out or pastel colors.
b - the brightness value (in the range 0-1.0). This represents how dark or light the color is.
a - the alpha value (in the range 0-1.0). This represents the transparency of the color.
Returns:
the integer color code

rgba

public static int rgba(int r,
                       int g,
                       int b,
                       int a)
Get the color code for the given red, green, blue, and alpha values.

Parameters:
r - the red color component (in the range 0-255)
g - the green color component (in the range 0-255)
b - the blue color component (in the range 0-255)
a - the alpha (transparency) component (in the range 0-255)
Returns:
the integer color code

rgba

public static int rgba(float r,
                       float g,
                       float b,
                       float a)
Get the color code for the given red, green, blue, and alpha values as floating point numbers in the range 0-1.0.

Parameters:
r - the red color component (in the range 0-1.0)
g - the green color component (in the range 0-1.0)
b - the blue color component (in the range 0-1.0)
a - the alpha (transparency) component (in the range 0-1.0)
Returns:
the integer color code

color

public static int color(java.awt.Color c)
Get the color code for the given Color instance.

Parameters:
c - the Java Color instance
Returns:
the integer color code

red

public static int red(int color)
Get the red component of the given color.

Parameters:
color - the color code
Returns:
the red component of the color (in the range 0-255)

green

public static int green(int color)
Get the green component of the given color.

Parameters:
color - the color code
Returns:
the green component of the color (in the range 0-255)

blue

public static int blue(int color)
Get the blue component of the given color.

Parameters:
color - the color code
Returns:
the blue component of the color (in the range 0-255)

alpha

public static int alpha(int color)
Get the alpha component of the given color.

Parameters:
color - the color code
Returns:
the alpha component of the color (in the range 0-255)

setAlpha

public static int setAlpha(int c,
                           int alpha)
Set the alpha component of the given color.

Parameters:
c - the color code
alpha - the alpha value to set
Returns:
the new color with updated alpha channel

getColor

public static java.awt.Color getColor(float r,
                                      float g,
                                      float b,
                                      float a)
Get a Java Color object for the given red, green, blue, and alpha values as floating point numbers in the range 0-1.0.

Parameters:
r - the red color component (in the range 0-1.0)
g - the green color component (in the range 0-1.0)
b - the blue color component (in the range 0-1.0)
a - the alpha (transparency) component (in the range 0-1.0)
Returns:
a Java Color object

getColor

public static java.awt.Color getColor(float r,
                                      float g,
                                      float b)
Get a Java Color object for the given red, green, and blue values as floating point numbers in the range 0-1.0.

Parameters:
r - the red color component (in the range 0-1.0)
g - the green color component (in the range 0-1.0)
b - the blue color component (in the range 0-1.0)
Returns:
a Java Color object

getColor

public static java.awt.Color getColor(int r,
                                      int g,
                                      int b,
                                      int a)
Get a Java Color object for the given red, green, and blue values.

Parameters:
r - the red color component (in the range 0-255)
g - the green color component (in the range 0-255)
b - the blue color component (in the range 0-255)
a - the alpa (transparency) component (in the range 0-255)
Returns:
a Java Color object

getColor

public static java.awt.Color getColor(int r,
                                      int g,
                                      int b)
Get a Java Color object for the given red, green, and blue values.

Parameters:
r - the red color component (in the range 0-255)
g - the green color component (in the range 0-255)
b - the blue color component (in the range 0-255)
Returns:
a Java Color object

getGrayscale

public static java.awt.Color getGrayscale(int v)
Get a Java Color object for the given grayscale value.

Parameters:
v - the grayscale value (in the range 0-255, 0 is black and 255 is white)
Returns:
a Java Color object

getColor

public static java.awt.Color getColor(int rgba)
Get a Java Color object for the given color code value.

Parameters:
rgba - the integer color code containing red, green, blue, and alpha channel information
Returns:
a Java Color object

getCacheMissCount

public static int getCacheMissCount()
Get the number of cache misses to the Color object cache.

Returns:
the number of cache misses

getCacheLookupCount

public static int getCacheLookupCount()
Get the number of cache lookups to the Color object cache.

Returns:
the number of cache lookups

clearCache

public static void clearCache()
Clear the Color object cache.


interp

public static int interp(int c1,
                         int c2,
                         double frac)
Interpolate between two color values by the given mixing proportion. A mixing fraction of 0 will result in c1, a value of 1.0 will result in c2, and value of 0.5 will result in the color mid-way between the two in RGB color space.

Parameters:
c1 - the starting color
c2 - the target color
frac - a fraction between 0 and 1.0 controlling the interpolation amount.
Returns:
the interpolated color code

darker

public static int darker(int c)
Get a darker shade of an input color.

Parameters:
c - a color code
Returns:
a darkened color code

brighter

public static int brighter(int c)
Get a brighter shade of an input color.

Parameters:
c - a color code
Returns:
a brighter color code

desaturate

public static int desaturate(int c)
Get a desaturated shade of an input color.

Parameters:
c - a color code
Returns:
a desaturated color code

saturate

public static int saturate(int c,
                           float saturation)
Set the saturation of an input color.

Parameters:
c - a color code
saturation - the new sautration value
Returns:
a saturated color code

getCoolPalette

public static int[] getCoolPalette(int size)
Returns a color palette that uses a "cool", blue-heavy color scheme.

Parameters:
size - the size of the color palette
Returns:
the color palette

getCoolPalette

public static int[] getCoolPalette()
Returns a color palette of default size that uses a "cool", blue-heavy color scheme.

Returns:
the color palette

getHotPalette

public static int[] getHotPalette(int size)
Returns a color map that moves from black to red to yellow to white.

Parameters:
size - the size of the color palette
Returns:
the color palette

getHotPalette

public static int[] getHotPalette()
Returns a color map of default size that moves from black to red to yellow to white.

Returns:
the color palette

getCategoryPalette

public static int[] getCategoryPalette(int size,
                                       float s1,
                                       float s2,
                                       float b,
                                       float a)
Returns a color palette of given size tries to provide colors appropriate as category labels. There are 12 basic color hues (red, orange, yellow, olive, green, cyan, blue, purple, magenta, and pink). If the size is greater than 12, these colors will be continually repeated, but with varying saturation levels.

Parameters:
size - the size of the color palette
s1 - the initial saturation to use
s2 - the final (most distant) saturation to use
b - the brightness value to use
a - the alpha value to use

getCategoryPalette

public static int[] getCategoryPalette(int size)
Returns a color palette of given size tries to provide colors appropriate as category labels. There are 12 basic color hues (red, orange, yellow, olive, green, cyan, blue, purple, magenta, and pink). If the size is greater than 12, these colors will be continually repeated, but with varying saturation levels.

Parameters:
size - the size of the color palette

getHSBPalette

public static int[] getHSBPalette(int size,
                                  float s,
                                  float b)
Returns a color palette of given size that cycles through the hues of the HSB (Hue/Saturation/Brightness) color space.

Parameters:
size - the size of the color palette
s - the saturation value to use
b - the brightness value to use
Returns:
the color palette

getHSBPalette

public static int[] getHSBPalette()
Returns a color palette of default size that cycles through the hues of the HSB (Hue/Saturation/Brightness) color space at full saturation and brightness.

Returns:
the color palette

getInterpolatedPalette

public static int[] getInterpolatedPalette(int size,
                                           int c1,
                                           int c2)
Returns a color palette of given size that ranges from one given color to the other.

Parameters:
size - the size of the color palette
c1 - the initial color in the color map
c2 - the final color in the color map
Returns:
the color palette

getInterpolatedPalette

public static int[] getInterpolatedPalette(int c1,
                                           int c2)
Returns a color palette of default size that ranges from one given color to the other.

Parameters:
c1 - the initial color in the color map
c2 - the final color in the color map
Returns:
the color palette

getGrayscalePalette

public static int[] getGrayscalePalette(int size)
Returns a color palette of specified size that ranges from white to black through shades of gray.

Parameters:
size - the size of the color palette
Returns:
the color palette

getGrayscalePalette

public static int[] getGrayscalePalette()
Returns a color palette of default size that ranges from white to black through shades of gray.

Returns:
the color palette


Copyright © 2007 Regents of the University of California