1:
44:
45: package ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54:
55:
62: public abstract class ColorPalette implements Cloneable, Serializable {
63:
64:
65: private static final long serialVersionUID = -9029901853079622051L;
66:
67:
68: protected double minZ = -1;
69:
70:
71: protected double maxZ = -1;
72:
73:
74: protected int[] r;
75:
76:
77: protected int[] g;
78:
79:
80: protected int[] b;
81:
82:
83: protected double[] tickValues = null;
84:
85:
86: protected boolean logscale = false;
87:
88:
89: protected boolean inverse = false;
90:
91:
92: protected String paletteName = null;
93:
94:
95: protected boolean stepped = false;
96:
97:
98: protected static final double log10 = Math.log(10);
99:
100:
103: public ColorPalette() {
104: super();
105: }
106:
107:
114: public Paint getColor(double value) {
115: int izV = (int) (253 * (value - this.minZ)
116: / (this.maxZ - this.minZ)) + 2;
117: return new Color(this.r[izV], this.g[izV], this.b[izV]);
118: }
119:
120:
127: public Color getColor(int izV) {
128: return new Color(this.r[izV], this.g[izV], this.b[izV]);
129: }
130:
131:
138: public Color getColorLinear(double value) {
139: int izV = 0;
140: if (this.stepped) {
141: int index = Arrays.binarySearch(this.tickValues, value);
142: if (index < 0) {
143: index = -1 * index - 2;
144: }
145:
146: if (index < 0) {
147:
148: value = this.minZ;
149: }
150: else {
151: value = this.tickValues[index];
152: }
153: }
154: izV = (int) (253 * (value - this.minZ) / (this.maxZ - this.minZ)) + 2;
155: izV = Math.min(izV, 255);
156: izV = Math.max(izV, 2);
157: return getColor(izV);
158: }
159:
160:
167: public Color getColorLog(double value) {
168: int izV = 0;
169: double minZtmp = this.minZ;
170: double maxZtmp = this.maxZ;
171: if (this.minZ <= 0.0) {
172:
173: this.maxZ = maxZtmp - minZtmp + 1;
174: this.minZ = 1;
175: value = value - minZtmp + 1;
176: }
177: double minZlog = Math.log(this.minZ) / log10;
178: double maxZlog = Math.log(this.maxZ) / log10;
179: value = Math.log(value) / log10;
180:
181: if (this.stepped) {
182: int numSteps = this.tickValues.length;
183: int steps = 256 / (numSteps - 1);
184: izV = steps * (int) (numSteps * (value - minZlog)
185: / (maxZlog - minZlog)) + 2;
186:
187: }
188: else {
189: izV = (int) (253 * (value - minZlog) / (maxZlog - minZlog)) + 2;
190: }
191: izV = Math.min(izV, 255);
192: izV = Math.max(izV, 2);
193:
194: this.minZ = minZtmp;
195: this.maxZ = maxZtmp;
196:
197: return getColor(izV);
198: }
199:
200:
205: public double getMaxZ() {
206: return this.maxZ;
207: }
208:
209:
214: public double getMinZ() {
215: return this.minZ;
216: }
217:
218:
226: public Paint getPaint(double value) {
227: if (isLogscale()) {
228: return getColorLog(value);
229: }
230: else {
231: return getColorLinear(value);
232: }
233: }
234:
235:
240: public String getPaletteName () {
241: return this.paletteName;
242: }
243:
244:
249: public double[] getTickValues() {
250: return this.tickValues;
251: }
252:
253:
256: public abstract void initialize();
257:
258:
261: public void invertPalette() {
262:
263: int[] red = new int[256];
264: int[] green = new int[256];
265: int[] blue = new int[256];
266: for (int i = 0; i < 256; i++) {
267: red[i] = this.r[i];
268: green[i] = this.g[i];
269: blue[i] = this.b[i];
270: }
271:
272: for (int i = 2; i < 256; i++) {
273: this.r[i] = red[257 - i];
274: this.g[i] = green[257 - i];
275: this.b[i] = blue[257 - i];
276: }
277: }
278:
279:
284: public boolean isInverse () {
285: return this.inverse;
286: }
287:
288:
293: public boolean isLogscale() {
294: return this.logscale;
295: }
296:
297:
302: public boolean isStepped () {
303: return this.stepped;
304: }
305:
306:
311: public void setInverse (boolean inverse) {
312: this.inverse = inverse;
313: initialize();
314: if (inverse) {
315: invertPalette();
316: }
317: return;
318: }
319:
320:
325: public void setLogscale(boolean logscale) {
326: this.logscale = logscale;
327: }
328:
329:
334: public void setMaxZ(double newMaxZ) {
335: this.maxZ = newMaxZ;
336: }
337:
338:
343: public void setMinZ(double newMinZ) {
344: this.minZ = newMinZ;
345: }
346:
347:
352: public void setPaletteName (String paletteName) {
353:
354: this.paletteName = paletteName;
355: return;
356: }
357:
358:
363: public void setStepped (boolean stepped) {
364: this.stepped = stepped;
365: return;
366: }
367:
368:
373: public void setTickValues(double[] newTickValues) {
374: this.tickValues = newTickValues;
375: }
376:
377:
382: public void setTickValues(java.util.List ticks) {
383: this.tickValues = new double[ticks.size()];
384: for (int i = 0; i < this.tickValues.length; i++) {
385: this.tickValues[i] = ((ValueTick) ticks.get(i)).getValue();
386: }
387: }
388:
389:
396: public boolean equals(Object o) {
397: if (this == o) {
398: return true;
399: }
400: if (!(o instanceof ColorPalette)) {
401: return false;
402: }
403:
404: ColorPalette colorPalette = (ColorPalette) o;
405:
406: if (this.inverse != colorPalette.inverse) {
407: return false;
408: }
409: if (this.logscale != colorPalette.logscale) {
410: return false;
411: }
412: if (this.maxZ != colorPalette.maxZ) {
413: return false;
414: }
415: if (this.minZ != colorPalette.minZ) {
416: return false;
417: }
418: if (this.stepped != colorPalette.stepped) {
419: return false;
420: }
421: if (!Arrays.equals(this.b, colorPalette.b)) {
422: return false;
423: }
424: if (!Arrays.equals(this.g, colorPalette.g)) {
425: return false;
426: }
427: if (this.paletteName != null
428: ? !this.paletteName.equals(colorPalette.paletteName)
429: : colorPalette.paletteName != null) {
430: return false;
431: }
432: if (!Arrays.equals(this.r, colorPalette.r)) {
433: return false;
434: }
435: if (!Arrays.equals(this.tickValues, colorPalette.tickValues)) {
436: return false;
437: }
438:
439: return true;
440: }
441:
442:
447: public int hashCode() {
448: int result;
449: long temp;
450: temp = Double.doubleToLongBits(this.minZ);
451: result = (int) (temp ^ (temp >>> 32));
452: temp = Double.doubleToLongBits(this.maxZ);
453: result = 29 * result + (int) (temp ^ (temp >>> 32));
454: result = 29 * result + (this.logscale ? 1 : 0);
455: result = 29 * result + (this.inverse ? 1 : 0);
456: result = 29 * result
457: + (this.paletteName != null ? this.paletteName.hashCode() : 0);
458: result = 29 * result + (this.stepped ? 1 : 0);
459: return result;
460: }
461:
462:
469: public Object clone() throws CloneNotSupportedException {
470:
471: ColorPalette clone = (ColorPalette) super.clone();
472: return clone;
473:
474: }
475:
476: }