diff 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
line wrap: on
line diff
--- 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;
     }

http://dive4elements.wald.intevation.org