1 43 44 package org.jfree.chart.ui; 45 46 import java.awt.Color ; 47 import java.awt.Paint ; 48 import java.io.Serializable ; 49 import java.util.Arrays ; 50 51 import org.jfree.chart.axis.ValueTick; 52 53 58 public abstract class ColorPalette implements Cloneable , Serializable { 59 60 61 private static final long serialVersionUID = -9029901853079622051L; 62 63 64 protected double minZ = -1; 65 66 67 protected double maxZ = -1; 68 69 70 protected int[] r; 71 72 73 protected int[] g; 74 75 76 protected int[] b; 77 78 79 protected double[] tickValues = null; 80 81 82 protected boolean logscale = false; 83 84 85 protected boolean inverse = false; 86 87 88 protected String paletteName = null; 89 90 91 protected boolean stepped = false; 92 93 94 protected static final double log10 = Math.log(10); 95 96 99 public ColorPalette() { 100 super(); 101 } 102 103 110 public Paint getColor(double value) { 111 int izV = (int) (253 * (value - this.minZ) 112 / (this.maxZ - this.minZ)) + 2; 113 return new Color (this.r[izV], this.g[izV], this.b[izV]); 114 } 115 116 123 public Color getColor(int izV) { 124 return new Color (this.r[izV], this.g[izV], this.b[izV]); 125 } 126 127 134 public Color 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) { 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 163 public Color getColorLog(double value) { 164 int izV = 0; 165 double minZtmp = this.minZ; 166 double maxZtmp = this.maxZ; 167 if (this.minZ <= 0.0) { 168 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 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 } 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 201 public double getMaxZ() { 202 return this.maxZ; 203 } 204 205 210 public double getMinZ() { 211 return this.minZ; 212 } 213 214 222 public Paint getPaint(double value) { 223 if (isLogscale()) { 224 return getColorLog(value); 225 } 226 else { 227 return getColorLinear(value); 228 } 229 } 230 231 236 public String getPaletteName () { 237 return this.paletteName; 238 } 239 240 245 public double[] getTickValues() { 246 return this.tickValues; 247 } 248 249 252 public abstract void initialize(); 253 254 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 280 public boolean isInverse () { 281 return this.inverse; 282 } 283 284 289 public boolean isLogscale() { 290 return this.logscale; 291 } 292 293 298 public boolean isStepped () { 299 return this.stepped; 300 } 301 302 307 public void setInverse (boolean inverse) { 308 this.inverse = inverse; 309 initialize(); 310 if (inverse) { 311 invertPalette(); 312 } 313 return; 314 } 315 316 321 public void setLogscale(boolean logscale) { 322 this.logscale = logscale; 323 } 324 325 330 public void setMaxZ(double newMaxZ) { 331 this.maxZ = newMaxZ; 332 } 333 334 339 public void setMinZ(double newMinZ) { 340 this.minZ = newMinZ; 341 } 342 343 348 public void setPaletteName (String paletteName) { 349 this.paletteName = paletteName; 351 return; 352 } 353 354 359 public void setStepped (boolean stepped) { 360 this.stepped = stepped; 361 return; 362 } 363 364 369 public void setTickValues(double[] newTickValues) { 370 this.tickValues = newTickValues; 371 } 372 373 378 public void setTickValues(java.util.List 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 392 public boolean equals(Object 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 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 465 public Object clone() throws CloneNotSupportedException { 466 467 ColorPalette clone = (ColorPalette) super.clone(); 468 return clone; 469 470 } 471 472 } 473 | Popular Tags |