comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java @ 435:67091b17462d

Added code to split palette interval into equal sized parts. gnv-artifacts/trunk@483 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 25 Dec 2009 12:00:14 +0000
parents 3a0c0ad113d9
children 7399bb8f83ea
comparison
equal deleted inserted replaced
434:0eed5749fd63 435:67091b17462d
30 private String description; 30 private String description;
31 31
32 private Color color; 32 private Color color;
33 33
34 public Entry() { 34 public Entry() {
35 }
36
37 public Entry(Entry other) {
38 index = other.index;
39 from = other.from;
40 to = other.to;
41 description = other.description;
42 color = other.color;
35 } 43 }
36 44
37 public Entry( 45 public Entry(
38 int index, 46 int index,
39 double from, 47 double from,
90 } 98 }
91 99
92 public String getDescription() { 100 public String getDescription() {
93 return description; 101 return description;
94 } 102 }
103
104 public boolean isInfinity() {
105 return from == -Double.MAX_VALUE
106 || to == Double.MAX_VALUE;
107 }
95 } // class Entry 108 } // class Entry
96 109
97 protected Entry [] entries; 110 protected Entry [] entries;
98 protected Entry lookup; 111 protected Entry lookup;
99 protected Color [] rgbs; 112 protected Color [] rgbs;
138 } 151 }
139 152
140 buildLookup(); 153 buildLookup();
141 } 154 }
142 155
156 public Palette(Palette original, int N) {
157 if (N < 2) {
158 throw new IllegalArgumentException("N < 2");
159 }
160
161 Entry [] origEntries = original.entries;
162
163 int newSize = 0;
164 for (int i = 0; i < origEntries.length; ++i) {
165 // cannot split infinity intervals
166 newSize += origEntries[i].isInfinity() ? 1 : N;
167 }
168
169 entries = new Entry[newSize];
170 rgbs = new Color[newSize];
171
172 for (int i = 0, j = 0; i < origEntries.length; ++i) {
173 Entry origEntry = origEntries[i];
174 if (origEntry.isInfinity()) {
175 // infinity intervals are just copied
176 Entry nEntry = new Entry(origEntry);
177 entries[j] = nEntry;
178 rgbs[j] = nEntry.color;
179 nEntry.index = j++;
180 }
181 else {
182 // split interval into N parts
183 double from = origEntry.from;
184 double to = origEntry.to;
185 double delta = (to - from)/N;
186 while (from < to) {
187 Entry nEntry = new Entry(origEntry);
188 nEntry.from = from;
189 nEntry.to = from + delta;
190 from += delta;
191 entries[j] = nEntry;
192 rgbs[j] = nEntry.color;
193 nEntry.index = j++;
194 }
195 } // limited interval
196 } // for all original entries
197
198 buildLookup();
199 }
200
143 private static final double doubleValue(String s) { 201 private static final double doubleValue(String s) {
144 if (s == null || (s = s.trim()).length() == 0) { 202 if (s == null || (s = s.trim()).length() == 0) {
145 return 0d; 203 return 0d;
146 } 204 }
147 if ((s = s.toLowerCase()).startsWith("-inf")) { 205 if ((s = s.toLowerCase()).startsWith("-inf")) {
166 protected void buildLookup() { 224 protected void buildLookup() {
167 Arrays.sort(entries); 225 Arrays.sort(entries);
168 lookup = buildLookup(entries, 0, entries.length-1); 226 lookup = buildLookup(entries, 0, entries.length-1);
169 } 227 }
170 228
229 public Palette subdivide(int N) {
230 return new Palette(this, N);
231 }
232
171 public String getDescription() { 233 public String getDescription() {
172 return description; 234 return description;
173 } 235 }
174 236
175 public int getSize() { 237 public int getSize() {

http://dive4elements.wald.intevation.org