svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / fmap / raster / lib / legend / impl / DefaultColorTable.java @ 44831
History | View | Annotate | Download (24.4 KB)
1 | 44831 | jjdelcerro | package org.gvsig.raster.lib.legend.impl; |
---|---|---|---|
2 | |||
3 | import java.awt.Color; |
||
4 | import java.io.File; |
||
5 | import java.util.ArrayList; |
||
6 | import java.util.Collections; |
||
7 | import java.util.List; |
||
8 | |||
9 | import org.apache.commons.lang3.StringUtils; |
||
10 | |||
11 | import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
||
12 | import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
||
13 | import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretationNotification; |
||
14 | import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
||
15 | import org.gvsig.raster.lib.legend.api.colortable.ColorTableNotification; |
||
16 | import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass; |
||
17 | import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClassNotification; |
||
18 | import org.gvsig.raster.lib.legend.impl.colorinterpretation.DefaultColorInterpretationNotification; |
||
19 | import org.gvsig.raster.lib.legend.impl.colortable.DefaultColorTableNotification; |
||
20 | import org.gvsig.tools.ToolsLocator; |
||
21 | import org.gvsig.tools.dynobject.DynStruct; |
||
22 | import org.gvsig.tools.observer.Notification; |
||
23 | import org.gvsig.tools.observer.Observable; |
||
24 | import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
||
25 | import org.gvsig.tools.persistence.PersistenceManager; |
||
26 | import org.gvsig.tools.persistence.PersistentState; |
||
27 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
28 | |||
29 | /**
|
||
30 | * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
||
31 | *
|
||
32 | */
|
||
33 | public class DefaultColorTable extends BaseWeakReferencingObservable implements ColorTable { |
||
34 | |||
35 | /**
|
||
36 | * Persistence definition name
|
||
37 | */
|
||
38 | public static final String PERSISTENT_NAME = "ColorTablePersistent"; |
||
39 | /**
|
||
40 | * Description of persistence definition
|
||
41 | */
|
||
42 | public static final String PERSISTENT_DESCRIPTION = "Persistent definition of color table"; |
||
43 | |||
44 | private static final String NAME_PERSISTENCE_FIELD = "name"; |
||
45 | private static final String RANGE_PERSISTENCE_FIELD = "range"; |
||
46 | private static final String NAME_CLASS_PERSISTENCE_FIELD = "nameClass"; |
||
47 | private static final String CLASSES_PERSISTENCE_FIELD = "classes"; |
||
48 | private static final String PALETTE_BY_BAND_PERSISTENCE_FIELD = "paletteByBand"; |
||
49 | private static final String INTERPOLATED_PERSISTENCE_FIELD = "interpolated"; |
||
50 | private static final String ERROR_COLOR_PERSISTENCE_FIELD = "errorColor"; |
||
51 | |||
52 | /**
|
||
53 | * Default number of classes when {@link ColorTable} is created
|
||
54 | */
|
||
55 | private static final int defaultNumberOfClasses = 256; |
||
56 | |||
57 | private List<ColorTableClass> classes; |
||
58 | private String name; |
||
59 | private boolean interpolated; |
||
60 | private int errorColor = 8; |
||
61 | private double[] classValues = null; |
||
62 | private byte[][] paletteByBand = null; |
||
63 | private String[] classNames = null; |
||
64 | |||
65 | /**
|
||
66 | * Empty constructor
|
||
67 | */
|
||
68 | public DefaultColorTable() {
|
||
69 | |||
70 | } |
||
71 | |||
72 | /**
|
||
73 | * Default constructor
|
||
74 | *
|
||
75 | * @param name
|
||
76 | * Name of {@link ColorTable}
|
||
77 | * @param classes
|
||
78 | * Classes of {@link ColorTable}
|
||
79 | * @param interpolated
|
||
80 | * True if this {@link ColorTable} is interpolated
|
||
81 | */
|
||
82 | public DefaultColorTable(String name, List<ColorTableClass> classes, boolean interpolated) { |
||
83 | |||
84 | if (isNamePath(name)) {
|
||
85 | throw new IllegalArgumentException("Name of Color table can not be a path"); |
||
86 | } |
||
87 | |||
88 | this.name = name;
|
||
89 | this.classes = classes;
|
||
90 | this.interpolated = interpolated;
|
||
91 | observeClasses(); |
||
92 | reCalculateColorTable(false);
|
||
93 | } |
||
94 | |||
95 | private void observeClasses() { |
||
96 | for (ColorTableClass clazz : this.classes) { |
||
97 | clazz.addObserver(this);
|
||
98 | } |
||
99 | } |
||
100 | |||
101 | private void stopObservingClasses() { |
||
102 | for (ColorTableClass clazz : this.classes) { |
||
103 | clazz.deleteObserver(this);
|
||
104 | } |
||
105 | } |
||
106 | |||
107 | private void applyPalette(List<ColorTableClass> colorItems) { |
||
108 | List<ColorTableClass> arrayColors = new ArrayList<ColorTableClass>(); |
||
109 | |||
110 | classValues = new double[0]; |
||
111 | classNames = new String[0]; |
||
112 | |||
113 | if (colorItems.size() == 0) { |
||
114 | return;
|
||
115 | } |
||
116 | |||
117 | double min = colorItems.get(0).getValue(); |
||
118 | double max = colorItems.get(colorItems.size() - 1).getValue(); |
||
119 | |||
120 | if (min > max) {
|
||
121 | double aux = max;
|
||
122 | max = min; |
||
123 | min = aux; |
||
124 | } |
||
125 | |||
126 | Color color = Color.white; |
||
127 | Color colorOld = null; |
||
128 | |||
129 | // Make parts, keeping each class in an array
|
||
130 | for (int i = 0; i < defaultNumberOfClasses; i++) { |
||
131 | double value = min + ((i * (max - min)) / (defaultNumberOfClasses - 1)); |
||
132 | int pos = 0; |
||
133 | for (int j = 1; j <= colorItems.size(); j++) { |
||
134 | if (j < colorItems.size()) {
|
||
135 | if (value < colorItems.get(j).getValue()) {
|
||
136 | pos = j - 1;
|
||
137 | break;
|
||
138 | } |
||
139 | } else {
|
||
140 | pos = j - 1;
|
||
141 | break;
|
||
142 | } |
||
143 | } |
||
144 | |||
145 | // Calculate palette color
|
||
146 | if (interpolated) {
|
||
147 | color = interpolatedColor(value, pos); |
||
148 | } else {
|
||
149 | if ((pos + 1) < colorItems.size()) { |
||
150 | double min2 = colorItems.get(pos).getValue();
|
||
151 | double max2 = colorItems.get(pos + 1).getValue(); |
||
152 | if ((min2 + ((max2 - min2) * colorItems.get(pos + 1).getInterpolated() / 100)) < value) { |
||
153 | pos++; |
||
154 | } |
||
155 | } |
||
156 | color = colorItems.get(pos).getColor(); |
||
157 | } |
||
158 | |||
159 | if (!isEqualColor(color, colorOld, 0)) { |
||
160 | ColorTableClass colorItem = new DefaultColorTableClass();
|
||
161 | colorItem.setValue(value); |
||
162 | colorItem.setColor(color); |
||
163 | arrayColors.add(colorItem); |
||
164 | } |
||
165 | |||
166 | colorOld = color; |
||
167 | } |
||
168 | |||
169 | // Una vez tenemos una paleta de 256 colores o inferior, rellenamos
|
||
170 | // los siguientes valores para hacer busquedas rapidas.
|
||
171 | paletteByBand = new byte[arrayColors.size()][4]; |
||
172 | classValues = new double[arrayColors.size()]; |
||
173 | classNames = new String[arrayColors.size()]; |
||
174 | |||
175 | for (int i = 0; i < arrayColors.size(); i++) { |
||
176 | paletteByBand[i][0] = (byte) arrayColors.get(i).getColor().getRed(); |
||
177 | paletteByBand[i][1] = (byte) arrayColors.get(i).getColor().getGreen(); |
||
178 | paletteByBand[i][2] = (byte) arrayColors.get(i).getColor().getBlue(); |
||
179 | paletteByBand[i][3] = (byte) arrayColors.get(i).getColor().getAlpha(); |
||
180 | classValues[i] = arrayColors.get(i).getValue(); |
||
181 | classNames[i] = arrayColors.get(i).getName(); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | private boolean canDelete(int first, int last) { |
||
186 | if (first >= getClasses().size()) {
|
||
187 | return false; |
||
188 | } |
||
189 | if (last >= getClasses().size()) {
|
||
190 | return false; |
||
191 | } |
||
192 | ColorTableClass c1 = getClasses().get(first); |
||
193 | ColorTableClass c2 = getClasses().get(last); |
||
194 | for (int i = (first + 1); i < last; i++) { |
||
195 | if (!isCorrectColor(c1, c2, getClasses().get(i))) {
|
||
196 | return false; |
||
197 | } |
||
198 | } |
||
199 | return true; |
||
200 | } |
||
201 | |||
202 | @Override
|
||
203 | public void compressPalette() { |
||
204 | removeDuplicatedValues(); |
||
205 | int size = -1; |
||
206 | |||
207 | while (size != classes.size()) {
|
||
208 | int init = 0; |
||
209 | int posMax = 2; |
||
210 | |||
211 | size = classes.size(); |
||
212 | while (init < classes.size()) {
|
||
213 | if ((posMax < classes.size()) && canDelete(init, posMax)) {
|
||
214 | posMax++; |
||
215 | continue;
|
||
216 | } |
||
217 | if ((init + 2) < posMax) { |
||
218 | if (canDelete(init, posMax - 1)) { |
||
219 | for (int i = (posMax - 2); i > init; i--) { |
||
220 | if (i < classes.size()) {
|
||
221 | classes.remove(i); |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | init++; |
||
227 | posMax = init + 2;
|
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | @Override
|
||
233 | public void copyFrom(ColorTable colorTable) { |
||
234 | RasterLegendManager rasterLegendManager = RasterLegendLocator.getRasterLegendManager(); |
||
235 | this.interpolated = colorTable.isInterpolated();
|
||
236 | this.name = colorTable.getName();
|
||
237 | List<ColorTableClass> clonedClasses = new ArrayList<ColorTableClass>(colorTable.getClasses().size()); |
||
238 | for (ColorTableClass colorTableClass : colorTable.getClasses()) {
|
||
239 | ColorTableClass clonedClass = rasterLegendManager.createColorTableClass( |
||
240 | colorTableClass.getName(), |
||
241 | colorTableClass.getValue(), |
||
242 | colorTableClass.getInterpolated(), |
||
243 | colorTableClass.getColor()); |
||
244 | clonedClasses.add(clonedClass); |
||
245 | } |
||
246 | |||
247 | setClasses(clonedClasses, false);
|
||
248 | this.notifyObservers(new DefaultColorTableNotification(ColorTableNotification.COPIED_FROM_COLOR_TABLE, new Object[] { colorTable })); |
||
249 | } |
||
250 | |||
251 | @Override
|
||
252 | public List<ColorTableClass> getClasses() { |
||
253 | return this.classes; |
||
254 | } |
||
255 | |||
256 | @Override
|
||
257 | public String getName() { |
||
258 | return this.name; |
||
259 | } |
||
260 | |||
261 | @Override
|
||
262 | public boolean hasAlpha() { |
||
263 | for (int i = 0; i < classes.size(); i++) { |
||
264 | ColorTableClass colorTableClass = classes.get(i); |
||
265 | if (colorTableClass.getColor().getAlpha() != 255) { |
||
266 | return true; |
||
267 | } |
||
268 | } |
||
269 | return false; |
||
270 | } |
||
271 | |||
272 | private Color interpolatedColor(double value, int pos) { |
||
273 | if (classes.size() <= 0) { |
||
274 | return Color.black; |
||
275 | } |
||
276 | |||
277 | if ((pos + 1) == classes.size()) { |
||
278 | return classes.get(pos).getColor();
|
||
279 | } |
||
280 | |||
281 | if (value <= classes.get(0).getValue()) { |
||
282 | return classes.get(0).getColor(); |
||
283 | } |
||
284 | |||
285 | ColorTableClass item1 = classes.get(pos); |
||
286 | ColorTableClass item2 = classes.get(pos + 1);
|
||
287 | |||
288 | double percValue =
|
||
289 | ((value - item1.getValue()) * 100) / (item2.getValue() - item1.getValue());
|
||
290 | |||
291 | Color halfColor =
|
||
292 | new Color((item2.getColor().getRed() + item1.getColor().getRed()) >> 1, (item2 |
||
293 | .getColor().getGreen() + item1.getColor().getGreen()) >> 1, (item2.getColor()
|
||
294 | .getBlue() + item1.getColor().getBlue()) >> 1, (item2.getColor().getAlpha() + item1
|
||
295 | .getColor().getAlpha()) >> 1);
|
||
296 | |||
297 | Color color1, color2;
|
||
298 | double perc1, perc2;
|
||
299 | |||
300 | if (percValue > item2.getInterpolated()) {
|
||
301 | color1 = halfColor; |
||
302 | color2 = item2.getColor(); |
||
303 | perc1 = item2.getInterpolated(); |
||
304 | perc2 = 100;
|
||
305 | } else {
|
||
306 | color1 = item1.getColor(); |
||
307 | color2 = halfColor; |
||
308 | perc1 = 0;
|
||
309 | perc2 = item2.getInterpolated(); |
||
310 | } |
||
311 | |||
312 | double percNew = (percValue - perc1) / (perc2 - perc1);
|
||
313 | |||
314 | Color newColor =
|
||
315 | new Color( |
||
316 | (int) (color1.getRed() + ((color2.getRed() - color1.getRed()) * percNew)) & 0xff, |
||
317 | (int) (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * percNew)) & 0xff, |
||
318 | (int) (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * percNew)) & 0xff, |
||
319 | (int) (color1.getAlpha() + ((color2.getAlpha() - color1.getAlpha()) * percNew)) & 0xff); |
||
320 | |||
321 | return newColor;
|
||
322 | } |
||
323 | |||
324 | private boolean isCorrectColor(ColorTableClass c1, ColorTableClass c2, ColorTableClass c3) { |
||
325 | if ((c3.getName() != null) && (c3.getName().length() > 0)) { |
||
326 | return false; |
||
327 | } |
||
328 | if (c3.getInterpolated() != 50) { |
||
329 | return false; |
||
330 | } |
||
331 | if (c2.getInterpolated() != 50) { |
||
332 | return false; |
||
333 | } |
||
334 | |||
335 | double max = c2.getValue() - c1.getValue();
|
||
336 | int r =
|
||
337 | c1.getColor().getRed() |
||
338 | + (int) (((c2.getColor().getRed() - c1.getColor().getRed()) * (c3.getValue() - c1
|
||
339 | .getValue())) / max); |
||
340 | int g =
|
||
341 | c1.getColor().getGreen() |
||
342 | + (int) (((c2.getColor().getGreen() - c1.getColor().getGreen()) * (c3.getValue() - c1
|
||
343 | .getValue())) / max); |
||
344 | int b =
|
||
345 | c1.getColor().getBlue() |
||
346 | + (int) (((c2.getColor().getBlue() - c1.getColor().getBlue()) * (c3.getValue() - c1
|
||
347 | .getValue())) / max); |
||
348 | int a =
|
||
349 | c1.getColor().getAlpha() |
||
350 | + (int) (((c2.getColor().getAlpha() - c1.getColor().getAlpha()) * (c3.getValue() - c1
|
||
351 | .getValue())) / max); |
||
352 | Color aux = new Color(r & 0xff, g & 0xff, b & 0xff, a & 0xff); |
||
353 | |||
354 | return isEqualColor(c3.getColor(), aux, errorColor);
|
||
355 | } |
||
356 | |||
357 | private boolean isEqualColor(Color c1, Color c2, int error) { |
||
358 | if ((c2 == null) && (c1 != null)) { |
||
359 | return false; |
||
360 | } |
||
361 | if ((c1 == null) && (c2 != null)) { |
||
362 | return false; |
||
363 | } |
||
364 | if (c2.getRed() < (c1.getRed() - error)) {
|
||
365 | return false; |
||
366 | } |
||
367 | if (c2.getGreen() < (c1.getGreen() - error)) {
|
||
368 | return false; |
||
369 | } |
||
370 | if (c2.getBlue() < (c1.getBlue() - error)) {
|
||
371 | return false; |
||
372 | } |
||
373 | if (c2.getAlpha() < (c1.getAlpha() - error)) {
|
||
374 | return false; |
||
375 | } |
||
376 | |||
377 | if (c2.getRed() > (c1.getRed() + error)) {
|
||
378 | return false; |
||
379 | } |
||
380 | if (c2.getGreen() > (c1.getGreen() + error)) {
|
||
381 | return false; |
||
382 | } |
||
383 | if (c2.getBlue() > (c1.getBlue() + error)) {
|
||
384 | return false; |
||
385 | } |
||
386 | if (c2.getAlpha() > (c1.getAlpha() + error)) {
|
||
387 | return false; |
||
388 | } |
||
389 | return true; |
||
390 | } |
||
391 | |||
392 | @Override
|
||
393 | public boolean isInterpolated() { |
||
394 | return interpolated;
|
||
395 | } |
||
396 | |||
397 | @Override
|
||
398 | @SuppressWarnings("unchecked") |
||
399 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
400 | interpolated = state.getBoolean(INTERPOLATED_PERSISTENCE_FIELD); |
||
401 | errorColor = state.getInt(ERROR_COLOR_PERSISTENCE_FIELD); |
||
402 | name = state.getString(NAME_PERSISTENCE_FIELD); |
||
403 | |||
404 | List<Double> range = state.getList(RANGE_PERSISTENCE_FIELD); |
||
405 | this.classValues = new double[range.size()]; |
||
406 | for (int i = 0; i < range.size(); i++) { |
||
407 | this.classValues[i] = range.get(i);
|
||
408 | } |
||
409 | |||
410 | List<String> nameClass = state.getList(NAME_CLASS_PERSISTENCE_FIELD); |
||
411 | if (nameClass != null) { |
||
412 | this.classNames = new String[nameClass.size()]; |
||
413 | nameClass.toArray(this.classNames);
|
||
414 | } |
||
415 | |||
416 | List<ColorTableClass> colorItems = state.getList(CLASSES_PERSISTENCE_FIELD);
|
||
417 | if (colorItems != null) { |
||
418 | this.classes = new ArrayList<ColorTableClass>(); |
||
419 | this.classes.addAll(colorItems);
|
||
420 | } |
||
421 | |||
422 | List<String> aux = state.getList(PALETTE_BY_BAND_PERSISTENCE_FIELD); |
||
423 | if (aux != null) { |
||
424 | this.paletteByBand = null; |
||
425 | for (int i = 0; i < aux.size(); i++) { |
||
426 | String[] values = aux.get(i).split("#"); |
||
427 | if (paletteByBand == null) { |
||
428 | if (values.length == 3) { |
||
429 | this.paletteByBand = new byte[aux.size()][3]; |
||
430 | } |
||
431 | if (values.length == 4) { |
||
432 | this.paletteByBand = new byte[aux.size()][4]; |
||
433 | } |
||
434 | } |
||
435 | paletteByBand[i][0] = Byte.valueOf(values[0]).byteValue(); |
||
436 | paletteByBand[i][1] = Byte.valueOf(values[1]).byteValue(); |
||
437 | paletteByBand[i][2] = Byte.valueOf(values[2]).byteValue(); |
||
438 | if (values.length == 4) { |
||
439 | paletteByBand[i][3] = Byte.valueOf(values[3]).byteValue(); |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 | |||
445 | private void reCalculateColorTable(boolean compress) { |
||
446 | // Sort classes
|
||
447 | Collections.sort(this.classes); |
||
448 | |||
449 | // Compress palette
|
||
450 | if (compress) {
|
||
451 | compressPalette(); |
||
452 | } |
||
453 | |||
454 | // Calculate and apply palette
|
||
455 | applyPalette(classes); |
||
456 | |||
457 | // Remove duplicated values
|
||
458 | removeDuplicatedValues(); |
||
459 | } |
||
460 | |||
461 | @Override
|
||
462 | public void removeDuplicatedValues() { |
||
463 | for (int i = classes.size() - 2; i >= 0; i--) { |
||
464 | if ((i + 1) >= classes.size()) { |
||
465 | continue;
|
||
466 | } |
||
467 | ColorTableClass nextColorTableClass = classes.get(i + 1);
|
||
468 | ColorTableClass colorTableClass = classes.get(i); |
||
469 | |||
470 | // If value is different, don't remove it
|
||
471 | if (nextColorTableClass.getValue() != colorTableClass.getValue()) {
|
||
472 | continue;
|
||
473 | } |
||
474 | |||
475 | // If color is different, don't remove it
|
||
476 | if (!nextColorTableClass.getColor().equals(colorTableClass.getColor())) {
|
||
477 | continue;
|
||
478 | } |
||
479 | |||
480 | // If class does not have name, remove it
|
||
481 | if (StringUtils.isBlank(nextColorTableClass.getName())) {
|
||
482 | classes.remove(i + 1);
|
||
483 | continue;
|
||
484 | } |
||
485 | if (StringUtils.isBlank(colorTableClass.getName())) {
|
||
486 | classes.remove(i); |
||
487 | continue;
|
||
488 | } |
||
489 | |||
490 | // Remove class if class name is equals
|
||
491 | if (nextColorTableClass.getName().equals(colorTableClass.getName())) {
|
||
492 | classes.remove(i); |
||
493 | continue;
|
||
494 | } |
||
495 | } |
||
496 | } |
||
497 | |||
498 | @Override
|
||
499 | public void saveToState(PersistentState state) throws PersistenceException { |
||
500 | state.set(INTERPOLATED_PERSISTENCE_FIELD, interpolated); |
||
501 | state.set(ERROR_COLOR_PERSISTENCE_FIELD, errorColor); |
||
502 | state.set(NAME_PERSISTENCE_FIELD, name); |
||
503 | state.set(RANGE_PERSISTENCE_FIELD, classValues); |
||
504 | state.set(NAME_CLASS_PERSISTENCE_FIELD, classNames); |
||
505 | state.set(CLASSES_PERSISTENCE_FIELD, classes); |
||
506 | |||
507 | ArrayList<String> list = new ArrayList<String>(); |
||
508 | if (paletteByBand != null) { |
||
509 | String entry = null; |
||
510 | for (int i = 0; i < paletteByBand.length; i++) { |
||
511 | entry = paletteByBand[i][0] + "#" + paletteByBand[i][1] + "#" + paletteByBand[i][2]; |
||
512 | if (paletteByBand[0].length > 3) { |
||
513 | entry += "#" + paletteByBand[i][3]; |
||
514 | } |
||
515 | list.add(entry); |
||
516 | } |
||
517 | state.set(PALETTE_BY_BAND_PERSISTENCE_FIELD, list); |
||
518 | } else {
|
||
519 | state.setNull(PALETTE_BY_BAND_PERSISTENCE_FIELD); |
||
520 | } |
||
521 | |||
522 | } |
||
523 | |||
524 | @Override
|
||
525 | public void setClasses(List<ColorTableClass> colorTableClasses, boolean compress) { |
||
526 | if(this.classes!=null){ |
||
527 | stopObservingClasses(); |
||
528 | } |
||
529 | this.classes = colorTableClasses;
|
||
530 | observeClasses(); |
||
531 | reCalculateColorTable(compress); |
||
532 | this.notifyObservers(new DefaultColorTableNotification(ColorTableNotification.SETTED_CLASS_VALUES_COLOR_TABLE, new Object[] { colorTableClasses })); |
||
533 | } |
||
534 | |||
535 | @Override
|
||
536 | public void setClasses(double min, double max, boolean compress) { |
||
537 | ColorTableClass colorTableClass; |
||
538 | double max2 = Double.NEGATIVE_INFINITY; |
||
539 | double min2 = Double.POSITIVE_INFINITY; |
||
540 | |||
541 | if (min > max) {
|
||
542 | double aux = min;
|
||
543 | min = max; |
||
544 | max = aux; |
||
545 | } |
||
546 | |||
547 | RasterLegendManager rasterLegendManager = RasterLegendLocator.getRasterLegendManager(); |
||
548 | List<ColorTableClass> arrayList = new ArrayList<ColorTableClass>(); |
||
549 | List<ColorTableClass> items = this.getClasses(); |
||
550 | |||
551 | // Actualizamos el maximo y minimo del array
|
||
552 | for (int i = 0; i < items.size(); i++) { |
||
553 | colorTableClass = items.get(i); |
||
554 | if (colorTableClass.getValue() > max2) {
|
||
555 | max2 = colorTableClass.getValue(); |
||
556 | } |
||
557 | if (colorTableClass.getValue() < min2) {
|
||
558 | min2 = colorTableClass.getValue(); |
||
559 | } |
||
560 | } |
||
561 | |||
562 | // A?adir el minimo
|
||
563 | if (items.size() > 0) { |
||
564 | colorTableClass = items.get(0);
|
||
565 | } else {
|
||
566 | colorTableClass = rasterLegendManager.createColorTableClass(); |
||
567 | colorTableClass.setValue(0);
|
||
568 | colorTableClass.setColor(Color.black);
|
||
569 | } |
||
570 | |||
571 | arrayList.add(colorTableClass); |
||
572 | |||
573 | for (int i = 0; i < items.size(); i++) { |
||
574 | colorTableClass = items.get(i); |
||
575 | colorTableClass.setValue(min |
||
576 | + (((colorTableClass.getValue() - min2) * (max - min)) / (max2 - min2))); |
||
577 | arrayList.add(colorTableClass); |
||
578 | } |
||
579 | |||
580 | // A?adir el maximo
|
||
581 | if (items.size() > 0) { |
||
582 | colorTableClass = items.get(items.size() - 1);
|
||
583 | } else {
|
||
584 | colorTableClass = rasterLegendManager.createColorTableClass(); |
||
585 | colorTableClass.setValue(255);
|
||
586 | colorTableClass.setColor(Color.white);
|
||
587 | } |
||
588 | arrayList.add(colorTableClass); |
||
589 | |||
590 | setClasses(arrayList, compress); |
||
591 | } |
||
592 | |||
593 | @Override
|
||
594 | public void setInterpolated(boolean flag) { |
||
595 | boolean oldInterpolated = this.interpolated; |
||
596 | this.interpolated = flag;
|
||
597 | if(this.classes!=null){ |
||
598 | reCalculateColorTable(false);
|
||
599 | } |
||
600 | this.notifyObservers(new DefaultColorTableNotification(ColorTableNotification.CHANGED_INTERPOLATED_COLOR_TABLE, new Object[] { new Boolean(oldInterpolated), new Boolean(this.interpolated) })); |
||
601 | } |
||
602 | |||
603 | @Override
|
||
604 | public void setName(String name) { |
||
605 | |||
606 | if (isNamePath(name)) {
|
||
607 | throw new IllegalArgumentException("Name of Color table can not be a path"); |
||
608 | } |
||
609 | |||
610 | this.name = name;
|
||
611 | this.notifyObservers(new DefaultColorTableNotification(ColorTableNotification.RENAMED_COLOR_TABLE, new Object[] { name })); |
||
612 | } |
||
613 | |||
614 | public static void registerPersistence() { |
||
615 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
616 | DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
||
617 | if (definition == null) { |
||
618 | definition = |
||
619 | manager.addDefinition(DefaultColorTable.class, PERSISTENT_NAME, |
||
620 | PERSISTENT_DESCRIPTION, null, null); |
||
621 | definition.addDynFieldBoolean(INTERPOLATED_PERSISTENCE_FIELD).setMandatory(true);
|
||
622 | definition.addDynFieldInt(ERROR_COLOR_PERSISTENCE_FIELD).setMandatory(false);
|
||
623 | definition.addDynFieldString(NAME_PERSISTENCE_FIELD).setMandatory(false);
|
||
624 | definition.addDynFieldList(RANGE_PERSISTENCE_FIELD).setClassOfItems(Double.class)
|
||
625 | .setMandatory(false);
|
||
626 | definition.addDynFieldList(NAME_CLASS_PERSISTENCE_FIELD).setClassOfItems(String.class)
|
||
627 | .setMandatory(false);
|
||
628 | definition.addDynFieldList(CLASSES_PERSISTENCE_FIELD) |
||
629 | .setClassOfItems(ColorTableClass.class).setMandatory(false);
|
||
630 | definition.addDynFieldList(PALETTE_BY_BAND_PERSISTENCE_FIELD) |
||
631 | .setClassOfItems(String.class).setMandatory(false); |
||
632 | } |
||
633 | } |
||
634 | |||
635 | private boolean isNamePath(String name) { |
||
636 | |||
637 | if(name.contains(File.pathSeparator) || name.contains(".")){ |
||
638 | return true; |
||
639 | } |
||
640 | |||
641 | return false; |
||
642 | } |
||
643 | |||
644 | @Override
|
||
645 | public byte[] getRGBA(Object value) { |
||
646 | Number number = null; |
||
647 | if (value instanceof Number) { |
||
648 | number = (Number) value;
|
||
649 | for (int i = 1; i <= classValues.length; i++) { |
||
650 | if (i < classValues.length) {
|
||
651 | if (number.doubleValue() < classValues[i]){
|
||
652 | return paletteByBand[i - 1].clone(); |
||
653 | } |
||
654 | } else {
|
||
655 | return paletteByBand[i - 1].clone(); |
||
656 | } |
||
657 | } |
||
658 | return new byte[4]; |
||
659 | } |
||
660 | throw new IllegalArgumentException("Value has to be a number"); |
||
661 | } |
||
662 | |||
663 | @Override
|
||
664 | public Object clone() throws CloneNotSupportedException { |
||
665 | ColorTable cloned = (ColorTable) super.clone();
|
||
666 | cloned.copyFrom(this);
|
||
667 | return cloned;
|
||
668 | } |
||
669 | |||
670 | @Override
|
||
671 | public void update(Observable observable, Object notification) { |
||
672 | //FIXME:
|
||
673 | |||
674 | if (notification instanceof ColorTableClassNotification && observable instanceof ColorTableClass) { |
||
675 | Notification colorTableClassNotification = (Notification) notification; |
||
676 | if (colorTableClassNotification.getType().equals(ColorTableClassNotification.SETTED_COLOR)
|
||
677 | || colorTableClassNotification.getType().equals(ColorTableClassNotification.SETTED_INTERPOLATED) |
||
678 | || colorTableClassNotification.getType().equals(ColorTableClassNotification.SETTED_NAME) |
||
679 | || colorTableClassNotification.getType().equals(ColorTableClassNotification.SETTED_VALUE)) { |
||
680 | this.notifyObservers(new DefaultColorTableNotification(ColorTableNotification.SETTED_CLASS_VALUES_COLOR_TABLE, |
||
681 | new Object[] { observable })); |
||
682 | } |
||
683 | } |
||
684 | } |
||
685 | } |