# HG changeset patch # User Sascha L. Teichmann # Date 1261742414 0 # Node ID 67091b17462d2c776c317d649f03969a33e1d9bb # Parent 0eed5749fd6358b63780a4434c4e4e52ac657606 Added code to split palette interval into equal sized parts. gnv-artifacts/trunk@483 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 0eed5749fd63 -r 67091b17462d gnv-artifacts/ChangeLog --- a/gnv-artifacts/ChangeLog Wed Dec 23 15:28:40 2009 +0000 +++ b/gnv-artifacts/ChangeLog Fri Dec 25 12:00:14 2009 +0000 @@ -1,3 +1,12 @@ +2009-12-25 Sascha L. Teichmann + + * src/main/java/de/intevation/gnv/raster/Palette.java: Added + method subdive(N) to Palette class which creates a new + palette in which each interval is splitted into N + equal sized intervals. Infinity sized intervals are not + splitted. This is useful to fulfill the conditions of + gnv/issue108. + 2009-12-23 Sascha L. Teichmann * src/main/java/de/intevation/gnv/math/Interpolation2D.java: diff -r 0eed5749fd63 -r 67091b17462d gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java Wed Dec 23 15:28:40 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java Fri Dec 25 12:00:14 2009 +0000 @@ -34,6 +34,14 @@ public Entry() { } + public Entry(Entry other) { + index = other.index; + from = other.from; + to = other.to; + description = other.description; + color = other.color; + } + public Entry( int index, double from, @@ -92,6 +100,11 @@ public String getDescription() { return description; } + + public boolean isInfinity() { + return from == -Double.MAX_VALUE + || to == Double.MAX_VALUE; + } } // class Entry protected Entry [] entries; @@ -140,6 +153,51 @@ buildLookup(); } + public Palette(Palette original, int N) { + if (N < 2) { + throw new IllegalArgumentException("N < 2"); + } + + Entry [] origEntries = original.entries; + + int newSize = 0; + for (int i = 0; i < origEntries.length; ++i) { + // cannot split infinity intervals + newSize += origEntries[i].isInfinity() ? 1 : N; + } + + entries = new Entry[newSize]; + rgbs = new Color[newSize]; + + for (int i = 0, j = 0; i < origEntries.length; ++i) { + Entry origEntry = origEntries[i]; + if (origEntry.isInfinity()) { + // infinity intervals are just copied + Entry nEntry = new Entry(origEntry); + entries[j] = nEntry; + rgbs[j] = nEntry.color; + nEntry.index = j++; + } + else { + // split interval into N parts + double from = origEntry.from; + double to = origEntry.to; + double delta = (to - from)/N; + while (from < to) { + Entry nEntry = new Entry(origEntry); + nEntry.from = from; + nEntry.to = from + delta; + from += delta; + entries[j] = nEntry; + rgbs[j] = nEntry.color; + nEntry.index = j++; + } + } // limited interval + } // for all original entries + + buildLookup(); + } + private static final double doubleValue(String s) { if (s == null || (s = s.trim()).length() == 0) { return 0d; @@ -168,6 +226,10 @@ lookup = buildLookup(entries, 0, entries.length-1); } + public Palette subdivide(int N) { + return new Palette(this, N); + } + public String getDescription() { return description; }