Revision 1853

View differences:

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
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
}
org.gvsig.legend.heatmap/trunk/org.gvsig.legend.heatmap/org.gvsig.legend.heatmap.lib/org.gvsig.legend.heatmap.lib.impl/src/main/resources/org/gvsig/legend/heatmap/lib/impl/DefaultHeatmapLegend.persistence.xml
28 28
        <field name="fieldName" type="String" defaultValue="">
29 29
          <description></description>
30 30
        </field>
31
        <field name="rampColdColor" type="Object" classOfValue="java.awt.Color">
32
          <description></description>
33
        </field>
34
        <field name="rampHotColor" type="Object" classOfValue="java.awt.Color">
35
          <description></description>
36
        </field>
37
        <field name="rampNumColors" type="integer" defaultValue="100">
38
          <description></description>
39
        </field>
31 40
      </fields>
32 41
    </class>
33 42

  
34 43
  </classes>
35
</definitions>  
44
</definitions>
org.gvsig.legend.heatmap/trunk/org.gvsig.legend.heatmap/org.gvsig.legend.heatmap.lib/org.gvsig.legend.heatmap.lib.api/src/main/java/org/gvsig/legend/heatmap/lib/api/HeatmapLegend.java
11 11

  
12 12
    public void setColorTable(Color[] colorTable);
13 13

  
14
    public void setColorTable(int numColor, Color first, Color last);
14
    public void setColorTable(int numColors, Color coldColor, Color hotColor);
15 15

  
16 16
    public Color[] getSourceColorTable();
17 17

  
......
34 34
    public boolean useAlphaInColorTable();
35 35

  
36 36
    public boolean setUseAlphaInColorTable(boolean use);
37

  
38
    public int getRampNumColors();
39

  
40
    public Color getRampColdColor();
41

  
42
    public Color getRampHotColor();
43

  
37 44
}
org.gvsig.legend.heatmap/trunk/org.gvsig.legend.heatmap/org.gvsig.legend.heatmap.swing/org.gvsig.legend.heatmap.swing.impl/src/main/java/org/gvsig/legend/heatmap/swing/impl/DefaultHeatmapLegendEditor.java
71 71
        });
72 72
        translateAll();
73 73
    }
74
    
75
    private void translateAll() {    
74

  
75
    private void translateAll() {
76 76
        this.translate(this.lblDistance);
77 77
        this.translate(this.lblPixels);
78 78
        this.translate(this.rdbUseColorRamp);
......
80 80
        this.translate(this.lblHotColor);
81 81
        this.translate(this.lblHotColorAlpha);
82 82
        this.translate(this.lblColdColor);
83
        this.translate(this.lblHotColorAlpha);
83
        this.translate(this.lblColdColorAlpha);
84 84
        this.translate(this.rdbUseColorTable);
85 85
        this.translate(this.lblColorTable);
86 86
        this.translate(this.chkColorTableUseAlpha);
......
88 88
        this.translate(this.lblColdColorTransparency);
89 89
        this.translate(this.chkUseField);
90 90
    }
91
    
91

  
92 92
    private void translate(AbstractButton component) {
93 93
        I18nManager i18n = ToolsLocator.getI18nManager();
94 94
        component.setText(i18n.getTranslation(component.getText()));
95 95
    }
96
    
96

  
97 97
    private void translate(JLabel component) {
98 98
        I18nManager i18n = ToolsLocator.getI18nManager();
99 99
        component.setText(i18n.getTranslation(component.getText()));
100 100
    }
101
    
101

  
102 102
    @Override
103 103
    public void setData(FLayer layer, ILegend legend) {
104 104
        if( layer == this.layer && legend == this.legend ) {
......
118 118
        }
119 119
        if( this.legend.useAlphaInColorTable() ) {
120 120
            this.chkColorTableUseAlpha.setSelected(true);
121
            this.coldColorChooser.set(this.legend.getRampColdColor());
122
            this.hotColorChooser.set(this.legend.getRampHotColor());
123
            this.spnNumberOfColors.setValue(this.legend.getRampNumColors());
124

  
121 125
        } else {
122 126
            this.chkColorTableUseAlpha.setSelected(false);
123 127
        }
124 128
        this.sldColorTableColdAlpha.setValue(this.legend.getColorTableColdColorAlpha());
125 129
        this.sldColorTableHotAlpha.setValue(this.legend.getColorTableHotColorAlpha());
126 130

  
131

  
127 132
        this.chkUseField.setEnabled(false);
128 133
        this.chkUseField.setSelected(false);
129 134
        this.cboFields.setEnabled(false);
......
160 165
            this.chkColorTableUseAlpha.setEnabled(false);
161 166
            this.sldColorTableColdAlpha.setEnabled(false);
162 167
            this.sldColorTableHotAlpha.setEnabled(false);
163
            Color[] table = this.legend.getSourceColorTable();
164
            this.spnNumberOfColors.setValue(table.length);
165
            this.coldColorChooser.set(table[0]);
166
            this.hotColorChooser.set(table[table.length-1]);
168
            this.spnNumberOfColors.setValue(this.legend.getRampNumColors());
169
            this.coldColorChooser.set(this.legend.getRampColdColor());
170
            this.hotColorChooser.set(this.legend.getRampHotColor());
167 171
        } else {
168 172
            this.hotColorChooser.setEnabled(false);
169 173
            this.coldColorChooser.setEnabled(false);

Also available in: Unified diff