1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.graphics;
20
21 import java.awt.Color;
22
23
24
25
26
27 public class ColorMap {
28 public static final Color DARK_RED = new Color( 128, 0, 0 );
29
30 public static final Color[] BLACKBODY_COLORMAP = { Color.black, DARK_RED, Color.orange, Color.yellow, Color.white };
31 public static final Color[] GREENRED_COLORMAP = { Color.green, Color.black, Color.red };
32
33 public static final int m_defaultSuggestedNumberOfColors = 32;
34 public static final Color[] REDGREEN_COLORMAP = { Color.red, Color.black, Color.green };
35
36 protected Color[] m_colorPalette;
37 protected Color[] m_currentColorMap = GREENRED_COLORMAP;
38 protected Color[] m_customColorMap;
39
40
41 protected Color m_maxColor;
42
43 protected Color m_minColor;
44
45 public ColorMap() {
46
47 this( m_defaultSuggestedNumberOfColors );
48 }
49
50 public ColorMap( Color[] colorMap ) {
51
52 this( m_defaultSuggestedNumberOfColors, colorMap );
53 }
54
55 public ColorMap( int suggestedNumberOfColors ) {
56
57 this( suggestedNumberOfColors, GREENRED_COLORMAP );
58 }
59
60
61
62
63
64
65
66 public ColorMap( int suggestedNumberOfColors, Color[] colorMap ) {
67
68 m_currentColorMap = colorMap;
69 m_colorPalette = createColorPalette( suggestedNumberOfColors, colorMap );
70 }
71
72 public Color getColor( int i ) {
73
74 return m_colorPalette[i];
75 }
76
77 public Color[] getPalette() {
78
79 return m_colorPalette;
80 }
81
82
83
84
85 public int getPaletteSize() {
86
87 return m_colorPalette.length;
88 }
89
90
91
92
93
94
95
96
97
98
99 protected Color[] createColorPalette( int suggestedNumberOfColors, Color[] colorMap ) {
100
101 Color[] colorPalette;
102 Color minColor;
103 Color maxColor;
104
105
106
107
108
109 int totalSegments = m_currentColorMap.length - 1;
110
111
112
113 int colorsPerSegment = suggestedNumberOfColors / totalSegments;
114
115
116
117 int totalColors = totalSegments * colorsPerSegment;
118
119
120 colorPalette = new Color[totalColors];
121
122 for ( int segment = 0; segment < totalSegments; segment++ ) {
123
124
125 minColor = colorMap[segment];
126 int r = minColor.getRed();
127 int g = minColor.getGreen();
128 int b = minColor.getBlue();
129
130
131 maxColor = colorMap[segment + 1];
132 int redStepSize = getStepSize( r, maxColor.getRed(), colorsPerSegment );
133 int greenStepSize = getStepSize( g, maxColor.getGreen(), colorsPerSegment );
134 int blueStepSize = getStepSize( b, maxColor.getBlue(), colorsPerSegment );
135
136 for ( int k, i = 0; i < colorsPerSegment; i++ ) {
137
138 r = Math.min( r, 255 );
139 g = Math.min( g, 255 );
140 b = Math.min( b, 255 );
141
142
143 r = Math.max( r, 0 );
144 g = Math.max( g, 0 );
145 b = Math.max( b, 0 );
146
147 k = segment * colorsPerSegment + i;
148 colorPalette[k] = new Color( r, g, b );
149
150 r += redStepSize;
151 g += greenStepSize;
152 b += blueStepSize;
153 }
154 }
155
156 return colorPalette;
157
158 }
159
160
161
162
163
164
165
166
167
168 protected int getStepSize( int minColor, int maxColor, int totalColors ) {
169
170 int colorRange = maxColor - minColor;
171 double stepSize = colorRange / ( double ) ( 1 == totalColors ? 1 : totalColors - 1 );
172 return ( int ) Math.round( stepSize );
173 }
174 }