]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicColorMap.java
09cb57943f6865ae8dd6f251c737c04d88f44588
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / visualisations / model / DynamicColorMap.java
1 package org.simantics.district.network.visualisations.model;
2
3 import java.awt.Color;
4 import java.util.Arrays;
5 import java.util.List;
6
7 public class DynamicColorMap {
8     
9     static List<RGBIntensity> blues = Arrays.asList(new RGBIntensity(0, 0, 0.1), new RGBIntensity(0, 0, 0.5), new RGBIntensity(0, 0, 0.9));
10
11     public static final DynamicColorMap DEFAULT = new DynamicColorMap("default", blues);
12     
13     private String label;
14     private List<RGBIntensity> intensities;
15     
16     public DynamicColorMap(String label, List<RGBIntensity> intensities) {
17         this.label = label;
18         this.intensities = intensities;
19     }
20
21     public String getLabel() {
22         return label;
23     }
24
25     public List<RGBIntensity> getIntensities() {
26         return intensities;
27     }
28
29     public static class RGBIntensity {
30         
31         private double red;
32         private double green;
33         private double blue;
34         
35         public RGBIntensity(double red, double green, double blue) {
36             this.red = red;
37             this.green = green;
38             this.blue = blue;
39         }
40
41         public double getRed() {
42             return red;
43         }
44
45         public double getGreen() {
46             return green;
47         }
48
49         public double getBlue() {
50             return blue;
51         }
52     }
53
54     public Color getColor(double value, double defaultMin, double defaultMax) {
55         
56         double gap = defaultMax - defaultMin;
57         double singleGap = gap / getIntensities().size();
58         
59         int i = 0;
60         while (i < getIntensities().size() - 1) {
61             if (value <= defaultMin + (i * singleGap)) {
62                 break;
63             }
64             i++;
65         }
66         
67         RGBIntensity intensity = getIntensities().get(i);
68         return new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue());
69     }
70 }