KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > ui > ColorPalette


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------------
27  * ColorPalette.java
28  * -----------------
29  * (C) Copyright 2002-2005, by David M. O'Donnell and Contributors.
30  *
31  * Original Author: David M. O'Donnell;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: ColorPalette.java,v 1.4 2005/05/19 14:01:47 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 26-Nov-2002 : Version 1 contributed by David M. O'Donnell (DG);
39  * 26-Mar-2003 : Implemented Serializable (DG);
40  * 14-Aug-2003 : Implemented Cloneable (DG);
41  *
42  */

43
44 package org.jfree.chart.ui;
45
46 import java.awt.Color JavaDoc;
47 import java.awt.Paint JavaDoc;
48 import java.io.Serializable JavaDoc;
49 import java.util.Arrays JavaDoc;
50
51 import org.jfree.chart.axis.ValueTick;
52
53 /**
54  * Defines palette used in Contour Plots.
55  *
56  * @author David M. O'Donnell.
57  */

58 public abstract class ColorPalette implements Cloneable JavaDoc, Serializable JavaDoc {
59
60     /** For serialization. */
61     private static final long serialVersionUID = -9029901853079622051L;
62     
63     /** The min z-axis value. */
64     protected double minZ = -1;
65
66     /** The max z-axis value. */
67     protected double maxZ = -1;
68
69     /** Red components. */
70     protected int[] r;
71
72     /** Green components. */
73     protected int[] g;
74
75     /** Blue components. */
76     protected int[] b;
77
78     /** Tick values are stored for use with stepped palette. */
79     protected double[] tickValues = null;
80
81     /** Logscale? */
82     protected boolean logscale = false;
83
84     /** Inverse palette (ie, min and max colors are reversed). */
85     protected boolean inverse = false;
86
87     /** The palette name. */
88     protected String JavaDoc paletteName = null;
89
90     /** Controls whether palette colors are stepped (not continuous). */
91     protected boolean stepped = false;
92
93     /** Constant for converting loge to log10. */
94     protected static final double log10 = Math.log(10);
95     
96     /**
97      * Default contructor.
98      */

99     public ColorPalette() {
100         super();
101     }
102
103     /**
104      * Returns the color associated with a value.
105      *
106      * @param value the value.
107      *
108      * @return The color.
109      */

110     public Paint JavaDoc getColor(double value) {
111         int izV = (int) (253 * (value - this.minZ)
112                     / (this.maxZ - this.minZ)) + 2;
113         return new Color JavaDoc(this.r[izV], this.g[izV], this.b[izV]);
114     }
115
116     /**
117      * Returns a color.
118      *
119      * @param izV ??.
120      *
121      * @return The color.
122      */

123     public Color JavaDoc getColor(int izV) {
124         return new Color JavaDoc(this.r[izV], this.g[izV], this.b[izV]);
125     }
126
127     /**
128      * Returns Color by mapping a given value to a linear palette.
129      *
130      * @param value the value.
131      *
132      * @return The color.
133      */

134     public Color JavaDoc getColorLinear(double value) {
135         int izV = 0;
136         if (this.stepped) {
137             int index = Arrays.binarySearch(this.tickValues, value);
138             if (index < 0) {
139                 index = -1 * index - 2;
140             }
141
142             if (index < 0) { // For the case were the first tick is greater
143
// than minZ
144
value = this.minZ;
145             }
146             else {
147                 value = this.tickValues[index];
148             }
149         }
150         izV = (int) (253 * (value - this.minZ) / (this.maxZ - this.minZ)) + 2;
151         izV = Math.min(izV, 255);
152         izV = Math.max(izV, 2);
153         return getColor(izV);
154     }
155
156     /**
157      * Returns Color by mapping a given value to a common log palette.
158      *
159      * @param value the value.
160      *
161      * @return The color.
162      */

163     public Color JavaDoc getColorLog(double value) {
164         int izV = 0;
165         double minZtmp = this.minZ;
166         double maxZtmp = this.maxZ;
167         if (this.minZ <= 0.0) {
168 // negatives = true;
169
this.maxZ = maxZtmp - minZtmp + 1;
170             this.minZ = 1;
171             value = value - minZtmp + 1;
172         }
173         double minZlog = Math.log(this.minZ) / log10;
174         double maxZlog = Math.log(this.maxZ) / log10;
175         value = Math.log(value) / log10;
176         // value = Math.pow(10,value);
177
if (this.stepped) {
178             int numSteps = this.tickValues.length;
179             int steps = 256 / (numSteps - 1);
180             izV = steps * (int) (numSteps * (value - minZlog)
181                     / (maxZlog - minZlog)) + 2;
182             // izV = steps*numSteps*(int)((value/minZ)/(maxZlog-minZlog)) + 2;
183
}
184         else {
185             izV = (int) (253 * (value - minZlog) / (maxZlog - minZlog)) + 2;
186         }
187         izV = Math.min(izV, 255);
188         izV = Math.max(izV, 2);
189
190         this.minZ = minZtmp;
191         this.maxZ = maxZtmp;
192
193         return getColor(izV);
194     }
195
196     /**
197      * Returns the maximum Z value.
198      *
199      * @return The value.
200      */

201     public double getMaxZ() {
202         return this.maxZ;
203     }
204
205     /**
206      * Returns the minimum Z value.
207      *
208      * @return The value.
209      */

210     public double getMinZ() {
211         return this.minZ;
212     }
213
214     /**
215      * Returns Paint by mapping a given value to a either a linear or common
216      * log palette as controlled by the value logscale.
217      *
218      * @param value the value.
219      *
220      * @return The paint.
221      */

222     public Paint JavaDoc getPaint(double value) {
223         if (isLogscale()) {
224             return getColorLog(value);
225         }
226         else {
227             return getColorLinear(value);
228         }
229     }
230
231     /**
232      * Returns the palette name.
233      *
234      * @return The palette name.
235      */

236     public String JavaDoc getPaletteName () {
237         return this.paletteName;
238     }
239
240     /**
241      * Returns the tick values.
242      *
243      * @return The tick values.
244      */

245     public double[] getTickValues() {
246         return this.tickValues;
247     }
248
249     /**
250      * Called to initialize the palette's color indexes
251      */

252     public abstract void initialize();
253
254     /**
255      * Inverts Palette
256      */

257     public void invertPalette() {
258
259         int[] red = new int[256];
260         int[] green = new int[256];
261         int[] blue = new int[256];
262         for (int i = 0; i < 256; i++) {
263             red[i] = this.r[i];
264             green[i] = this.g[i];
265             blue[i] = this.b[i];
266         }
267
268         for (int i = 2; i < 256; i++) {
269             this.r[i] = red[257 - i];
270             this.g[i] = green[257 - i];
271             this.b[i] = blue[257 - i];
272         }
273     }
274
275     /**
276      * Returns the inverse flag.
277      *
278      * @return The flag.
279      */

280     public boolean isInverse () {
281         return this.inverse;
282     }
283
284     /**
285      * Returns the log-scale flag.
286      *
287      * @return The flag.
288      */

289     public boolean isLogscale() {
290         return this.logscale;
291     }
292
293     /**
294      * Returns the 'is-stepped' flag.
295      *
296      * @return The flag.
297      */

298     public boolean isStepped () {
299         return this.stepped;
300     }
301
302     /**
303      * Sets the inverse flag.
304      *
305      * @param inverse the new value.
306      */

307     public void setInverse (boolean inverse) {
308         this.inverse = inverse;
309         initialize();
310         if (inverse) {
311             invertPalette();
312         }
313         return;
314     }
315
316     /**
317      * Sets the 'log-scale' flag.
318      *
319      * @param logscale the new value.
320      */

321     public void setLogscale(boolean logscale) {
322         this.logscale = logscale;
323     }
324
325     /**
326      * Sets the maximum Z value.
327      *
328      * @param newMaxZ the new value.
329      */

330     public void setMaxZ(double newMaxZ) {
331         this.maxZ = newMaxZ;
332     }
333
334     /**
335      * Sets the minimum Z value.
336      *
337      * @param newMinZ the new value.
338      */

339     public void setMinZ(double newMinZ) {
340         this.minZ = newMinZ;
341     }
342
343     /**
344      * Sets the palette name.
345      *
346      * @param paletteName the name.
347      */

348     public void setPaletteName (String JavaDoc paletteName) {
349         //String oldValue = this.paletteName;
350
this.paletteName = paletteName;
351         return;
352     }
353
354     /**
355      * Sets the stepped flag.
356      *
357      * @param stepped the flag.
358      */

359     public void setStepped (boolean stepped) {
360         this.stepped = stepped;
361         return;
362     }
363
364     /**
365      * Sets the tick values.
366      *
367      * @param newTickValues the tick values.
368      */

369     public void setTickValues(double[] newTickValues) {
370         this.tickValues = newTickValues;
371     }
372
373     /**
374      * Store ticks. Required when doing stepped axis
375      *
376      * @param ticks the ticks.
377      */

378     public void setTickValues(java.util.List JavaDoc ticks) {
379         this.tickValues = new double[ticks.size()];
380         for (int i = 0; i < this.tickValues.length; i++) {
381             this.tickValues[i] = ((ValueTick) ticks.get(i)).getValue();
382         }
383     }
384
385     /**
386      * Tests an object for equality with this instance.
387      *
388      * @param o the object to test.
389      *
390      * @return A boolean.
391      */

392     public boolean equals(Object JavaDoc o) {
393         if (this == o) {
394             return true;
395         }
396         if (!(o instanceof ColorPalette)) {
397             return false;
398         }
399
400         ColorPalette colorPalette = (ColorPalette) o;
401
402         if (this.inverse != colorPalette.inverse) {
403             return false;
404         }
405         if (this.logscale != colorPalette.logscale) {
406             return false;
407         }
408         if (this.maxZ != colorPalette.maxZ) {
409             return false;
410         }
411         if (this.minZ != colorPalette.minZ) {
412             return false;
413         }
414         if (this.stepped != colorPalette.stepped) {
415             return false;
416         }
417         if (!Arrays.equals(this.b, colorPalette.b)) {
418             return false;
419         }
420         if (!Arrays.equals(this.g, colorPalette.g)) {
421             return false;
422         }
423         if (this.paletteName != null
424                 ? !this.paletteName.equals(colorPalette.paletteName)
425                 : colorPalette.paletteName != null) {
426             return false;
427         }
428         if (!Arrays.equals(this.r, colorPalette.r)) {
429             return false;
430         }
431         if (!Arrays.equals(this.tickValues, colorPalette.tickValues)) {
432             return false;
433         }
434
435         return true;
436     }
437
438     /**
439      * Returns a hash code.
440      *
441      * @return A hash code.
442      */

443     public int hashCode() {
444         int result;
445         long temp;
446         temp = Double.doubleToLongBits(this.minZ);
447         result = (int) (temp ^ (temp >>> 32));
448         temp = Double.doubleToLongBits(this.maxZ);
449         result = 29 * result + (int) (temp ^ (temp >>> 32));
450         result = 29 * result + (this.logscale ? 1 : 0);
451         result = 29 * result + (this.inverse ? 1 : 0);
452         result = 29 * result
453                  + (this.paletteName != null ? this.paletteName.hashCode() : 0);
454         result = 29 * result + (this.stepped ? 1 : 0);
455         return result;
456     }
457
458     /**
459      * Returns a clone of the palette.
460      *
461      * @return A clone.
462      *
463      * @throws CloneNotSupportedException never.
464      */

465     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
466         
467         ColorPalette clone = (ColorPalette) super.clone();
468         return clone;
469         
470     }
471
472 }
473
Popular Tags