]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicColorMap.java
Add support for gradients in dynamic visualisations
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / visualisations / model / DynamicColorMap.java
index 09cb57943f6865ae8dd6f251c737c04d88f44588..4c4580979ef6f613c83c776d0f44819fbe4f2e16 100644 (file)
@@ -1,15 +1,10 @@
 package org.simantics.district.network.visualisations.model;
 
 import java.awt.Color;
-import java.util.Arrays;
 import java.util.List;
 
 public class DynamicColorMap {
     
-    static List<RGBIntensity> blues = Arrays.asList(new RGBIntensity(0, 0, 0.1), new RGBIntensity(0, 0, 0.5), new RGBIntensity(0, 0, 0.9));
-
-    public static final DynamicColorMap DEFAULT = new DynamicColorMap("default", blues);
-    
     private String label;
     private List<RGBIntensity> intensities;
     
@@ -51,20 +46,49 @@ public class DynamicColorMap {
         }
     }
 
-    public Color getColor(double value, double defaultMin, double defaultMax) {
+    public Color getColor(double value, boolean useGradient, double defaultMin, double defaultMax) {
+        List<RGBIntensity> intensities = getIntensities();
         
         double gap = defaultMax - defaultMin;
-        double singleGap = gap / getIntensities().size();
+        double singleGap = gap / intensities.size();
         
+        double threshold = defaultMin;
         int i = 0;
-        while (i < getIntensities().size() - 1) {
-            if (value <= defaultMin + (i * singleGap)) {
+        while (i < intensities.size() - 1) {
+            threshold = threshold + singleGap;
+            if (value <= threshold) {
                 break;
             }
             i++;
         }
         
-        RGBIntensity intensity = getIntensities().get(i);
-        return new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue());
+        RGBIntensity intensity = intensities.get(i);
+        if (useGradient) {
+            
+            RGBIntensity upperLimitIntensity;
+            if (i + 1 != intensities.size())
+                upperLimitIntensity = intensities.get(i + 1);
+            else
+                upperLimitIntensity = intensity;
+            
+            double minRed = intensity.getRed();
+            double minGreen = intensity.getGreen();
+            double minBlue = intensity.getBlue();
+            
+            double maxRed = upperLimitIntensity.getRed();
+            double maxGreen = upperLimitIntensity.getGreen();
+            double maxBlue = upperLimitIntensity.getBlue();
+            
+            double delta = Math.max(value - (defaultMin + singleGap * i), 0);
+            
+            double d = delta / singleGap;
+            
+            double redDelta = (maxRed - minRed) * d;
+            double greenDelta = (maxGreen - minGreen) * d;
+            double blueDelta = (maxBlue - minBlue) * d;
+            return new Color((float)(minRed + redDelta), (float)(minGreen + greenDelta), (float)(minBlue + blueDelta));
+        } else {
+            return new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue());
+        }
     }
 }