|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ColorMap.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
package baseCode.gui;
|
|
| 2 |
|
|
| 3 |
import java.awt.Color;
|
|
| 4 |
|
|
| 5 |
/**
|
|
| 6 |
* <p>
|
|
| 7 |
* Title: ColorMap
|
|
| 8 |
* </p>
|
|
| 9 |
* <p>
|
|
| 10 |
* Description: contains predefined color maps for visualization and color palette methods
|
|
| 11 |
* </p>
|
|
| 12 |
* <p>
|
|
| 13 |
* Copyright (c) 2004
|
|
| 14 |
* </p>
|
|
| 15 |
* <p>
|
|
| 16 |
* Institution:: Columbia University
|
|
| 17 |
* </p>
|
|
| 18 |
*
|
|
| 19 |
* @author Will Braynen
|
|
| 20 |
* @version $Id: ColorMap.java,v 1.7 2004/07/27 03:18:58 pavlidis Exp $
|
|
| 21 |
*/
|
|
| 22 |
|
|
| 23 |
public class ColorMap { |
|
| 24 |
|
|
| 25 |
/** first color in the current color map */
|
|
| 26 |
protected Color m_minColor;
|
|
| 27 |
/** last color in the current color map */
|
|
| 28 |
protected Color m_maxColor;
|
|
| 29 |
protected Color[] m_customColorMap;
|
|
| 30 |
|
|
| 31 |
public static final int m_defaultSuggestedNumberOfColors = 64; |
|
| 32 |
public static final Color DARK_RED = new Color( 128, 0, 0 ); |
|
| 33 |
public static final Color[] GREENRED_COLORMAP = { |
|
| 34 |
Color.green, Color.black, Color.red |
|
| 35 |
}; |
|
| 36 |
public static final Color[] REDGREEN_COLORMAP = { |
|
| 37 |
Color.red, Color.black, Color.green |
|
| 38 |
}; |
|
| 39 |
public static final Color[] BLACKBODY_COLORMAP = { |
|
| 40 |
Color.black, DARK_RED, Color.orange, Color.yellow, Color.white |
|
| 41 |
}; |
|
| 42 |
|
|
| 43 |
protected Color[] m_currentColorMap = GREENRED_COLORMAP; // reference to a |
|
| 44 |
// color map
|
|
| 45 |
protected Color[] m_colorPalette;
|
|
| 46 |
|
|
| 47 | 0 |
public ColorMap() {
|
| 48 |
|
|
| 49 | 0 |
this( m_defaultSuggestedNumberOfColors );
|
| 50 |
} |
|
| 51 |
|
|
| 52 | 0 |
public ColorMap( int suggestedNumberOfColors ) { |
| 53 |
|
|
| 54 | 0 |
this( suggestedNumberOfColors, GREENRED_COLORMAP );
|
| 55 |
} |
|
| 56 |
|
|
| 57 | 0 |
public ColorMap( Color[] colorMap ) {
|
| 58 |
|
|
| 59 | 0 |
this( m_defaultSuggestedNumberOfColors, colorMap );
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Pre-condition: suggestedNumberOfColors > colorMap.length
|
|
| 64 |
*
|
|
| 65 |
* @param suggestedNumberOfColors int
|
|
| 66 |
* @param colorMap Color[]
|
|
| 67 |
*/
|
|
| 68 | 0 |
public ColorMap( int suggestedNumberOfColors, Color[] colorMap ) { |
| 69 |
|
|
| 70 | 0 |
m_currentColorMap = colorMap; |
| 71 | 0 |
m_colorPalette = createColorPalette( suggestedNumberOfColors, colorMap ); |
| 72 |
} |
|
| 73 |
|
|
| 74 |
/**
|
|
| 75 |
* Calculate how fast we have to change color components. Assume min and max colors are different!
|
|
| 76 |
*
|
|
| 77 |
* @param minColor red, green, or blue component of the RGB color
|
|
| 78 |
* @param maxColor red, green, or blue component of the RGB color
|
|
| 79 |
* @param totalColors int
|
|
| 80 |
* @return positive or negative step size
|
|
| 81 |
*/
|
|
| 82 | 0 |
protected int getStepSize( int minColor, int maxColor, int totalColors ) { |
| 83 |
|
|
| 84 | 0 |
int colorRange = maxColor - minColor;
|
| 85 | 0 |
double stepSize = colorRange / ( 1 == totalColors ? 1 : totalColors - 1 );
|
| 86 | 0 |
return ( int ) Math.round( stepSize ); |
| 87 |
} |
|
| 88 |
|
|
| 89 |
/**
|
|
| 90 |
* Allocates colors across a range.
|
|
| 91 |
*
|
|
| 92 |
* @param suggestedNumberOfColors palette resolution; if colorPalette.length does not evenly divide into this number,
|
|
| 93 |
* the actual number of colors in the palette will be rounded down.
|
|
| 94 |
* @param colorMap the simplest color map is { minColor, maxColor }; you might, however, want to go through
|
|
| 95 |
* intermediate colors instead of following a straight-line route through the color space.
|
|
| 96 |
* @return Color[] the color palette
|
|
| 97 |
*/
|
|
| 98 | 0 |
protected Color[] createColorPalette( int suggestedNumberOfColors, |
| 99 |
Color[] colorMap ) {
|
|
| 100 |
|
|
| 101 | 0 |
Color[] colorPalette; |
| 102 | 0 |
Color minColor; |
| 103 | 0 |
Color maxColor; |
| 104 |
|
|
| 105 |
// number of segments is one less than the number of points
|
|
| 106 |
// dividing the line into segments; the color map contains points,
|
|
| 107 |
// not segments, so for example, if the color map is trivially
|
|
| 108 |
// { minColor, maxColor }, then there is only one segment
|
|
| 109 | 0 |
int totalSegments = m_currentColorMap.length - 1;
|
| 110 |
|
|
| 111 |
// allocate colors across a range; distribute evenly
|
|
| 112 |
// between intermediate points, if there are any
|
|
| 113 | 0 |
int colorsPerSegment = suggestedNumberOfColors / totalSegments;
|
| 114 |
|
|
| 115 |
// make sure all segments are equal by rounding down
|
|
| 116 |
// the total number of colors if necessary
|
|
| 117 | 0 |
int totalColors = totalSegments * colorsPerSegment;
|
| 118 |
|
|
| 119 |
// create color map to return
|
|
| 120 | 0 |
colorPalette = new Color[totalColors];
|
| 121 |
|
|
| 122 | 0 |
for ( int segment = 0; segment < totalSegments; segment++ ) { |
| 123 |
// the minimum color for each segment as defined by the current color
|
|
| 124 |
// map
|
|
| 125 | 0 |
minColor = colorMap[segment]; |
| 126 | 0 |
int r = minColor.getRed();
|
| 127 | 0 |
int g = minColor.getGreen();
|
| 128 | 0 |
int b = minColor.getBlue();
|
| 129 |
|
|
| 130 |
// the maximum color for each segment and the step sizes
|
|
| 131 | 0 |
maxColor = colorMap[segment + 1]; |
| 132 | 0 |
int redStepSize = getStepSize( r, maxColor.getRed(), colorsPerSegment );
|
| 133 | 0 |
int greenStepSize = getStepSize( g, maxColor.getGreen(),
|
| 134 |
colorsPerSegment ); |
|
| 135 | 0 |
int blueStepSize = getStepSize( b, maxColor.getBlue(),
|
| 136 |
colorsPerSegment ); |
|
| 137 |
|
|
| 138 | 0 |
for ( int k, i = 0; i < colorsPerSegment; i++ ) { |
| 139 |
// clip
|
|
| 140 | 0 |
r = Math.min( r, 255 ); |
| 141 | 0 |
g = Math.min( g, 255 ); |
| 142 | 0 |
b = Math.min( b, 255 ); |
| 143 |
|
|
| 144 |
// but also make sure it's not less than zero
|
|
| 145 | 0 |
r = Math.max( r, 0 ); |
| 146 | 0 |
g = Math.max( g, 0 ); |
| 147 | 0 |
b = Math.max( b, 0 ); |
| 148 |
|
|
| 149 | 0 |
k = segment * colorsPerSegment + i; |
| 150 | 0 |
colorPalette[k] = new Color( r, g, b );
|
| 151 |
|
|
| 152 | 0 |
r += redStepSize; |
| 153 | 0 |
g += greenStepSize; |
| 154 | 0 |
b += blueStepSize; |
| 155 |
} |
|
| 156 |
} |
|
| 157 |
|
|
| 158 | 0 |
return colorPalette;
|
| 159 |
|
|
| 160 |
} // end createColorPalette
|
|
| 161 |
|
|
| 162 | 0 |
public Color getColor( int i ) { |
| 163 |
|
|
| 164 | 0 |
return m_colorPalette[i];
|
| 165 |
} |
|
| 166 |
|
|
| 167 | 0 |
public Color[] getPalette() {
|
| 168 |
|
|
| 169 | 0 |
return m_colorPalette;
|
| 170 |
} |
|
| 171 |
|
|
| 172 |
/**
|
|
| 173 |
* @return the number of colors in the palette
|
|
| 174 |
*/
|
|
| 175 | 0 |
public int getPaletteSize() { |
| 176 |
|
|
| 177 | 0 |
return m_colorPalette.length;
|
| 178 |
} |
|
| 179 |
} |
|
||||||||||