001: package prefuse.util;
002:
003: /**
004: * A color map provides a mapping from numeric values to specific colors.
005: * This useful for assigning colors to visualized items. The numeric values
006: * may represent different categories (i.e. nominal variables) or run along
007: * a spectrum of values (i.e. quantitative variables).
008: *
009: * @author <a href="http://jheer.org">jeffrey heer</a>
010: */
011: public class ColorMap {
012:
013: private int[] palette;
014: private double minValue, maxValue;
015:
016: /**
017: * Creates a new ColorMap instance using the given internal color map
018: * array and minimum and maximum index values.
019: * @param map the color palette, an int array of color values
020: * @param min the minimum value in the color map
021: * @param max the maximum value in the color map
022: */
023: public ColorMap(int[] map, double min, double max) {
024: palette = map;
025: minValue = min;
026: maxValue = max;
027: }
028:
029: /**
030: * Returns the color associated with the given value. If the value
031: * is outside the range defined by this map's minimum or maximum
032: * values, a endpoint value is returned (i.e. the first entry
033: * in the color map for values below the minimum, the last enty
034: * for value above the maximum).
035: * @param val the value for which to retrieve the color
036: * @return the color corresponding the given value
037: */
038: public int getColor(double val) {
039: if (val < minValue) {
040: return palette[0];
041: } else if (val >= maxValue) {
042: return palette[palette.length - 1];
043: } else {
044: int idx = (int) (palette.length * (val - minValue) / (maxValue - minValue));
045: return palette[idx];
046: }
047: }
048:
049: /**
050: * Gets the internal color palette, an int array of color values.
051: * @return returns the color palette.
052: */
053: public int[] getColorPalette() {
054: return palette;
055: }
056:
057: /**
058: * Sets the internal color palette, an int array of color values.
059: * @param palette the new palette.
060: */
061: public void setColorPalette(int[] palette) {
062: this .palette = palette;
063: }
064:
065: /**
066: * Gets the maximum value that corresponds to the last
067: * color in the color map.
068: * @return returns the max index value into the color map.
069: */
070: public double getMaxValue() {
071: return maxValue;
072: }
073:
074: /**
075: * Sets the maximum value that corresponds to the last
076: * color in the color map.
077: * @param maxValue the new max index value.
078: */
079: public void setMaxValue(double maxValue) {
080: this .maxValue = maxValue;
081: }
082:
083: /**
084: * Gets the minimum value that corresponds to the first
085: * color in the color map.
086: * @return Returns the min index value.
087: */
088: public double getMinValue() {
089: return minValue;
090: }
091:
092: /**
093: * Sets the minimum value that corresponds to the first
094: * color in the color map.
095: * @param minValue the new min index value.
096: */
097: public void setMinValue(double minValue) {
098: this .minValue = minValue;
099: }
100:
101: } // end of class ColorMap
|