2.17.2: 2011-06-17

net.sf.basedb.util
Class ColorGenerator

java.lang.Object
  extended by net.sf.basedb.util.ColorGenerator

public class ColorGenerator
extends Object

This class is used to generate colors for numerical values. Given a numeric range mith a start, mid and max value and one color for each of these values you can create new colors for values within the specified range.

Example:
We have the range and colors: -1 (BLUE), 0 (WHITE) , +1 (YELLOW) The value -0.5 is converted to a lighter blue and the value +0.5 is converted to a ligther yellow. Values falling outside the min or max values are assigned the endpoint colors. In this case, BLUE and YELLOW.

Version:
2.0
Author:
nicklas
Last modified
$Date: 2008-09-11 22:08:14 +0200 (Thu, 11 Sep 2008) $

Field Summary
private  int[] deltaMidMax
          The difference of the RGB components of the mid and max colors. [0] = red; [1] = green; [2] = blue
private  int[] deltaMinMid
          The difference of the RGB components of the min and mid colors. [0] = red; [1] = green; [2] = blue
static float INVLN2
          Constant holding the value of 1 / ln(2).
private  boolean log
           
private  Color maxColor
           
private  float maxValue
           
private  Color midColor
           
private  float midValue
           
private  Color minColor
           
private  float minValue
           
private  int[] offsetMid
          The RGB components of the midpoint color. [0] = red; [1] = green; [2] = blue
private  int[] offsetMin
          The RGB components of the min color. [0] = red; [1] = green; [2] = blue
private  float rangeMidMax
          The inverted difference between the max and mid values: 1 / (maxValue - midValue)
private  float rangeMinMid
          The inverted difference between the mid and min values: 1 / (midValue - minValue)
 
Constructor Summary
ColorGenerator(Color minColor, Color midColor, Color maxColor, float minValue, float midValue, float maxValue, boolean log)
          Create a new color generator.
 
Method Summary
static Color fromHex(String hexRGB)
          Convert a hexadecimal color string such as one used in HTML to a Color object.
 Color getColor(float value)
          Get the color for the value.
static String toHex(Color color)
          Generate a hex color string from a Color object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INVLN2

public static final float INVLN2
Constant holding the value of 1 / ln(2). It is used to calculate the two-based logarithm of values using the formula: log2(x) = ln(x) / ln(2)


minColor

private Color minColor

midColor

private Color midColor

maxColor

private Color maxColor

minValue

private float minValue

midValue

private float midValue

maxValue

private float maxValue

log

private boolean log

offsetMin

private final int[] offsetMin
The RGB components of the min color. [0] = red; [1] = green; [2] = blue


offsetMid

private final int[] offsetMid
The RGB components of the midpoint color. [0] = red; [1] = green; [2] = blue


deltaMinMid

private final int[] deltaMinMid
The difference of the RGB components of the min and mid colors. [0] = red; [1] = green; [2] = blue


deltaMidMax

private final int[] deltaMidMax
The difference of the RGB components of the mid and max colors. [0] = red; [1] = green; [2] = blue


rangeMinMid

private final float rangeMinMid
The inverted difference between the mid and min values: 1 / (midValue - minValue)


rangeMidMax

private final float rangeMidMax
The inverted difference between the max and mid values: 1 / (maxValue - midValue)

Constructor Detail

ColorGenerator

public ColorGenerator(Color minColor,
                      Color midColor,
                      Color maxColor,
                      float minValue,
                      float midValue,
                      float maxValue,
                      boolean log)
Create a new color generator.

Parameters:
minColor - The color for the minimum endpoint
midColor - The color for the midpoint
maxColor - The color for the maximum endpoint
minValue - The value for the minimum endpoint
midValue - The value for the midpoint
maxValue - The value for the maximum endpoint
log - If values should be logarithmised before beeing assigned a color or not. Note the values in this constructor are not affected by this setting.
Throws:
InvalidDataException - If not the minValue <= midValue <= maxValue
Method Detail

fromHex

public static Color fromHex(String hexRGB)
Convert a hexadecimal color string such as one used in HTML to a Color object. The hexadecimal string must have 6 characters in the range 0-9 or A-F (case ignored).

Parameters:
hexRGB - The hexadecimal color string
Returns:
A Color object
Throws:
InvalidDataException - If the string cannot be converted

toHex

public static String toHex(Color color)
Generate a hex color string from a Color object. Useful for outputting into HTML pages.

Parameters:
color - The color object
Returns:
The hexadecimal representation of the color

getColor

public Color getColor(float value)
Get the color for the value. If the log flag was true when creating the generator the value is first logarithmised. Note that negative values can't be logarithmised so null is returned in those cases.

If the value is lower than the min endpoint the min endpoint color is returned. If the value is higher than the max endpoint the max endpoint color is returned. Otherwise a new color is calculated based on the difference between the value and the given points.

Parameters:
value - The value
Returns:
A color object or null if no color could be generated

2.17.2: 2011-06-17