Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / tools / ColorTables.java @ 34300

History | View | Annotate | Download (4.44 KB)

1
package org.gvsig.fmap.raster.tools;
2

    
3
import java.awt.Color;
4
import java.awt.Graphics2D;
5
import java.awt.Rectangle;
6
import java.util.ArrayList;
7
import java.util.Iterator;
8
import java.util.List;
9

    
10
import org.gvsig.raster.datastruct.ColorItem;
11
import org.gvsig.raster.datastruct.ColorTable;
12
import org.gvsig.raster.datastruct.persistence.ColorTableLibraryPersistence;
13

    
14
import org.gvsig.gui.IColorTables;
15
import org.gvsig.gui.IColorTables.IColorTablePaint;
16

    
17

    
18
public class ColorTables implements IColorTables {
19
        
20
        /**
21
         * 
22
         */
23
        private static final long serialVersionUID = -8518160001050481010L;
24

    
25
        private List tables = null;
26
        ColorTables(){}
27
        
28
        public void load(String palettesPath, boolean interpolateValues) {
29
                tables = new ArrayList();
30
                ArrayList fileList = ColorTableLibraryPersistence.getPaletteFileList(palettesPath);
31

    
32

    
33
                for (int i = 0; i < fileList.size(); i++) {
34
                        ArrayList paletteItems = new ArrayList();
35
                        String paletteName = ColorTableLibraryPersistence.loadPalette(
36
                                                palettesPath,
37
                                                (String) fileList.get(i),
38
                                                paletteItems);
39

    
40
                        if (paletteItems.size() <= 0) {
41
                                continue;
42
                        }
43

    
44
                        ColorTable colorTable = new ColorTable();
45
                        colorTable.setName(paletteName);
46
                        colorTable.createPaletteFromColorItems(paletteItems, true);
47
                        colorTable.setInterpolated(interpolateValues);
48

    
49
                        this.tables.add(new ColorTablePaint(colorTable,paletteName));
50
                }
51

    
52
        }
53
        
54
        public IColorTablePaint get(int i){
55
                return (IColorTablePaint) this.tables.get(i);
56
        }
57
        
58
        public int size(){
59
                return this.tables.size();
60
        }
61
        
62
        public Iterator iterator(){
63
                return this.tables.iterator();
64
        }
65
        
66
        
67
        /**
68
         *
69
         * Class to paint a color palette in a box. It can be indicated if the palette
70
         * is selected and if it is painted with interpolations
71
         *
72
         * @version 30/07/2007
73
         * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
74
         */
75
        public static class ColorTablePaint implements IColorTablePaint {
76
                private ColorTable colorTable;
77
                private String tableName;
78

    
79
                /**
80
                 * Build a ColorTablePaint with a color table (parameter or the method)
81
                 *
82
                 * @param colorTable
83
                 */
84
                public ColorTablePaint(ColorTable colorTable, String tableName) {
85
                        this.colorTable = colorTable;
86
                        this.tableName = tableName;
87
                }
88

    
89
                /**
90
                 * Defines if the values are interpolated between them or not.
91
                 *
92
                 * @param value
93
                 */
94
                public void setInterpolated(boolean value) {
95
                        colorTable.setInterpolated(value);
96
                }
97

    
98
                /**
99
                 * Obtains the color array of the color palette
100
                 *
101
                 * @return
102
                 */
103
                private ArrayList getColorItems() {
104
                        return colorTable.getColorItems();
105
                }
106

    
107
                public Color[] getColors() {
108
                        List colorItems = colorTable.getColorItems();
109
                        
110
                        Color[] colors = new Color[colorItems.size()];
111
                        for (int i = 0; i < colors.length; i++) {
112
                                colors[i] = ((ColorItem) colorItems.get(i)).getColor();
113
                        }
114
                        
115
                        return colors;
116
                }
117

    
118
                /**
119
                 * Obtains the ColorTable
120
                 * @return
121
                 */
122
                private ColorTable getColorTable() {
123
                        return colorTable;
124
                }
125

    
126
                /**
127
                 * Specifies the colors of the color table, defining it the values are
128
                 * interpolated and if the palette will be compressed or not.
129
                 *
130
                 * @param value
131
                 * @param interpolated
132
                 * @param compress
133
                 */
134
                private void setColorItems(ArrayList value, boolean interpolated, boolean compress) {
135
                        colorTable.createPaletteFromColorItems(value, compress);
136
                        setInterpolated(interpolated);
137
                }
138

    
139
                /**
140
                 * Method to paint the color table
141
                 *
142
                 * @param g
143
                 * @param isSelected
144
                 */
145
                public void paint(Graphics2D g, boolean isSelected, Rectangle bounds) {
146
                        Rectangle area = bounds;
147
                        area.y=0;
148
                        area.x=0;
149
                        int x1 = area.x;
150
                        int x2 = area.x + area.width - 1;
151

    
152
                        if (colorTable.getColorItems().size()>=1) {
153
                                double min = ((ColorItem) colorTable.getColorItems().get(0)).getValue();
154
                                double max = ((ColorItem) colorTable.getColorItems().get(colorTable.getColorItems().size()-1)).getValue();
155
                                for (int i = area.x; i < (area.x + area.width); i++) {
156
                                        double pos = min + (((max - min) * (i - area.x)) / (area.width - 2));
157

    
158
                                        byte[] col3 = colorTable.getRGBAByBand(pos);
159
                                        g.setColor(new Color(col3[0] & 0xff, col3[1] & 0xff, col3[2] & 0xff));
160
                                        g.drawLine(i, area.y, i, area.y + area.height);
161
                                }
162
                        } else {
163
                                g.setColor(new Color(224, 224, 224));
164
                                g.fillRect(x1, area.y, x2 - x1, area.height - 1);
165
                        }
166
                        if (isSelected) {
167
                                g.setColor(Color.black);
168
                        } else {
169
                                g.setColor(new Color(96, 96, 96));
170
                        }
171
                        g.drawRect(x1, area.y, x2 - x1, area.height - 1);
172
                }
173
                
174
                public String getTableName(){
175
                        return this.tableName;
176
                }
177
        }
178

    
179
}
180