Revision 1847

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
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
}
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
13 13

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

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

  
18
    public Color[] getTargetColorTable();
19

  
18 20
    public boolean useRamp();
19
    
21

  
20 22
    public String getFieldName();
21
    
23

  
22 24
    public void setFieldName(String fieldName);
23
    
25

  
24 26
    public int getColorTableHotColorAlpha();
25 27

  
26 28
    public void setColorTableHotColorAlpha(int colorTableHotColorAlpha);
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
38 38
    private ColorChooserController hotColorChooser;
39 39
    private ColorChooserController coldColorChooser;
40 40
    private ColorSchemeSelector colorSchemaSelector;
41
    
41

  
42 42
    public DefaultHeatmapLegendEditor() {
43 43
        this.legendClass = HeatmapLegendLocator.getHeatmapLegendManager().getLegendClass();
44 44
    }
45
    
45

  
46 46
    @Override
47 47
    public void setData(FLayer layer, ILegend legend) {
48 48
        if( layer == this.layer && legend == this.legend ) {
......
76 76
                useAlphaInColorTableChanged();
77 77
            }
78 78
        });
79
        
79

  
80 80
        this.spnDistance.setValue(this.legend.getDistance());
81 81
        if( this.legend.useRamp() ) {
82 82
            this.rdbUseColorRamp.setSelected(true);
......
112 112
                        }
113 113
                    }
114 114
                } catch (Exception ex) {
115
                    
115

  
116 116
                }
117 117
            }
118 118
        }
119 119
    }
120
    
120

  
121 121
    private void useColorModeChanged() {
122 122
        if( this.rdbUseColorRamp.isSelected() ) {
123 123
            this.hotColorChooser.setEnabled(true);
......
127 127
            this.chkColorTableUseAlpha.setEnabled(false);
128 128
            this.sldColorTableColdAlpha.setEnabled(false);
129 129
            this.sldColorTableHotAlpha.setEnabled(false);
130
            Color[] table = this.legend.getColorTable();
130
            Color[] table = this.legend.getSourceColorTable();
131 131
            this.spnNumberOfColors.setValue(table.length);
132 132
            this.coldColorChooser.set(table[0]);
133 133
            this.hotColorChooser.set(table[table.length-1]);
......
139 139
            this.chkColorTableUseAlpha.setEnabled(true);
140 140
            this.sldColorTableColdAlpha.setEnabled(true);
141 141
            this.sldColorTableHotAlpha.setEnabled(true);
142
            this.colorSchemaSelector.setSelectedColors(this.legend.getColorTable());
142
            this.colorSchemaSelector.setSelectedColors(this.legend.getSourceColorTable());
143 143
        }
144 144
    }
145
    
145

  
146 146
    private void useFieldChanged() {
147 147
        this.cboFields.setEnabled(this.chkUseField.isSelected());
148 148
    }
149
    
149

  
150 150
    private void useAlphaInColorTableChanged() {
151 151
        boolean x = this.chkColorTableUseAlpha.isSelected();
152 152
        this.sldColorTableColdAlpha.setEnabled(x);
153 153
        this.sldColorTableHotAlpha.setEnabled(x);
154 154
    }
155
    
155

  
156 156
    @Override
157 157
    public ILegend getLegend() {
158 158
        legend.setDistance((int) spnDistance.getValue());
159 159
        if( this.rdbUseColorRamp.isSelected() ) {
160 160
            legend.setColorTable(
161
                (int) spnNumberOfColors.getValue(), 
162
                coldColorChooser.get(), 
161
                (int) spnNumberOfColors.getValue(),
162
                coldColorChooser.get(),
163 163
                hotColorChooser.get()
164 164
            );
165 165
        } else {
......
174 174
        this.legend.setColorTableColdColorAlpha(this.sldColorTableColdAlpha.getValue());
175 175
        this.legend.setColorTableHotColorAlpha(this.sldColorTableHotAlpha.getValue());
176 176
        this.legend.setUseAlphaInColorTable(this.chkColorTableUseAlpha.isSelected());
177
        
177

  
178 178
        return legend;
179 179
    }
180 180

  
......
225 225
        try {
226 226
            value = Integer.parseInt(s);
227 227
        } catch(Throwable th) {
228
            
228

  
229 229
        }
230 230
        return value;
231 231
    }
232
        
232

  
233 233
}

Also available in: Unified diff