104 |
104 |
}
|
105 |
105 |
}
|
106 |
106 |
|
107 |
|
public void draw(BufferedImage img, Graphics2D g, Color[] colorTable, int alphaHot, int alphaCold, Cancellable cancel) {
|
|
107 |
public void draw(BufferedImage img, Graphics2D g, HeatmapColorTable hmColorTable, Cancellable cancel) {
|
108 |
108 |
try {
|
109 |
109 |
Color c;
|
110 |
|
int maxColors = colorTable.length;
|
111 |
|
double deltaAlpha = (alphaHot - alphaCold) / maxColors;
|
|
110 |
int maxIndexColor = hmColorTable.length-1;
|
112 |
111 |
for( int x = 0; x < with; x++ ) {
|
113 |
112 |
for( int y = 0; y < height; y++ ) {
|
114 |
113 |
if( cancel.isCanceled() ) {
|
... | ... | |
116 |
115 |
}
|
117 |
116 |
double value = this.grid[x][y];
|
118 |
117 |
if( value > 0 ) {
|
119 |
|
int icolor = (int) (value * maxColors / maxValue);
|
120 |
|
icolor = icolor < 0 ? 0 : icolor >= maxColors ? maxColors - 1 : icolor;
|
121 |
|
if( alphaHot > 0 ) {
|
122 |
|
c = colorTable[icolor];
|
123 |
|
c = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (icolor * deltaAlpha) & 0xff);
|
124 |
|
} else {
|
125 |
|
c = colorTable[icolor];
|
126 |
|
}
|
127 |
|
c = blend(new Color(img.getRGB(x, y)),c);
|
|
118 |
int icolor = (int) (value * maxIndexColor / maxValue);
|
|
119 |
// icolor = icolor < 0 ? 0 : icolor >= maxIndexColor ? maxIndexColor - 1 : icolor;
|
|
120 |
c = blend(new Color(img.getRGB(x, y)),hmColorTable.getColorTable()[icolor]);
|
128 |
121 |
img.setRGB(x, y, c.getRGB());
|
129 |
122 |
}
|
130 |
123 |
}
|
... | ... | |
143 |
136 |
// double outa = 1;
|
144 |
137 |
double srca = src.getAlpha() / 255.0;
|
145 |
138 |
double srca_1 = (1 - srca);
|
146 |
|
|
|
139 |
|
147 |
140 |
Color color = new Color(
|
148 |
141 |
(int)(src.getRed() * srca + dst.getRed() * srca_1) & 0xff,
|
149 |
142 |
(int)(src.getGreen()* srca + dst.getGreen()* srca_1) & 0xff,
|
... | ... | |
157 |
150 |
private final ISymbol defaultSymbol;
|
158 |
151 |
private final DensityAlgorithm algorithm;
|
159 |
152 |
private boolean isRamp;
|
160 |
|
private Color[] colorTable;
|
|
153 |
private Color[] sourceColorTable;
|
161 |
154 |
private int colorTableHotColorAlpha;
|
162 |
155 |
private int colorTableColdColorAlpha;
|
163 |
156 |
private String fieldName;
|
164 |
157 |
private boolean useAlphaInColorTable;
|
165 |
158 |
private Image imageLegend;
|
166 |
|
|
|
159 |
private HeatmapColorTable hmColorTable;
|
|
160 |
|
167 |
161 |
public DefaultHeatmapLegend() {
|
168 |
162 |
this.defaultSymbol = new SimpleTextSymbol();
|
169 |
163 |
this.algorithm = new DensityAlgorithm(30);
|
170 |
164 |
this.colorTableHotColorAlpha = 255;
|
171 |
165 |
this.colorTableColdColorAlpha = 0;
|
172 |
166 |
this.useAlphaInColorTable = false;
|
173 |
|
this.setColorTable(100, new Color(0, 0, 255, 1), new Color(255, 0, 0, 255));
|
|
167 |
this.setColorTable(100, new Color(0, 0, 255, 0), new Color(255, 0, 0, 255));
|
174 |
168 |
this.imageLegend = null;
|
175 |
169 |
}
|
176 |
170 |
|
... | ... | |
230 |
224 |
this.algorithm.init(image.getWidth(), image.getHeight());
|
231 |
225 |
super.draw(image, g, viewPort, cancel, scale, queryParameters, coordTrans, featureStore, featureQuery, dpi);
|
232 |
226 |
if( !cancel.isCanceled() ) {
|
|
227 |
hmColorTable = null;
|
233 |
228 |
if( this.useRamp() || !useAlphaInColorTable ) {
|
234 |
|
this.algorithm.draw(image, g, this.getColorTable(), -1, -1, cancel);
|
|
229 |
hmColorTable = new HeatmapColorTable(this.getSourceColorTable());
|
235 |
230 |
} else {
|
236 |
|
this.algorithm.draw(image, g, this.getColorTable(), colorTableHotColorAlpha, colorTableColdColorAlpha, cancel);
|
|
231 |
hmColorTable = new HeatmapColorTable(this.getSourceColorTable(),colorTableColdColorAlpha, colorTableHotColorAlpha);
|
237 |
232 |
}
|
|
233 |
this.algorithm.draw(image, g, hmColorTable, cancel);
|
238 |
234 |
}
|
239 |
235 |
}
|
240 |
236 |
|
... | ... | |
274 |
270 |
double value = 0;
|
275 |
271 |
try {
|
276 |
272 |
value = feature.getDouble(n);
|
277 |
|
} catch(Exception ex) {
|
|
273 |
} catch(Exception ex) {
|
278 |
274 |
}
|
279 |
275 |
if( value >0 ) {
|
280 |
276 |
algorithm.add((int) pointPixels.getX(), (int) pointPixels.getY(), value);
|
... | ... | |
306 |
302 |
@Override
|
307 |
303 |
public void setColorTable(Color[] colorTable) {
|
308 |
304 |
this.isRamp = false;
|
309 |
|
this.colorTable = colorTable;
|
|
305 |
this.sourceColorTable = colorTable;
|
310 |
306 |
this.imageLegend = null;
|
311 |
307 |
this.fireDefaultSymbolChangedEvent(new SymbolLegendEvent(null,null));
|
312 |
308 |
}
|
... | ... | |
328 |
324 |
);
|
329 |
325 |
}
|
330 |
326 |
this.isRamp = true;
|
331 |
|
this.colorTable = table;
|
|
327 |
this.sourceColorTable = table;
|
332 |
328 |
this.imageLegend = null;
|
333 |
329 |
this.fireDefaultSymbolChangedEvent(new SymbolLegendEvent(null,null));
|
334 |
330 |
}
|
335 |
331 |
|
336 |
332 |
@Override
|
337 |
|
public Color[] getColorTable() {
|
338 |
|
return this.colorTable;
|
|
333 |
public Color[] getSourceColorTable() {
|
|
334 |
return this.sourceColorTable;
|
339 |
335 |
}
|
340 |
336 |
|
341 |
337 |
@Override
|
|
338 |
public Color[] getTargetColorTable() {
|
|
339 |
return this.getHetMapColorTable().getColorTable();
|
|
340 |
}
|
|
341 |
|
|
342 |
private HeatmapColorTable getHetMapColorTable() {
|
|
343 |
if (this.hmColorTable == null) {
|
|
344 |
if (this.useRamp() || !useAlphaInColorTable) {
|
|
345 |
this.hmColorTable = new HeatmapColorTable(this.getSourceColorTable());
|
|
346 |
} else {
|
|
347 |
this.hmColorTable =
|
|
348 |
new HeatmapColorTable(this.getSourceColorTable(), colorTableColdColorAlpha, colorTableHotColorAlpha);
|
|
349 |
}
|
|
350 |
}
|
|
351 |
return this.hmColorTable;
|
|
352 |
}
|
|
353 |
|
|
354 |
|
|
355 |
@Override
|
342 |
356 |
public boolean useRamp() {
|
343 |
357 |
return this.isRamp;
|
344 |
358 |
}
|
... | ... | |
361 |
375 |
@Override
|
362 |
376 |
public void setColorTableHotColorAlpha(int colorTableHotColorAlpha) {
|
363 |
377 |
this.colorTableHotColorAlpha = colorTableHotColorAlpha;
|
|
378 |
this.imageLegend = null;
|
364 |
379 |
}
|
365 |
380 |
|
366 |
381 |
@Override
|
... | ... | |
371 |
386 |
@Override
|
372 |
387 |
public void setColorTableColdColorAlpha(int colorTableColdColorAlpha) {
|
373 |
388 |
this.colorTableColdColorAlpha = colorTableColdColorAlpha;
|
|
389 |
this.imageLegend = null;
|
374 |
390 |
}
|
375 |
391 |
|
376 |
392 |
@Override
|
... | ... | |
389 |
405 |
public Image getImageLegend() {
|
390 |
406 |
if( this.imageLegend==null ) {
|
391 |
407 |
BufferedImage img = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
|
392 |
|
ColorTablePainter painter = new DefaultColorTablePainter(this.colorTable,"");
|
|
408 |
ColorTablePainter painter = new DefaultColorTablePainter(this.getTargetColorTable(),"");
|
393 |
409 |
Graphics2D g = img.createGraphics();
|
394 |
|
g.setClip(0, 0, 150, 20);
|
|
410 |
g.setClip(0, 0, 80, 20);
|
|
411 |
g.setBackground(Color.WHITE);
|
|
412 |
g.fillRect(0, 0, 80, 20);
|
395 |
413 |
painter.paint(g, false);
|
396 |
414 |
this.imageLegend = img;
|
397 |
415 |
}
|
... | ... | |
403 |
421 |
return null;
|
404 |
422 |
}
|
405 |
423 |
|
|
424 |
private static class HeatmapColorTable {
|
|
425 |
|
|
426 |
private Color[] sourceColorTable = null;
|
|
427 |
private int coldAlpha = -1;
|
|
428 |
private int hotAlpha = -1;
|
|
429 |
private Color coldColor = null;
|
|
430 |
private Color hotColor = null;
|
|
431 |
private Color[] targetColorTable = null;
|
|
432 |
private int length = -1;
|
|
433 |
|
|
434 |
public HeatmapColorTable(Color[] sourceColorTable){
|
|
435 |
this.sourceColorTable = sourceColorTable;
|
|
436 |
this.length = sourceColorTable.length;
|
|
437 |
}
|
|
438 |
|
|
439 |
public HeatmapColorTable(Color[] sourceColorTable, int coldAlpha, int hotAlpha){
|
|
440 |
this.sourceColorTable = sourceColorTable;
|
|
441 |
this.coldAlpha = coldAlpha;
|
|
442 |
this.hotAlpha = hotAlpha;
|
|
443 |
this.length = sourceColorTable.length;
|
|
444 |
}
|
|
445 |
|
|
446 |
public HeatmapColorTable(Color coldColor, Color hotColor, int coldAlpha, int hotAlpha, int length){
|
|
447 |
this.coldColor = coldColor;
|
|
448 |
this.hotColor = hotColor;
|
|
449 |
this.coldAlpha = coldAlpha;
|
|
450 |
this.hotAlpha = hotAlpha;
|
|
451 |
this.length = length;
|
|
452 |
}
|
|
453 |
|
|
454 |
public Color[] getColorTable(){
|
|
455 |
if(targetColorTable==null){
|
|
456 |
if(sourceColorTable!=null){ //Tenemos tabla de color
|
|
457 |
double alphaDelta = getDelta(coldAlpha, hotAlpha, length);
|
|
458 |
targetColorTable = new Color[sourceColorTable.length];
|
|
459 |
for (int i = 0; i < sourceColorTable.length; i++) {
|
|
460 |
Color sourceColor = sourceColorTable[i];
|
|
461 |
if (coldAlpha >= 0 && hotAlpha >= 0) {
|
|
462 |
targetColorTable[i] =
|
|
463 |
new Color(sourceColor.getRed(),
|
|
464 |
sourceColor.getGreen(),
|
|
465 |
sourceColor.getBlue(),
|
|
466 |
coldAlpha + (int) (i * alphaDelta));
|
|
467 |
} else {
|
|
468 |
targetColorTable[i] = sourceColor;
|
|
469 |
}
|
|
470 |
}
|
|
471 |
} else { //Tenemos gradiente
|
|
472 |
int coldRed = coldColor.getRed();
|
|
473 |
int coldGreen = coldColor.getGreen();
|
|
474 |
int hotGreen = hotColor.getGreen();
|
|
475 |
|
|
476 |
int hotRed = hotColor.getRed();
|
|
477 |
int coldBlue = coldColor.getBlue();
|
|
478 |
int hotBlue = hotColor.getBlue();
|
|
479 |
|
|
480 |
double redDelta = getDelta(coldRed, hotRed, length);
|
|
481 |
double greenDelta = getDelta(coldGreen, hotGreen, length);
|
|
482 |
double blueDelta = getDelta(coldBlue, hotBlue, length);
|
|
483 |
double alphaDelta = getDelta(coldAlpha, hotAlpha, length);
|
|
484 |
targetColorTable = new Color[length];
|
|
485 |
for (int i = 0; i < sourceColorTable.length; i++) {
|
|
486 |
targetColorTable[i] =
|
|
487 |
new Color(coldRed + (int) (i * redDelta),
|
|
488 |
coldGreen + (int) (i * greenDelta),
|
|
489 |
hotGreen + (int) (i * blueDelta),
|
|
490 |
coldAlpha + (int) (i * alphaDelta));
|
|
491 |
}
|
|
492 |
}
|
|
493 |
}
|
|
494 |
return targetColorTable;
|
|
495 |
}
|
|
496 |
|
|
497 |
private double getDelta(int x1, int x2, int lenght){
|
|
498 |
return (x2-x1)/((double)lenght-1);
|
|
499 |
}
|
|
500 |
}
|
|
501 |
|
406 |
502 |
}
|