KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ColorMap


1 package prefuse.util;
2
3 /**
4  * A color map provides a mapping from numeric values to specific colors.
5  * This useful for assigning colors to visualized items. The numeric values
6  * may represent different categories (i.e. nominal variables) or run along
7  * a spectrum of values (i.e. quantitative variables).
8  *
9  * @author <a HREF="http://jheer.org">jeffrey heer</a>
10  */

11 public class ColorMap {
12
13     private int[] palette;
14     private double minValue, maxValue;
15     
16     /**
17      * Creates a new ColorMap instance using the given internal color map
18      * array and minimum and maximum index values.
19      * @param map the color palette, an int array of color values
20      * @param min the minimum value in the color map
21      * @param max the maximum value in the color map
22      */

23     public ColorMap(int[] map, double min, double max) {
24         palette = map;
25         minValue = min;
26         maxValue = max;
27     }
28     
29     /**
30      * Returns the color associated with the given value. If the value
31      * is outside the range defined by this map's minimum or maximum
32      * values, a endpoint value is returned (i.e. the first entry
33      * in the color map for values below the minimum, the last enty
34      * for value above the maximum).
35      * @param val the value for which to retrieve the color
36      * @return the color corresponding the given value
37      */

38     public int getColor(double val) {
39         if ( val < minValue ) {
40             return palette[0];
41         } else if ( val >= maxValue ) {
42             return palette[palette.length-1];
43         } else {
44             int idx = (int)(palette.length *
45                             (val-minValue)/(maxValue-minValue));
46             return palette[idx];
47         }
48     }
49
50     /**
51      * Gets the internal color palette, an int array of color values.
52      * @return returns the color palette.
53      */

54     public int[] getColorPalette() {
55         return palette;
56     }
57
58     /**
59      * Sets the internal color palette, an int array of color values.
60      * @param palette the new palette.
61      */

62     public void setColorPalette(int[] palette) {
63         this.palette = palette;
64     }
65
66     /**
67      * Gets the maximum value that corresponds to the last
68      * color in the color map.
69      * @return returns the max index value into the color map.
70      */

71     public double getMaxValue() {
72         return maxValue;
73     }
74
75     /**
76      * Sets the maximum value that corresponds to the last
77      * color in the color map.
78      * @param maxValue the new max index value.
79      */

80     public void setMaxValue(double maxValue) {
81         this.maxValue = maxValue;
82     }
83
84     /**
85      * Gets the minimum value that corresponds to the first
86      * color in the color map.
87      * @return Returns the min index value.
88      */

89     public double getMinValue() {
90         return minValue;
91     }
92
93     /**
94      * Sets the minimum value that corresponds to the first
95      * color in the color map.
96      * @param minValue the new min index value.
97      */

98     public void setMinValue(double minValue) {
99         this.minValue = minValue;
100     }
101     
102 } // end of class ColorMap
103
Popular Tags