Revision 1853 org.gvsig.legend.heatmap/trunk/org.gvsig.legend.heatmap/org.gvsig.legend.heatmap.lib/org.gvsig.legend.heatmap.lib.impl/src/main/java/org/gvsig/legend/heatmap/lib/impl/DefaultHeatmapLegend.java
DefaultHeatmapLegend.java | ||
---|---|---|
5 | 5 |
import java.awt.Image; |
6 | 6 |
import java.awt.image.BufferedImage; |
7 | 7 |
import java.util.Map; |
8 |
|
|
8 | 9 |
import org.apache.commons.lang3.StringUtils; |
9 | 10 |
import org.cresques.cts.ICoordTrans; |
11 |
|
|
10 | 12 |
import org.gvsig.fmap.dal.exception.DataException; |
11 | 13 |
import org.gvsig.fmap.dal.feature.Feature; |
12 | 14 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
... | ... | |
121 | 123 |
double value = this.grid[x][y]; |
122 | 124 |
if( value > 0 ) { |
123 | 125 |
int icolor = (int) (value * maxIndexColor / maxValue); |
124 |
// icolor = icolor < 0 ? 0 : icolor >= maxIndexColor ? maxIndexColor - 1 : icolor; |
|
125 | 126 |
c = toolsSwingManager.alphaBlendingWithOpaqueBackground( |
126 | 127 |
new Color(img.getRGB(x, y)), |
127 | 128 |
colorTable[icolor] |
... | ... | |
134 | 135 |
LOG.warn("Problems drawing heatmap", ex); |
135 | 136 |
} |
136 | 137 |
} |
137 |
|
|
138 |
private Color blend(Color dst, Color src) { |
|
139 |
// |
|
140 |
// https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending |
|
141 |
// |
|
142 |
// when the destination background is opaque |
|
143 |
// |
|
144 |
// double outa = 1; |
|
145 |
double srca = src.getAlpha() / 255.0; |
|
146 |
double srca_1 = (1 - srca); |
|
147 |
|
|
148 |
Color color = new Color( |
|
149 |
(int)(src.getRed() * srca + dst.getRed() * srca_1) & 0xff, |
|
150 |
(int)(src.getGreen()* srca + dst.getGreen()* srca_1) & 0xff, |
|
151 |
(int)(src.getBlue() * srca + dst.getBlue() * srca_1) & 0xff |
|
152 |
); |
|
153 |
return color; |
|
154 |
} |
|
155 |
|
|
156 | 138 |
} |
157 | 139 |
|
158 | 140 |
private ISymbol defaultSymbol; |
... | ... | |
165 | 147 |
private String fieldName; |
166 | 148 |
private Image imageLegend; |
167 | 149 |
private HeatmapColorTable hmColorTable; |
150 |
private Color rampColdColor; |
|
151 |
private Color rampHotColor; |
|
152 |
private int rampNumColors; |
|
168 | 153 |
|
169 | 154 |
public DefaultHeatmapLegend() { |
170 | 155 |
this.defaultSymbol = new SimpleTextSymbol(); |
... | ... | |
234 | 219 |
this.algorithm.init(image.getWidth(), image.getHeight()); |
235 | 220 |
super.draw(image, g, viewPort, cancel, scale, queryParameters, coordTrans, featureStore, featureQuery, dpi); |
236 | 221 |
if( !cancel.isCanceled() ) { |
237 |
// |
|
238 |
// Para PACO, despues borralo. |
|
239 |
// |
|
240 |
// Probablemente aqui habria que llamar a getHeatMapColorTable() |
|
241 |
// |
|
242 |
// |
|
243 |
hmColorTable = null; |
|
244 |
if( this.useRamp() || !useAlphaInColorTable ) { |
|
245 |
hmColorTable = new HeatmapColorTable(this.getSourceColorTable()); |
|
246 |
} else { |
|
247 |
hmColorTable = new HeatmapColorTable(this.getSourceColorTable(),colorTableColdColorAlpha, colorTableHotColorAlpha); |
|
248 |
} |
|
249 |
this.algorithm.draw(image, g, hmColorTable.getColorTable(), cancel); |
|
222 |
this.algorithm.draw(image, g, this.getHeatMapColorTable().getColorTable(), cancel); |
|
250 | 223 |
} |
251 | 224 |
} |
252 | 225 |
|
... | ... | |
325 | 298 |
} |
326 | 299 |
|
327 | 300 |
@Override |
328 |
public void setColorTable(int numColor, Color first, Color last) { |
|
329 |
// |
|
330 |
// Para PACO, despues borralo. |
|
331 |
// |
|
332 |
// Probablemente aqui sobra la creacion de la tabla de color |
|
333 |
// y solo se deberian guardar los dos colores y numColor en propiedades |
|
334 |
// de la instancia. |
|
335 |
// De hacer eso, estaria bien a?adir esas propiedades al interface de la |
|
336 |
// leyenda para que las use el GUI de esta. |
|
337 |
// |
|
338 |
Color[] table = new Color[numColor]; |
|
339 |
|
|
340 |
double deltaRed = (last.getRed() - first.getRed()) / numColor; |
|
341 |
double deltaGreen = (last.getGreen() - first.getGreen()) / numColor; |
|
342 |
double deltaBlue = (last.getBlue() - first.getBlue()) / numColor; |
|
343 |
double deltaAlpha = (last.getAlpha() - first.getAlpha()) / numColor; |
|
344 |
for( int i = 0; i < numColor; i++ ) { |
|
345 |
table[i] = new Color( |
|
346 |
(int) (first.getRed() + i * deltaRed), |
|
347 |
(int) (first.getGreen() + i * deltaGreen), |
|
348 |
(int) (first.getBlue() + i * deltaBlue), |
|
349 |
(int) (first.getAlpha() + i * deltaAlpha) |
|
350 |
); |
|
351 |
} |
|
301 |
public void setColorTable(int numColors, Color coldColor, Color hotColor) { |
|
352 | 302 |
this.isRamp = true; |
353 |
this.sourceColorTable = table; |
|
303 |
this.rampColdColor = coldColor; |
|
304 |
this.rampHotColor = hotColor; |
|
305 |
this.rampNumColors = numColors; |
|
354 | 306 |
this.imageLegend = null; |
355 | 307 |
this.hmColorTable = null; |
356 | 308 |
this.fireDefaultSymbolChangedEvent(new SymbolLegendEvent(null,null)); |
... | ... | |
368 | 320 |
|
369 | 321 |
private HeatmapColorTable getHeatMapColorTable() { |
370 | 322 |
if (this.hmColorTable == null) { |
371 |
if (this.useRamp() || !useAlphaInColorTable) { |
|
372 |
// |
|
373 |
// Para PACO, despues borralo. |
|
374 |
// |
|
375 |
// Creo que aqui queris llamar a: |
|
376 |
// public HeatmapColorTable(Color coldColor, Color hotColor, int coldAlpha, int hotAlpha, int length){ |
|
377 |
// y en setColorTable con un gradiente guardarte los colores de inicio y fin |
|
378 |
// y el numero de colores. |
|
379 |
// Si es asi habria que a?adir esos tres valores tambien a la descripcion de |
|
380 |
// persistencia (el xml) y en el load y save state. |
|
381 |
// |
|
382 |
this.hmColorTable = new HeatmapColorTable(this.getSourceColorTable()); |
|
323 |
if (this.useRamp()) { |
|
324 |
this.hmColorTable = new HeatmapColorTable(this.rampColdColor, this.rampHotColor, this.rampNumColors); |
|
383 | 325 |
} else { |
326 |
if(useAlphaInColorTable) { |
|
327 |
this.hmColorTable = new HeatmapColorTable(this.getSourceColorTable(), colorTableColdColorAlpha, colorTableHotColorAlpha); |
|
328 |
} else { |
|
384 | 329 |
this.hmColorTable = |
385 |
new HeatmapColorTable(this.getSourceColorTable(), colorTableColdColorAlpha, colorTableHotColorAlpha); |
|
330 |
this.hmColorTable = new HeatmapColorTable(this.getSourceColorTable()); |
|
331 |
} |
|
386 | 332 |
} |
387 | 333 |
} |
388 | 334 |
return this.hmColorTable; |
... | ... | |
413 | 359 |
public void setColorTableHotColorAlpha(int colorTableHotColorAlpha) { |
414 | 360 |
this.colorTableHotColorAlpha = colorTableHotColorAlpha; |
415 | 361 |
this.imageLegend = null; |
362 |
this.hmColorTable = null; |
|
416 | 363 |
} |
417 | 364 |
|
418 | 365 |
@Override |
... | ... | |
424 | 371 |
public void setColorTableColdColorAlpha(int colorTableColdColorAlpha) { |
425 | 372 |
this.colorTableColdColorAlpha = colorTableColdColorAlpha; |
426 | 373 |
this.imageLegend = null; |
374 |
this.hmColorTable = null; |
|
427 | 375 |
} |
428 | 376 |
|
429 | 377 |
@Override |
... | ... | |
435 | 383 |
public boolean setUseAlphaInColorTable(boolean use) { |
436 | 384 |
boolean x = this.useAlphaInColorTable; |
437 | 385 |
this.useAlphaInColorTable = use; |
386 |
this.hmColorTable = null; |
|
387 |
|
|
438 | 388 |
return x; |
439 | 389 |
} |
440 | 390 |
|
... | ... | |
471 | 421 |
this.colorTableColdColorAlpha = state.getInt("colorTableColdColorAlpha"); |
472 | 422 |
this.useAlphaInColorTable = state.getBoolean("useAlphaInColorTable"); |
473 | 423 |
this.fieldName = state.getString("fieldName"); |
424 |
this.rampNumColors = state.getInt("rampNumColors"); |
|
425 |
this.rampColdColor = (Color)state.get("rampColdColor"); |
|
426 |
this.rampHotColor = (Color)state.get("rampHotColor"); |
|
474 | 427 |
|
475 | 428 |
this.algorithm = new DensityAlgorithm(state.getInt("distance")); |
476 | 429 |
} |
477 | 430 |
|
478 | 431 |
@Override |
432 |
public int getRampNumColors() { |
|
433 |
return this.rampNumColors; |
|
434 |
} |
|
435 |
|
|
436 |
@Override |
|
437 |
public Color getRampColdColor() { |
|
438 |
return this.rampColdColor; |
|
439 |
} |
|
440 |
|
|
441 |
@Override |
|
442 |
public Color getRampHotColor() { |
|
443 |
return this.rampHotColor; |
|
444 |
} |
|
445 |
|
|
446 |
@Override |
|
479 | 447 |
public void saveToState(PersistentState state) throws PersistenceException { |
480 | 448 |
super.saveToState(state); |
481 | 449 |
state.set("isRamp", isRamp); |
... | ... | |
485 | 453 |
state.set("useAlphaInColorTable", useAlphaInColorTable); |
486 | 454 |
state.set("fieldName", fieldName); |
487 | 455 |
state.set("distance", algorithm.distance); |
456 |
|
|
457 |
state.set("rampNumColors", rampNumColors); |
|
458 |
state.set("rampColdColor", rampColdColor); |
|
459 |
state.set("rampHotColor", rampHotColor); |
|
488 | 460 |
} |
489 |
|
|
490 |
|
|
461 |
|
|
462 |
|
|
491 | 463 |
private static class HeatmapColorTable { |
492 | 464 |
|
493 | 465 |
private Color[] sourceColorTable = null; |
... | ... | |
500 | 472 |
|
501 | 473 |
public HeatmapColorTable(Color[] sourceColorTable){ |
502 | 474 |
this.sourceColorTable = sourceColorTable; |
503 |
this.length = sourceColorTable.length; |
|
504 | 475 |
} |
505 | 476 |
|
506 | 477 |
public HeatmapColorTable(Color[] sourceColorTable, int coldAlpha, int hotAlpha){ |
507 | 478 |
this.sourceColorTable = sourceColorTable; |
508 | 479 |
this.coldAlpha = coldAlpha; |
509 | 480 |
this.hotAlpha = hotAlpha; |
510 |
this.length = sourceColorTable.length; |
|
511 | 481 |
} |
512 | 482 |
|
513 |
public HeatmapColorTable(Color coldColor, Color hotColor, int coldAlpha, int hotAlpha, int length){
|
|
483 |
public HeatmapColorTable(Color coldColor, Color hotColor, int length){ |
|
514 | 484 |
this.coldColor = coldColor; |
515 | 485 |
this.hotColor = hotColor; |
516 |
this.coldAlpha = coldAlpha; |
|
517 |
this.hotAlpha = hotAlpha; |
|
518 | 486 |
this.length = length; |
519 | 487 |
} |
520 | 488 |
|
521 | 489 |
public Color[] getColorTable(){ |
522 | 490 |
if(targetColorTable==null){ |
523 | 491 |
if(sourceColorTable!=null){ //Tenemos tabla de color |
524 |
double alphaDelta = getDelta(coldAlpha, hotAlpha, length);
|
|
525 |
targetColorTable = new Color[sourceColorTable.length];
|
|
526 |
for (int i = 0; i < sourceColorTable.length; i++) {
|
|
527 |
Color sourceColor = sourceColorTable[i];
|
|
528 |
if (coldAlpha >= 0 && hotAlpha >= 0) {
|
|
529 |
targetColorTable[i] =
|
|
530 |
new Color(sourceColor.getRed(),
|
|
531 |
sourceColor.getGreen(),
|
|
532 |
sourceColor.getBlue(),
|
|
533 |
coldAlpha + (int) (i * alphaDelta));
|
|
534 |
} else {
|
|
535 |
targetColorTable[i] = sourceColor;
|
|
492 |
if (coldAlpha >= 0 || hotAlpha >= 0) { //Se usa alpha para la tabla de color
|
|
493 |
double alphaDelta = getDelta(coldAlpha, hotAlpha, sourceColorTable.length);
|
|
494 |
targetColorTable = new Color[sourceColorTable.length];
|
|
495 |
for (int i = 0; i < sourceColorTable.length; i++) {
|
|
496 |
Color sourceColor = sourceColorTable[i];
|
|
497 |
if (coldAlpha >= 0 && hotAlpha >= 0) {
|
|
498 |
targetColorTable[i] =
|
|
499 |
new Color(sourceColor.getRed(), sourceColor.getGreen(), sourceColor.getBlue(),
|
|
500 |
coldAlpha + (int) (i * alphaDelta));
|
|
501 |
} else {
|
|
502 |
targetColorTable[i] = sourceColor;
|
|
503 |
}
|
|
536 | 504 |
} |
505 |
} else { //No se usa alpha para la tabla de color |
|
506 |
targetColorTable = sourceColorTable; |
|
537 | 507 |
} |
538 |
} else { //Tenemos gradiente |
|
539 |
int coldRed = coldColor.getRed(); |
|
540 |
int coldGreen = coldColor.getGreen(); |
|
541 |
int hotGreen = hotColor.getGreen(); |
|
542 |
|
|
543 |
int hotRed = hotColor.getRed(); |
|
544 |
int coldBlue = coldColor.getBlue(); |
|
545 |
int hotBlue = hotColor.getBlue(); |
|
546 |
|
|
547 |
double redDelta = getDelta(coldRed, hotRed, length); |
|
548 |
double greenDelta = getDelta(coldGreen, hotGreen, length); |
|
549 |
double blueDelta = getDelta(coldBlue, hotBlue, length); |
|
550 |
double alphaDelta = getDelta(coldAlpha, hotAlpha, length); |
|
508 |
} else { // Tenemos gradiente |
|
551 | 509 |
targetColorTable = new Color[length]; |
552 |
for (int i = 0; i < sourceColorTable.length; i++) { |
|
510 |
|
|
511 |
double deltaRed = (hotColor.getRed() - coldColor.getRed()) / length; |
|
512 |
double deltaGreen = (hotColor.getGreen() - coldColor.getGreen()) / length; |
|
513 |
double deltaBlue = (hotColor.getBlue() - coldColor.getBlue()) / length; |
|
514 |
double deltaAlpha = (hotColor.getAlpha() - coldColor.getAlpha()) / length; |
|
515 |
for (int i = 0; i < length; i++) { |
|
553 | 516 |
targetColorTable[i] = |
554 |
new Color(coldRed + (int) (i * redDelta), |
|
555 |
coldGreen + (int) (i * greenDelta), |
|
556 |
hotGreen + (int) (i * blueDelta), |
|
557 |
coldAlpha + (int) (i * alphaDelta)); |
|
517 |
new Color( |
|
518 |
(int) (coldColor.getRed() + i * deltaRed), |
|
519 |
(int) (coldColor.getGreen() + i * deltaGreen), |
|
520 |
(int) (coldColor.getBlue() + i * deltaBlue), |
|
521 |
(int) (coldColor.getAlpha() + i * deltaAlpha)); |
|
558 | 522 |
} |
559 | 523 |
} |
560 | 524 |
} |
... | ... | |
565 | 529 |
return (x2-x1)/((double)lenght-1); |
566 | 530 |
} |
567 | 531 |
} |
568 |
|
|
569 | 532 |
} |
Also available in: Unified diff