001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------
028: * ColorPalette.java
029: * -----------------
030: * (C) Copyright 2002-2007, by David M. O'Donnell and Contributors.
031: *
032: * Original Author: David M. O'Donnell;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: ColorPalette.java,v 1.1.2.5 2007/04/04 09:05:57 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 26-Nov-2002 : Version 1 contributed by David M. O'Donnell (DG);
040: * 26-Mar-2003 : Implemented Serializable (DG);
041: * 14-Aug-2003 : Implemented Cloneable (DG);
042: * ------------- JFREECHART 1.0.x ---------------------------------------------
043: * 31-Jan-2007 : Deprecated (DG);
044: *
045: */
046:
047: package org.jfree.chart.plot;
048:
049: import java.awt.Color;
050: import java.awt.Paint;
051: import java.io.Serializable;
052: import java.util.Arrays;
053:
054: import org.jfree.chart.axis.ValueTick;
055: import org.jfree.chart.renderer.xy.XYBlockRenderer;
056:
057: /**
058: * Defines palette used by {@link ContourPlot}.
059: *
060: * @deprecated This class is no longer supported (as of version 1.0.4). If
061: * you are creating contour plots, please try to use {@link XYPlot} and
062: * {@link XYBlockRenderer}.
063: */
064: public abstract class ColorPalette implements Cloneable, Serializable {
065:
066: /** For serialization. */
067: private static final long serialVersionUID = -9029901853079622051L;
068:
069: /** The min z-axis value. */
070: protected double minZ = -1;
071:
072: /** The max z-axis value. */
073: protected double maxZ = -1;
074:
075: /** Red components. */
076: protected int[] r;
077:
078: /** Green components. */
079: protected int[] g;
080:
081: /** Blue components. */
082: protected int[] b;
083:
084: /** Tick values are stored for use with stepped palette. */
085: protected double[] tickValues = null;
086:
087: /** Logscale? */
088: protected boolean logscale = false;
089:
090: /** Inverse palette (ie, min and max colors are reversed). */
091: protected boolean inverse = false;
092:
093: /** The palette name. */
094: protected String paletteName = null;
095:
096: /** Controls whether palette colors are stepped (not continuous). */
097: protected boolean stepped = false;
098:
099: /** Constant for converting loge to log10. */
100: protected static final double log10 = Math.log(10);
101:
102: /**
103: * Default contructor.
104: */
105: public ColorPalette() {
106: super ();
107: }
108:
109: /**
110: * Returns the color associated with a value.
111: *
112: * @param value the value.
113: *
114: * @return The color.
115: */
116: public Paint getColor(double value) {
117: int izV = (int) (253 * (value - this .minZ) / (this .maxZ - this .minZ)) + 2;
118: return new Color(this .r[izV], this .g[izV], this .b[izV]);
119: }
120:
121: /**
122: * Returns a color.
123: *
124: * @param izV the index into the palette (zero based).
125: *
126: * @return The color.
127: */
128: public Color getColor(int izV) {
129: return new Color(this .r[izV], this .g[izV], this .b[izV]);
130: }
131:
132: /**
133: * Returns Color by mapping a given value to a linear palette.
134: *
135: * @param value the value.
136: *
137: * @return The color.
138: */
139: public Color getColorLinear(double value) {
140: int izV = 0;
141: if (this .stepped) {
142: int index = Arrays.binarySearch(this .tickValues, value);
143: if (index < 0) {
144: index = -1 * index - 2;
145: }
146:
147: if (index < 0) { // For the case were the first tick is greater
148: // than minZ
149: value = this .minZ;
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: /**
161: * Returns Color by mapping a given value to a common log palette.
162: *
163: * @param value the value.
164: *
165: * @return The color.
166: */
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: // negatives = true;
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: // value = Math.pow(10,value);
181: if (this .stepped) {
182: int numSteps = this .tickValues.length;
183: int steps = 256 / (numSteps - 1);
184: izV = steps
185: * (int) (numSteps * (value - minZlog) / (maxZlog - minZlog))
186: + 2;
187: // izV = steps*numSteps*(int)((value/minZ)/(maxZlog-minZlog)) + 2;
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: /**
201: * Returns the maximum Z value.
202: *
203: * @return The value.
204: */
205: public double getMaxZ() {
206: return this .maxZ;
207: }
208:
209: /**
210: * Returns the minimum Z value.
211: *
212: * @return The value.
213: */
214: public double getMinZ() {
215: return this .minZ;
216: }
217:
218: /**
219: * Returns Paint by mapping a given value to a either a linear or common
220: * log palette as controlled by the value logscale.
221: *
222: * @param value the value.
223: *
224: * @return The paint.
225: */
226: public Paint getPaint(double value) {
227: if (isLogscale()) {
228: return getColorLog(value);
229: } else {
230: return getColorLinear(value);
231: }
232: }
233:
234: /**
235: * Returns the palette name.
236: *
237: * @return The palette name.
238: */
239: public String getPaletteName() {
240: return this .paletteName;
241: }
242:
243: /**
244: * Returns the tick values.
245: *
246: * @return The tick values.
247: */
248: public double[] getTickValues() {
249: return this .tickValues;
250: }
251:
252: /**
253: * Called to initialize the palette's color indexes
254: */
255: public abstract void initialize();
256:
257: /**
258: * Inverts Palette
259: */
260: public void invertPalette() {
261:
262: int[] red = new int[256];
263: int[] green = new int[256];
264: int[] blue = new int[256];
265: for (int i = 0; i < 256; i++) {
266: red[i] = this .r[i];
267: green[i] = this .g[i];
268: blue[i] = this .b[i];
269: }
270:
271: for (int i = 2; i < 256; i++) {
272: this .r[i] = red[257 - i];
273: this .g[i] = green[257 - i];
274: this .b[i] = blue[257 - i];
275: }
276: }
277:
278: /**
279: * Returns the inverse flag.
280: *
281: * @return The flag.
282: */
283: public boolean isInverse() {
284: return this .inverse;
285: }
286:
287: /**
288: * Returns the log-scale flag.
289: *
290: * @return The flag.
291: */
292: public boolean isLogscale() {
293: return this .logscale;
294: }
295:
296: /**
297: * Returns the 'is-stepped' flag.
298: *
299: * @return The flag.
300: */
301: public boolean isStepped() {
302: return this .stepped;
303: }
304:
305: /**
306: * Sets the inverse flag.
307: *
308: * @param inverse the new value.
309: */
310: public void setInverse(boolean inverse) {
311: this .inverse = inverse;
312: initialize();
313: if (inverse) {
314: invertPalette();
315: }
316: return;
317: }
318:
319: /**
320: * Sets the 'log-scale' flag.
321: *
322: * @param logscale the new value.
323: */
324: public void setLogscale(boolean logscale) {
325: this .logscale = logscale;
326: }
327:
328: /**
329: * Sets the maximum Z value.
330: *
331: * @param newMaxZ the new value.
332: */
333: public void setMaxZ(double newMaxZ) {
334: this .maxZ = newMaxZ;
335: }
336:
337: /**
338: * Sets the minimum Z value.
339: *
340: * @param newMinZ the new value.
341: */
342: public void setMinZ(double newMinZ) {
343: this .minZ = newMinZ;
344: }
345:
346: /**
347: * Sets the palette name.
348: *
349: * @param paletteName the name.
350: */
351: public void setPaletteName(String paletteName) {
352: //String oldValue = this.paletteName;
353: this .paletteName = paletteName;
354: return;
355: }
356:
357: /**
358: * Sets the stepped flag.
359: *
360: * @param stepped the flag.
361: */
362: public void setStepped(boolean stepped) {
363: this .stepped = stepped;
364: return;
365: }
366:
367: /**
368: * Sets the tick values.
369: *
370: * @param newTickValues the tick values.
371: */
372: public void setTickValues(double[] newTickValues) {
373: this .tickValues = newTickValues;
374: }
375:
376: /**
377: * Store ticks. Required when doing stepped axis
378: *
379: * @param ticks the ticks.
380: */
381: public void setTickValues(java.util.List ticks) {
382: this .tickValues = new double[ticks.size()];
383: for (int i = 0; i < this .tickValues.length; i++) {
384: this .tickValues[i] = ((ValueTick) ticks.get(i)).getValue();
385: }
386: }
387:
388: /**
389: * Tests an object for equality with this instance.
390: *
391: * @param o the object to test.
392: *
393: * @return A boolean.
394: */
395: public boolean equals(Object o) {
396: if (this == o) {
397: return true;
398: }
399: if (!(o instanceof ColorPalette)) {
400: return false;
401: }
402:
403: ColorPalette colorPalette = (ColorPalette) o;
404:
405: if (this .inverse != colorPalette.inverse) {
406: return false;
407: }
408: if (this .logscale != colorPalette.logscale) {
409: return false;
410: }
411: if (this .maxZ != colorPalette.maxZ) {
412: return false;
413: }
414: if (this .minZ != colorPalette.minZ) {
415: return false;
416: }
417: if (this .stepped != colorPalette.stepped) {
418: return false;
419: }
420: if (!Arrays.equals(this .b, colorPalette.b)) {
421: return false;
422: }
423: if (!Arrays.equals(this .g, colorPalette.g)) {
424: return false;
425: }
426: if (this .paletteName != null ? !this .paletteName
427: .equals(colorPalette.paletteName)
428: : colorPalette.paletteName != null) {
429: return false;
430: }
431: if (!Arrays.equals(this .r, colorPalette.r)) {
432: return false;
433: }
434: if (!Arrays.equals(this .tickValues, colorPalette.tickValues)) {
435: return false;
436: }
437:
438: return true;
439: }
440:
441: /**
442: * Returns a hash code.
443: *
444: * @return A hash code.
445: */
446: public int hashCode() {
447: int result;
448: long temp;
449: temp = Double.doubleToLongBits(this .minZ);
450: result = (int) (temp ^ (temp >>> 32));
451: temp = Double.doubleToLongBits(this .maxZ);
452: result = 29 * result + (int) (temp ^ (temp >>> 32));
453: result = 29 * result + (this .logscale ? 1 : 0);
454: result = 29 * result + (this .inverse ? 1 : 0);
455: result = 29
456: * result
457: + (this .paletteName != null ? this .paletteName
458: .hashCode() : 0);
459: result = 29 * result + (this .stepped ? 1 : 0);
460: return result;
461: }
462:
463: /**
464: * Returns a clone of the palette.
465: *
466: * @return A clone.
467: *
468: * @throws CloneNotSupportedException never.
469: */
470: public Object clone() throws CloneNotSupportedException {
471:
472: ColorPalette clone = (ColorPalette) super.clone();
473: return clone;
474:
475: }
476:
477: }
|