root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / ColorTable.java @ 13022
History | View | Annotate | Download (21.5 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.datastruct; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.util.ArrayList; |
23 |
import java.util.Arrays; |
24 |
|
25 |
import org.gvsig.raster.RasterLibrary; |
26 |
|
27 |
import es.gva.cit.jgdal.GdalColorEntry; |
28 |
import es.gva.cit.jgdal.GdalColorTable; |
29 |
import es.gva.cit.jgdal.GdalException; |
30 |
/**
|
31 |
* Paleta para raster. Esta consta de los valores RGB de la paleta que son
|
32 |
* almacenados en un vector donde cada elemento contiene en su interior el RGB
|
33 |
* completo y del vector de rangos.
|
34 |
*
|
35 |
* @version 04/07/2007
|
36 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
37 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
38 |
*/
|
39 |
public class ColorTable implements Cloneable { |
40 |
private static int hashCode(Object[] array) { |
41 |
final int PRIME = 31; |
42 |
if (array == null) |
43 |
return 0; |
44 |
int result = 1; |
45 |
for (int index = 0; index < array.length; index++) { |
46 |
result = PRIME * result + (array[index] == null ? 0 : array[index].hashCode()); |
47 |
} |
48 |
return result;
|
49 |
} |
50 |
|
51 |
private static int hashCode(double[] array) { |
52 |
final int PRIME = 31; |
53 |
if (array == null) |
54 |
return 0; |
55 |
int result = 1; |
56 |
for (int index = 0; index < array.length; index++) { |
57 |
long temp = Double.doubleToLongBits(array[index]); |
58 |
result = PRIME * result + (int) (temp ^ (temp >>> 32)); |
59 |
} |
60 |
return result;
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* Lista de ColorItem donde estaran todos los valores de la paleta segun
|
65 |
* el interfaz
|
66 |
*/
|
67 |
protected ArrayList colorItems = null; |
68 |
|
69 |
/**
|
70 |
* Booleano que define si se interpolaran los valores de la paleta.
|
71 |
*/
|
72 |
protected boolean interpolated = true; |
73 |
|
74 |
/**
|
75 |
* Lista de rangos para paletas decimales
|
76 |
*/
|
77 |
|
78 |
protected double[] range = null; |
79 |
/**
|
80 |
* Lista de valores RGB
|
81 |
*/
|
82 |
protected byte[][] paletteByBand = null; |
83 |
|
84 |
/**
|
85 |
* Nombre de la clase asociada a la entrada
|
86 |
*/
|
87 |
protected String[] nameClass = null; |
88 |
|
89 |
/**
|
90 |
* Lista con todas las transparencias de la paleta
|
91 |
*/
|
92 |
protected ArrayList transparencyRange = new ArrayList(); |
93 |
|
94 |
/**
|
95 |
* Nombre de la paleta
|
96 |
*/
|
97 |
protected String name = null; |
98 |
|
99 |
/**
|
100 |
* Ruta del fichero a la cual se asocia la paleta. Las bandas de un
|
101 |
* GeoRasterMultiFile han de saber a que paleta van asociadas.
|
102 |
*/
|
103 |
protected String filePath = null; |
104 |
|
105 |
private int errorColor = 8; |
106 |
|
107 |
/**
|
108 |
* Constructor vac?o.
|
109 |
* @param name
|
110 |
*/
|
111 |
public ColorTable() {
|
112 |
this.name = ""; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Constructor. Asigna el nombre de la paleta.
|
117 |
* @param name
|
118 |
*/
|
119 |
public ColorTable(String name) { |
120 |
this.name = name;
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* Asigna el nombre de la paleta
|
125 |
* @param Nombre de la paleta
|
126 |
*/
|
127 |
public void setName(String name) { |
128 |
this.name = name;
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* Obtiene el nombre de la paleta
|
133 |
* @return Nombre de la paleta
|
134 |
*/
|
135 |
public String getName() { |
136 |
return name;
|
137 |
} |
138 |
|
139 |
// static int total = 0;
|
140 |
public void createPaletteFromColorItems(ArrayList colorItems, boolean compress) { |
141 |
this.colorItems = colorItems;
|
142 |
|
143 |
// Ordena la paleta
|
144 |
sortPalette(colorItems); |
145 |
/*
|
146 |
System.out.println(name);
|
147 |
System.out.println("----- Sin comprimir (" + colorItems.size() + ") -----");
|
148 |
*/
|
149 |
/*
|
150 |
if (name.equals("MDT1")) {
|
151 |
for (int i=0; i<colorItems.size(); i++) {
|
152 |
ColorItem c1 = ((ColorItem) colorItems.get(i));
|
153 |
Color co = c1.getColor();
|
154 |
System.out.println(c1.getValue() + ": " + co.getRed() + "," + co.getGreen() + "," + co.getBlue() + "," + co.getAlpha());
|
155 |
}
|
156 |
}
|
157 |
*/
|
158 |
// Mira que valores se pueden descartar y asi dejamos la paleta reducida
|
159 |
// para poder hacer interpolaciones
|
160 |
if (compress)
|
161 |
compressPalette(colorItems); |
162 |
/*
|
163 |
System.out.println("------ Comprimido (" + colorItems.size() + ") ------");
|
164 |
total += colorItems.size();
|
165 |
System.out.println("------ Total (" + total + ")----");
|
166 |
*/
|
167 |
/*
|
168 |
if (name.equals("MDT1")) {
|
169 |
for (int i=0; i<colorItems.size(); i++) {
|
170 |
ColorItem c1 = ((ColorItem) colorItems.get(i));
|
171 |
Color co = c1.getColor();
|
172 |
System.out.println(c1.getValue() + ": " + co.getRed() + "," + co.getGreen() + "," + co.getBlue() + "," + co.getAlpha());
|
173 |
}
|
174 |
}
|
175 |
*/
|
176 |
// Genera la paleta final para poder ser usada
|
177 |
applyPalette(colorItems); |
178 |
} |
179 |
|
180 |
private boolean isEqualColor(Color c1, Color c2, int error) { |
181 |
if ((c2 == null) && (c1 != null)) |
182 |
return false; |
183 |
if ((c1 == null) && (c2 != null)) |
184 |
return false; |
185 |
if (c2.getRed() < (c1.getRed() - error))
|
186 |
return false; |
187 |
if (c2.getGreen() < (c1.getGreen() - error))
|
188 |
return false; |
189 |
if (c2.getBlue() < (c1.getBlue() - error))
|
190 |
return false; |
191 |
if (c2.getAlpha() < (c1.getAlpha() - error))
|
192 |
return false; |
193 |
|
194 |
if (c2.getRed() > (c1.getRed() + error))
|
195 |
return false; |
196 |
if (c2.getGreen() > (c1.getGreen() + error))
|
197 |
return false; |
198 |
if (c2.getBlue() > (c1.getBlue() + error))
|
199 |
return false; |
200 |
if (c2.getAlpha() > (c1.getAlpha() + error))
|
201 |
return false; |
202 |
|
203 |
return true; |
204 |
} |
205 |
|
206 |
/**
|
207 |
* Informa de si el color c3 se encuentra en un rango valido entre c1 y c2.
|
208 |
* Para colores con nombre de clase se devolver? false para no eliminar nunca
|
209 |
* ese item.
|
210 |
* @param c1
|
211 |
* @param c2
|
212 |
* @param c3
|
213 |
* @return
|
214 |
*/
|
215 |
private boolean isCorrectColor(ColorItem c1, ColorItem c2, ColorItem c3) { |
216 |
if ((c3.getNameClass() != null) && (c3.getNameClass().length() > 0)) |
217 |
return false; |
218 |
|
219 |
double max = c2.getValue() - c1.getValue();
|
220 |
int r = c1.getColor().getRed() + (int) (((c2.getColor().getRed() - c1.getColor().getRed()) * (c3.getValue() - c1.getValue())) / max); |
221 |
int g = c1.getColor().getGreen() + (int) (((c2.getColor().getGreen() - c1.getColor().getGreen()) * (c3.getValue() - c1.getValue())) / max); |
222 |
int b = c1.getColor().getBlue() + (int) (((c2.getColor().getBlue() - c1.getColor().getBlue()) * (c3.getValue() - c1.getValue())) / max); |
223 |
int a = c1.getColor().getAlpha() + (int) (((c2.getColor().getAlpha() - c1.getColor().getAlpha()) * (c3.getValue() - c1.getValue())) / max); |
224 |
Color aux = new Color(r & 0xff, g & 0xff, b & 0xff, a & 0xff); |
225 |
|
226 |
return isEqualColor(c3.getColor(), aux, errorColor);
|
227 |
} |
228 |
|
229 |
private boolean canDelete(int first, int last) { |
230 |
ColorItem c1 = (ColorItem) colorItems.get(first); |
231 |
ColorItem c2 = (ColorItem) colorItems.get(last); |
232 |
for (int i = (first + 1); i < last; i++) { |
233 |
if (!isCorrectColor(c1, c2, (ColorItem) colorItems.get(i)))
|
234 |
return false; |
235 |
} |
236 |
return true; |
237 |
} |
238 |
|
239 |
public void compressPalette() { |
240 |
System.out.println("Antes: " + colorItems.size()); |
241 |
compressPalette(colorItems); |
242 |
System.out.println("Despues: " + colorItems.size()); |
243 |
} |
244 |
|
245 |
public boolean isCompressible() { |
246 |
ArrayList cloneList = (ArrayList) colorItems.clone(); |
247 |
compressPalette(cloneList); |
248 |
return (cloneList.size() != colorItems.size());
|
249 |
} |
250 |
|
251 |
private void compressPalette(ArrayList colorItems) { |
252 |
int size = -1; |
253 |
|
254 |
while (size != colorItems.size()) {
|
255 |
int init = 0; |
256 |
int posMax = 2; |
257 |
|
258 |
size = colorItems.size(); |
259 |
while (init < colorItems.size()) {
|
260 |
if ((posMax < colorItems.size()) && canDelete(init, posMax)) {
|
261 |
posMax++; |
262 |
continue;
|
263 |
} |
264 |
if ((init + 2) < posMax) { |
265 |
if (canDelete(init, posMax - 1)) |
266 |
for (int i = (posMax - 2); i > init; i--) |
267 |
if (i < colorItems.size())
|
268 |
colorItems.remove(i); |
269 |
} |
270 |
init++; |
271 |
posMax = init + 2;
|
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Crea una paleta a partir de un objeto GdalColorTable. Esto es necesario
|
278 |
* para los ficheros que tienen una paleta asignada, como los gif, y que son
|
279 |
* tratados por Gdal. Se pasa la tabla de color le?da desde gdal y se crea
|
280 |
* directamente un objeto Palette.
|
281 |
* @param table
|
282 |
*/
|
283 |
public void createPaletteFromGdalColorTable(GdalColorTable table) { |
284 |
try {
|
285 |
colorItems = new ArrayList(); |
286 |
for (int iEntry = 0; iEntry < table.getColorEntryCount(); iEntry++) { |
287 |
GdalColorEntry entry = table.getColorEntryAsRGB(iEntry); |
288 |
|
289 |
ColorItem colorItem = new ColorItem();
|
290 |
colorItem.setNameClass("");
|
291 |
colorItem.setValue(iEntry); |
292 |
colorItem.setColor(new Color( (int) (entry.c1 & 0xff), |
293 |
(int) (entry.c2 & 0xff), |
294 |
(int) (entry.c3 & 0xff), |
295 |
(int) (entry.c4 & 0xff))); |
296 |
|
297 |
colorItems.add(colorItem); |
298 |
} |
299 |
} catch (GdalException ex) {
|
300 |
// No se crea la paleta
|
301 |
} |
302 |
sortPalette(colorItems); |
303 |
//compressPalette();
|
304 |
applyPalette(colorItems); |
305 |
} |
306 |
|
307 |
/**
|
308 |
* Obtiene la transparencia por rangos asociada a la tabla de color actual.
|
309 |
* @return Lista de objetos TransparencyRange
|
310 |
*/
|
311 |
public ArrayList getTransparencyRanges() { |
312 |
return transparencyRange;
|
313 |
} |
314 |
|
315 |
/**
|
316 |
* Obtiene la tabla de color
|
317 |
* @return Paleta
|
318 |
*/
|
319 |
/*
|
320 |
public int[] getColorTable() {
|
321 |
return palette;
|
322 |
}
|
323 |
*/
|
324 |
|
325 |
/**
|
326 |
* Obtiene la tabla de color por banda
|
327 |
* @return Paleta
|
328 |
*/
|
329 |
public byte[][] getColorTableByBand() { |
330 |
return paletteByBand;
|
331 |
} |
332 |
|
333 |
|
334 |
/**
|
335 |
* Asigna una paleta
|
336 |
* @param palette Paleta
|
337 |
*/
|
338 |
public void setColorTable(int[] palette) { |
339 |
paletteByBand = new byte[palette.length][3]; |
340 |
for (int i = 0; i < palette.length; i++) { |
341 |
paletteByBand[i][0] = (byte)((palette[i] & 0x00ff0000) >> 16); |
342 |
paletteByBand[i][1] = (byte)((palette[i] & 0x0000ff00) >> 8); |
343 |
paletteByBand[i][2] = (byte)(palette[i] & 0x000000ff); |
344 |
} |
345 |
} |
346 |
|
347 |
/**
|
348 |
* Obtiene los nombres de las clases de la paleta
|
349 |
* @return Array de cadenas. Cada una corresponde con un nombre de clase
|
350 |
* que corresponde a cada rango de tipos.
|
351 |
*/
|
352 |
public String[] getNameClass() { |
353 |
return nameClass;
|
354 |
} |
355 |
|
356 |
/**
|
357 |
* Asigna los nombres de las clases de la paleta
|
358 |
* @param names Array de cadenas. Cada una corresponde con un nombre de clase
|
359 |
* que corresponde a cada rango de tipos.
|
360 |
*/
|
361 |
public void setNameClass(String[] names) { |
362 |
nameClass = names; |
363 |
} |
364 |
|
365 |
/**
|
366 |
* Obtiene la ruta del fichero al que va asociada la paleta.
|
367 |
* @return Ruta del fichero al que va asociada la paleta.
|
368 |
*/
|
369 |
public String getFilePath() { |
370 |
return filePath;
|
371 |
} |
372 |
|
373 |
/**
|
374 |
* Asigna la ruta del fichero al que va asociada la paleta.
|
375 |
* @param Ruta del fichero al que va asociada la paleta.
|
376 |
*/
|
377 |
public void setFilePath(String filePath) { |
378 |
this.filePath = filePath;
|
379 |
} |
380 |
|
381 |
/**
|
382 |
* Obtiene los rangos de transparencia que tiene la paleta. Estos se obtienen
|
383 |
* en un array de objetos TransparencyRange. Cada uno de ellos representa un
|
384 |
* rango de pixeles
|
385 |
* @return ArrayList
|
386 |
*/
|
387 |
public ArrayList getTransparencyRange() { |
388 |
return transparencyRange;
|
389 |
} |
390 |
|
391 |
/*
|
392 |
* (non-Javadoc)
|
393 |
* @see java.lang.Object#clone()
|
394 |
*/
|
395 |
public Object clone() { |
396 |
ColorTable clone = null;
|
397 |
try {
|
398 |
clone = (ColorTable) super.clone();
|
399 |
} catch (CloneNotSupportedException e) { |
400 |
} |
401 |
|
402 |
if (colorItems != null) { |
403 |
clone.colorItems = new ArrayList(); |
404 |
for (int i = 0; i < colorItems.size(); i++) { |
405 |
clone.colorItems.add(((ColorItem) colorItems.get(i)).clone()); |
406 |
} |
407 |
} |
408 |
|
409 |
if (filePath != null) |
410 |
clone.filePath = new String(filePath); |
411 |
|
412 |
if (name != null) |
413 |
clone.name = new String(name); |
414 |
|
415 |
if (nameClass != null) { |
416 |
clone.nameClass = new String[nameClass.length]; |
417 |
for (int i = 0; i < nameClass.length; i++) |
418 |
clone.nameClass[i] = new String(nameClass[i]); |
419 |
} |
420 |
|
421 |
if (paletteByBand != null) { |
422 |
clone.paletteByBand = (byte[][]) paletteByBand.clone(); |
423 |
for (int i = 0; i < paletteByBand.length; i++) |
424 |
clone.paletteByBand[i] = (byte[]) paletteByBand[i].clone(); |
425 |
} |
426 |
|
427 |
if (range != null) |
428 |
clone.range = (double[]) range.clone(); |
429 |
|
430 |
if (transparencyRange != null) { |
431 |
clone.transparencyRange = new ArrayList(); |
432 |
for (int i = 0; i < transparencyRange.size(); i++) { |
433 |
clone.transparencyRange.add(((TransparencyRange) transparencyRange.get(i)).clone()); |
434 |
} |
435 |
} |
436 |
|
437 |
return clone;
|
438 |
} |
439 |
|
440 |
/**
|
441 |
* Devuelve un color de interpolacion entre dos colores
|
442 |
* @param value
|
443 |
* @param pos
|
444 |
* @return
|
445 |
*/
|
446 |
private Color interpolatedColor(double value, int pos) { |
447 |
if (colorItems.size() <= 0) |
448 |
return Color.black; |
449 |
|
450 |
if ((pos + 1) == colorItems.size()) |
451 |
return ((ColorItem) colorItems.get(pos)).getColor();
|
452 |
|
453 |
if (value <= ((ColorItem) colorItems.get(0)).getValue()) |
454 |
return ((ColorItem) colorItems.get(0)).getColor(); |
455 |
|
456 |
ColorItem item1 = (ColorItem) colorItems.get(pos); |
457 |
ColorItem item2 = (ColorItem) colorItems.get(pos + 1);
|
458 |
|
459 |
double percValue = ((value - item1.getValue()) * 100) / (item2.getValue() - item1.getValue()); |
460 |
|
461 |
Color halfColor = new Color( |
462 |
(item2.getColor().getRed() + item1.getColor().getRed()) >> 1,
|
463 |
(item2.getColor().getGreen() + item1.getColor().getGreen()) >> 1,
|
464 |
(item2.getColor().getBlue() + item1.getColor().getBlue()) >> 1,
|
465 |
(item2.getColor().getAlpha() + item1.getColor().getAlpha()) >> 1);
|
466 |
|
467 |
Color color1, color2;
|
468 |
double perc1, perc2;
|
469 |
|
470 |
if (percValue > item2.getInterpolated()) {
|
471 |
color1 = halfColor; |
472 |
color2 = item2.getColor(); |
473 |
perc1 = item2.getInterpolated(); |
474 |
perc2 = 100;
|
475 |
} else {
|
476 |
color1 = item1.getColor(); |
477 |
color2 = halfColor; |
478 |
perc1 = 0;
|
479 |
perc2 = item2.getInterpolated(); |
480 |
} |
481 |
|
482 |
double percNew = (percValue - perc1) / (perc2 - perc1);
|
483 |
|
484 |
Color newColor = new Color( |
485 |
(int) (color1.getRed() + ((color2.getRed() - color1.getRed()) * percNew)) & 0xff, |
486 |
(int) (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * percNew)) & 0xff, |
487 |
(int) (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * percNew)) & 0xff, |
488 |
(int) (color1.getAlpha() + ((color2.getAlpha() - color1.getAlpha()) * percNew)) & 0xff); |
489 |
|
490 |
|
491 |
return newColor;
|
492 |
} |
493 |
|
494 |
/*
|
495 |
* TODO: RENDIMIENTO: Incluir una heuristica que dado un valor se compare con
|
496 |
* el valor de la mitad de la tabla y si es menor se empieza a recorrer desde
|
497 |
* el principio sino se empieza a recorrer desde la mitad de la tabla hasta
|
498 |
* abajo. Esto hace que se reduzca la tabla a la mitad de valores haciendo
|
499 |
* solo una comparaci?n.
|
500 |
*/
|
501 |
/**
|
502 |
* Obtiene el valor RGB para un clave entera pasada por par?metro
|
503 |
* @param value clave de la cual se quiere obtener el valor RGB de la paleta
|
504 |
* @return valor RGB
|
505 |
*/
|
506 |
public byte[] getRGBByBand(double value) { |
507 |
for (int i = 1; i <= range.length; i++) { |
508 |
if (i < range.length) {
|
509 |
if (value < range[i])
|
510 |
return paletteByBand[i - 1]; |
511 |
} else {
|
512 |
return paletteByBand[i - 1]; |
513 |
} |
514 |
} |
515 |
return new byte[4]; |
516 |
} |
517 |
|
518 |
/*
|
519 |
* TODO: Usar el m?todo Quicksort para ordenar
|
520 |
*/
|
521 |
/**
|
522 |
* Ordena el ColorItems de manera ascendente. De momento se usa el m?todo de
|
523 |
* ordenaci?n por burbuja.
|
524 |
*/
|
525 |
private void sortPalette(ArrayList colorItems) { |
526 |
for (int i = 0; i < colorItems.size(); i++) { |
527 |
for (int j = i + 1; j < colorItems.size(); j++) { |
528 |
if (((ColorItem) colorItems.get(j)).getValue() < ((ColorItem) colorItems.get(i)).getValue()) {
|
529 |
Object aux = colorItems.get(i);
|
530 |
colorItems.set(i, colorItems.get(j)); |
531 |
colorItems.set(j, aux); |
532 |
} |
533 |
} |
534 |
} |
535 |
} |
536 |
|
537 |
/**
|
538 |
* Genera una paleta intermedia para acelerar los calculos.
|
539 |
*/
|
540 |
private void applyPalette(ArrayList colorItems) { |
541 |
ArrayList arrayColors = new ArrayList(); |
542 |
|
543 |
paletteByBand = new byte[0][3]; |
544 |
range = new double[0]; |
545 |
nameClass = new String[0]; |
546 |
|
547 |
if (colorItems.size()==0) |
548 |
return;
|
549 |
|
550 |
// Nos preparamos para hacer las particiones, sabiendo el minimo y maximo
|
551 |
double min = ((ColorItem) colorItems.get(0)).getValue(); |
552 |
double max = ((ColorItem) colorItems.get(colorItems.size() - 1)).getValue(); |
553 |
|
554 |
if (min > max) {
|
555 |
double aux = max;
|
556 |
max = min; |
557 |
min = aux; |
558 |
} |
559 |
|
560 |
Color color = Color.white; |
561 |
Color colorOld = null; |
562 |
|
563 |
// Hacemos las particiones, metiendo cada item calculado en un array
|
564 |
int defaultColors = RasterLibrary.defaultNumberOfColors;
|
565 |
for (int i = 0; i < defaultColors; i++) { |
566 |
double value = min + ((i * (max - min)) / (defaultColors - 1)); |
567 |
int pos = 0; |
568 |
for (int j = 1; j <= colorItems.size(); j++) { |
569 |
if (j < colorItems.size()) {
|
570 |
if (value < ((ColorItem) colorItems.get(j)).getValue()) {
|
571 |
pos = j - 1;
|
572 |
break;
|
573 |
} |
574 |
} else {
|
575 |
pos = j - 1;
|
576 |
break;
|
577 |
} |
578 |
} |
579 |
|
580 |
// Calculamos el color que corresponde, tanto interpolado como no
|
581 |
if (interpolated) {
|
582 |
color = interpolatedColor(value, pos); |
583 |
} else {
|
584 |
if ((pos + 1) < colorItems.size()) { |
585 |
double min2 = ((ColorItem) colorItems.get(pos)).getValue();
|
586 |
double max2 = ((ColorItem) colorItems.get(pos + 1)).getValue(); |
587 |
if ((min2 + ((max2 - min2) * ((ColorItem) colorItems.get(pos + 1)).getInterpolated() / 100)) < value) |
588 |
pos++; |
589 |
} |
590 |
color = ((ColorItem) colorItems.get(pos)).getColor(); |
591 |
} |
592 |
|
593 |
if (!isEqualColor(color, colorOld, 0)) { |
594 |
ColorItem colorItem = new ColorItem();
|
595 |
colorItem.setNameClass("");
|
596 |
colorItem.setValue(value); |
597 |
colorItem.setColor(color); |
598 |
arrayColors.add(colorItem); |
599 |
} |
600 |
|
601 |
colorOld = color; |
602 |
} |
603 |
|
604 |
// Una vez tenemos una paleta de 256 colores o inferior, rellenamos
|
605 |
// los siguientes valores para hacer busquedas rapidas.
|
606 |
|
607 |
paletteByBand = new byte[arrayColors.size()][3]; |
608 |
range = new double[arrayColors.size()]; |
609 |
nameClass = new String[arrayColors.size()]; |
610 |
|
611 |
transparencyRange.clear(); |
612 |
for (int i = 0; i < arrayColors.size(); i++) { |
613 |
paletteByBand[i][0] = (byte) ((ColorItem) arrayColors.get(i)).getColor().getRed(); |
614 |
paletteByBand[i][1] = (byte) ((ColorItem) arrayColors.get(i)).getColor().getGreen(); |
615 |
paletteByBand[i][2] = (byte) ((ColorItem) arrayColors.get(i)).getColor().getBlue(); |
616 |
range[i] = ((ColorItem) arrayColors.get(i)).getValue(); |
617 |
nameClass[i] = ((ColorItem) arrayColors.get(i)).getNameClass(); |
618 |
if (((ColorItem) arrayColors.get(i)).getColor().getAlpha() != 255) { |
619 |
TransparencyRange r = new TransparencyRange();
|
620 |
r.setRed(new int[] { ((ColorItem) arrayColors.get(i)).getColor().getRed(), ((ColorItem) arrayColors.get(i)).getColor().getRed() }); |
621 |
r.setGreen(new int[] { ((ColorItem) arrayColors.get(i)).getColor().getGreen(), ((ColorItem) arrayColors.get(i)).getColor().getGreen() }); |
622 |
r.setBlue(new int[] { ((ColorItem) arrayColors.get(i)).getColor().getBlue(), ((ColorItem) arrayColors.get(i)).getColor().getBlue() }); |
623 |
r.setAlpha(((ColorItem) arrayColors.get(i)).getColor().getAlpha()); |
624 |
r.setAnd(true);
|
625 |
r.loadStrEntryFromValues(); |
626 |
transparencyRange.add(r); |
627 |
} |
628 |
} |
629 |
} |
630 |
|
631 |
public double[] getRange() { |
632 |
return range;
|
633 |
} |
634 |
|
635 |
public void setRange(double[] range) { |
636 |
this.range = range;
|
637 |
} |
638 |
|
639 |
public ArrayList getColorItems() { |
640 |
return colorItems;
|
641 |
} |
642 |
|
643 |
/**
|
644 |
* Nos indica si la paleta la ha generado con valores interpolados o no.
|
645 |
* @return
|
646 |
*/
|
647 |
public boolean isInterpolated() { |
648 |
return interpolated;
|
649 |
} |
650 |
|
651 |
/**
|
652 |
* Definir si la paleta tendra los valores interpolados o no
|
653 |
* @param interpolated
|
654 |
*/
|
655 |
public void setInterpolated(boolean interpolated) { |
656 |
this.interpolated = interpolated;
|
657 |
applyPalette(colorItems); |
658 |
} |
659 |
|
660 |
/*
|
661 |
* (non-Javadoc)
|
662 |
* @see java.lang.Object#hashCode()
|
663 |
*/
|
664 |
public int hashCode() { |
665 |
final int PRIME = 31; |
666 |
int result = 1; |
667 |
result = PRIME * result + ((colorItems == null) ? 0 : colorItems.hashCode()); |
668 |
result = PRIME * result + errorColor; |
669 |
result = PRIME * result + ((filePath == null) ? 0 : filePath.hashCode()); |
670 |
result = PRIME * result + (interpolated ? 1231 : 1237); |
671 |
result = PRIME * result + ((name == null) ? 0 : name.hashCode()); |
672 |
result = PRIME * result + ColorTable.hashCode(nameClass); |
673 |
result = PRIME * result + ColorTable.hashCode(paletteByBand); |
674 |
result = PRIME * result + ColorTable.hashCode(range); |
675 |
result = PRIME * result + ((transparencyRange == null) ? 0 : transparencyRange.hashCode()); |
676 |
return result;
|
677 |
} |
678 |
|
679 |
/*
|
680 |
* (non-Javadoc)
|
681 |
* @see java.lang.Object#equals(java.lang.Object)
|
682 |
*/
|
683 |
public boolean equals(Object obj) { |
684 |
if (this == obj) |
685 |
return true; |
686 |
if (obj == null) |
687 |
return false; |
688 |
if (getClass() != obj.getClass())
|
689 |
return false; |
690 |
final ColorTable other = (ColorTable) obj;
|
691 |
|
692 |
if ( ((colorItems == null) && (other.colorItems != null)) || |
693 |
((colorItems != null) && (other.colorItems == null)) ) |
694 |
return false; |
695 |
|
696 |
if (colorItems != null) { |
697 |
if (colorItems.size() != other.colorItems.size())
|
698 |
return false; |
699 |
for (int i = 0; i < colorItems.size(); i++) { |
700 |
if (!((ColorItem) colorItems.get(i)).equals(other.colorItems.get(i)))
|
701 |
return false; |
702 |
} |
703 |
} |
704 |
|
705 |
if (filePath == null) { |
706 |
if (other.filePath != null) |
707 |
return false; |
708 |
} else if (!filePath.equals(other.filePath)) |
709 |
return false; |
710 |
if (name == null) { |
711 |
if (other.name != null) |
712 |
return false; |
713 |
} else if (!name.equals(other.name)) |
714 |
return false; |
715 |
|
716 |
if (nameClass != null) { |
717 |
if (nameClass.length != other.nameClass.length)
|
718 |
return false; |
719 |
|
720 |
for (int i = 0; i < nameClass.length; i++) { |
721 |
if (!nameClass[i].equals(other.nameClass[i]))
|
722 |
return false; |
723 |
} |
724 |
} |
725 |
|
726 |
if ( ((paletteByBand == null) && (other.paletteByBand != null)) || |
727 |
((paletteByBand != null) && (other.paletteByBand == null)) ) |
728 |
return false; |
729 |
|
730 |
if (paletteByBand != null) { |
731 |
for (int i = 0; i < paletteByBand.length; i++) { |
732 |
for (int j = 0; j < paletteByBand[i].length; j++) { |
733 |
if (paletteByBand[i][j] != other.paletteByBand[i][j])
|
734 |
return false; |
735 |
} |
736 |
} |
737 |
} |
738 |
|
739 |
if (!Arrays.equals(range, other.range)) |
740 |
return false; |
741 |
|
742 |
if ( ((colorItems == null) && (other.colorItems != null)) || |
743 |
((colorItems != null) && (other.colorItems == null)) ) |
744 |
return false; |
745 |
|
746 |
if (transparencyRange != null) { |
747 |
if (transparencyRange.size() != other.transparencyRange.size())
|
748 |
return false; |
749 |
for (int i = 0; i < transparencyRange.size(); i++) { |
750 |
if (!((TransparencyRange) transparencyRange.get(i)).equals(other.transparencyRange.get(i)))
|
751 |
return false; |
752 |
} |
753 |
} |
754 |
|
755 |
return true; |
756 |
} |
757 |
} |