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;
}
}
- 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());
+ }
}
}