Package net.sf.basedb.util
Class ColorGenerator
java.lang.Object
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 (to, 11 sep 2008) $
-
Field Summary
Fields Modifier and Type Field Description private int[]
deltaMidMax
The difference of the RGB components of the mid and max colors. [0] = red; [1] = green; [2] = blueprivate int[]
deltaMinMid
The difference of the RGB components of the min and mid colors. [0] = red; [1] = green; [2] = bluestatic float
INVLN2
Constant holding the value of1 / 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] = blueprivate int[]
offsetMin
The RGB components of the min color. [0] = red; [1] = green; [2] = blueprivate 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
Constructors Constructor Description ColorGenerator(Color minColor, Color midColor, Color maxColor, float minValue, float midValue, float maxValue, boolean log)
Create a new color generator. -
Method Summary
-
Field Details
-
INVLN2
public static final float INVLN2Constant holding the value of1 / ln(2)
. It is used to calculate the two-based logarithm of values using the formula:log2(x) = ln(x) / ln(2)
-
minColor
-
midColor
-
maxColor
-
minValue
private float minValue -
midValue
private float midValue -
maxValue
private float maxValue -
log
private boolean log -
offsetMin
private final int[] offsetMinThe RGB components of the min color. [0] = red; [1] = green; [2] = blue -
offsetMid
private final int[] offsetMidThe RGB components of the midpoint color. [0] = red; [1] = green; [2] = blue -
deltaMinMid
private final int[] deltaMinMidThe difference of the RGB components of the min and mid colors. [0] = red; [1] = green; [2] = blue -
deltaMidMax
private final int[] deltaMidMaxThe difference of the RGB components of the mid and max colors. [0] = red; [1] = green; [2] = blue -
rangeMinMid
private final float rangeMinMidThe inverted difference between the mid and min values: 1 / (midValue - minValue) -
rangeMidMax
private final float rangeMidMaxThe inverted difference between the max and mid values: 1 / (maxValue - midValue)
-
-
Constructor Details
-
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 endpointmidColor
- The color for the midpointmaxColor
- The color for the maximum endpointminValue
- The value for the minimum endpointmidValue
- The value for the midpointmaxValue
- The value for the maximum endpointlog
- 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 Details
-
fromHex
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
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
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
-