1:
44:
45: package ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53:
54:
59: public abstract class ColorPalette implements Cloneable, Serializable {
60:
61:
62: private static final long serialVersionUID = -9029901853079622051L;
63:
64:
65: protected double minZ = -1;
66:
67:
68: protected double maxZ = -1;
69:
70:
71: protected int[] r;
72:
73:
74: protected int[] g;
75:
76:
77: protected int[] b;
78:
79:
80: protected double[] tickValues = null;
81:
82:
83: protected boolean logscale = false;
84:
85:
86: protected boolean inverse = false;
87:
88:
89: protected String paletteName = null;
90:
91:
92: protected boolean stepped = false;
93:
94:
95: protected static final double log10 = Math.log(10);
96:
97:
100: public ColorPalette() {
101: super();
102: }
103:
104:
111: public Paint getColor(double value) {
112: int izV = (int) (253 * (value - this.minZ)
113: / (this.maxZ - this.minZ)) + 2;
114: return new Color(this.r[izV], this.g[izV], this.b[izV]);
115: }
116:
117:
124: public Color getColor(int izV) {
125: return new Color(this.r[izV], this.g[izV], this.b[izV]);
126: }
127:
128:
135: public Color getColorLinear(double value) {
136: int izV = 0;
137: if (this.stepped) {
138: int index = Arrays.binarySearch(this.tickValues, value);
139: if (index < 0) {
140: index = -1 * index - 2;
141: }
142:
143: if (index < 0) {
144:
145: value = this.minZ;
146: }
147: else {
148: value = this.tickValues[index];
149: }
150: }
151: izV = (int) (253 * (value - this.minZ) / (this.maxZ - this.minZ)) + 2;
152: izV = Math.min(izV, 255);
153: izV = Math.max(izV, 2);
154: return getColor(izV);
155: }
156:
157:
164: public Color getColorLog(double value) {
165: int izV = 0;
166: double minZtmp = this.minZ;
167: double maxZtmp = this.maxZ;
168: if (this.minZ <= 0.0) {
169:
170: this.maxZ = maxZtmp - minZtmp + 1;
171: this.minZ = 1;
172: value = value - minZtmp + 1;
173: }
174: double minZlog = Math.log(this.minZ) / log10;
175: double maxZlog = Math.log(this.maxZ) / log10;
176: value = Math.log(value) / log10;
177:
178: if (this.stepped) {
179: int numSteps = this.tickValues.length;
180: int steps = 256 / (numSteps - 1);
181: izV = steps * (int) (numSteps * (value - minZlog)
182: / (maxZlog - minZlog)) + 2;
183:
184: }
185: else {
186: izV = (int) (253 * (value - minZlog) / (maxZlog - minZlog)) + 2;
187: }
188: izV = Math.min(izV, 255);
189: izV = Math.max(izV, 2);
190:
191: this.minZ = minZtmp;
192: this.maxZ = maxZtmp;
193:
194: return getColor(izV);
195: }
196:
197:
202: public double getMaxZ() {
203: return this.maxZ;
204: }
205:
206:
211: public double getMinZ() {
212: return this.minZ;
213: }
214:
215:
223: public Paint getPaint(double value) {
224: if (isLogscale()) {
225: return getColorLog(value);
226: }
227: else {
228: return getColorLinear(value);
229: }
230: }
231:
232:
237: public String getPaletteName () {
238: return this.paletteName;
239: }
240:
241:
246: public double[] getTickValues() {
247: return this.tickValues;
248: }
249:
250:
253: public abstract void initialize();
254:
255:
258: public void invertPalette() {
259:
260: int[] red = new int[256];
261: int[] green = new int[256];
262: int[] blue = new int[256];
263: for (int i = 0; i < 256; i++) {
264: red[i] = this.r[i];
265: green[i] = this.g[i];
266: blue[i] = this.b[i];
267: }
268:
269: for (int i = 2; i < 256; i++) {
270: this.r[i] = red[257 - i];
271: this.g[i] = green[257 - i];
272: this.b[i] = blue[257 - i];
273: }
274: }
275:
276:
281: public boolean isInverse () {
282: return this.inverse;
283: }
284:
285:
290: public boolean isLogscale() {
291: return this.logscale;
292: }
293:
294:
299: public boolean isStepped () {
300: return this.stepped;
301: }
302:
303:
308: public void setInverse (boolean inverse) {
309: this.inverse = inverse;
310: initialize();
311: if (inverse) {
312: invertPalette();
313: }
314: return;
315: }
316:
317:
322: public void setLogscale(boolean logscale) {
323: this.logscale = logscale;
324: }
325:
326:
331: public void setMaxZ(double newMaxZ) {
332: this.maxZ = newMaxZ;
333: }
334:
335:
340: public void setMinZ(double newMinZ) {
341: this.minZ = newMinZ;
342: }
343:
344:
349: public void setPaletteName (String paletteName) {
350:
351: this.paletteName = paletteName;
352: return;
353: }
354:
355:
360: public void setStepped (boolean stepped) {
361: this.stepped = stepped;
362: return;
363: }
364:
365:
370: public void setTickValues(double[] newTickValues) {
371: this.tickValues = newTickValues;
372: }
373:
374:
379: public void setTickValues(java.util.List ticks) {
380: this.tickValues = new double[ticks.size()];
381: for (int i = 0; i < this.tickValues.length; i++) {
382: this.tickValues[i] = ((ValueTick) ticks.get(i)).getValue();
383: }
384: }
385:
386:
393: public boolean equals(Object o) {
394: if (this == o) {
395: return true;
396: }
397: if (!(o instanceof ColorPalette)) {
398: return false;
399: }
400:
401: ColorPalette colorPalette = (ColorPalette) o;
402:
403: if (this.inverse != colorPalette.inverse) {
404: return false;
405: }
406: if (this.logscale != colorPalette.logscale) {
407: return false;
408: }
409: if (this.maxZ != colorPalette.maxZ) {
410: return false;
411: }
412: if (this.minZ != colorPalette.minZ) {
413: return false;
414: }
415: if (this.stepped != colorPalette.stepped) {
416: return false;
417: }
418: if (!Arrays.equals(this.b, colorPalette.b)) {
419: return false;
420: }
421: if (!Arrays.equals(this.g, colorPalette.g)) {
422: return false;
423: }
424: if (this.paletteName != null
425: ? !this.paletteName.equals(colorPalette.paletteName)
426: : colorPalette.paletteName != null) {
427: return false;
428: }
429: if (!Arrays.equals(this.r, colorPalette.r)) {
430: return false;
431: }
432: if (!Arrays.equals(this.tickValues, colorPalette.tickValues)) {
433: return false;
434: }
435:
436: return true;
437: }
438:
439:
444: public int hashCode() {
445: int result;
446: long temp;
447: temp = Double.doubleToLongBits(this.minZ);
448: result = (int) (temp ^ (temp >>> 32));
449: temp = Double.doubleToLongBits(this.maxZ);
450: result = 29 * result + (int) (temp ^ (temp >>> 32));
451: result = 29 * result + (this.logscale ? 1 : 0);
452: result = 29 * result + (this.inverse ? 1 : 0);
453: result = 29 * result
454: + (this.paletteName != null ? this.paletteName.hashCode() : 0);
455: result = 29 * result + (this.stepped ? 1 : 0);
456: return result;
457: }
458:
459:
466: public Object clone() throws CloneNotSupportedException {
467:
468: ColorPalette clone = (ColorPalette) super.clone();
469: return clone;
470:
471: }
472:
473: }