1 package org.opentrafficsim.road.gtu.colorer;
2
3 import java.awt.Color;
4 import java.io.Serializable;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.djunits.value.vdouble.scalar.Duration;
9 import org.opentrafficsim.base.parameters.ParameterTypes;
10 import org.opentrafficsim.base.parameters.Parameters;
11 import org.opentrafficsim.core.animation.ColorInterpolator;
12 import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
13 import org.opentrafficsim.core.gtu.GTU;
14
15
16
17
18
19
20
21
22
23
24
25
26 public class DesiredHeadwayColorer implements GTUColorer, Serializable
27 {
28
29
30 private static final long serialVersionUID = 20170420L;
31
32
33 public final Duration tMin;
34
35
36 public final Duration tMax;
37
38
39 private final List<LegendEntry> legend;
40
41
42 private static final Color LOW = Color.RED;
43
44
45 private static final Color MIDDLE = Color.YELLOW;
46
47
48 private static final Color HIGH = Color.GREEN;
49
50
51 protected static final Color UNKNOWN = Color.WHITE;
52
53
54
55
56 public DesiredHeadwayColorer()
57 {
58 this.legend = new ArrayList<>(4);
59 this.legend.add(new LegendEntry(LOW, "Tmin", "Tmin"));
60 this.legend.add(new LegendEntry(MIDDLE, "Mean", "Mean"));
61 this.legend.add(new LegendEntry(HIGH, "Tmax", "Tmax"));
62 this.legend.add(new LegendEntry(UNKNOWN, "Unknown", "Unknown"));
63 this.tMin = null;
64 this.tMax = null;
65 }
66
67
68
69
70
71
72 public DesiredHeadwayColorer(final Duration tMin, final Duration tMax)
73 {
74 String format = "%.2fs";
75 this.legend = new ArrayList<>(4);
76 this.legend.add(new LegendEntry(LOW, String.format(format, tMin.si), "Tmin"));
77 this.legend.add(new LegendEntry(MIDDLE, String.format(format, (tMin.si + tMax.si) / 2.0), "Mean"));
78 this.legend.add(new LegendEntry(HIGH, String.format(format, tMax.si), "Tmax"));
79 this.legend.add(new LegendEntry(UNKNOWN, "Unknown", "Unknown"));
80 this.tMin = tMin;
81 this.tMax = tMax;
82 }
83
84
85 @Override
86 public final Color getColor(final GTU gtu)
87 {
88 Parameters params = gtu.getParameters();
89 Double minT;
90 Double maxT;
91 if (this.tMin == null)
92 {
93 minT = params.getParameterOrNull(ParameterTypes.TMIN).si;
94 maxT = params.getParameterOrNull(ParameterTypes.TMAX).si;
95 }
96 else
97 {
98 minT = this.tMin.si;
99 maxT = this.tMax.si;
100 }
101 Double t = params.getParameterOrNull(ParameterTypes.T).si;
102 if (minT == null || maxT == null || t == null)
103 {
104 return UNKNOWN;
105 }
106 if (t <= minT)
107 {
108 return LOW;
109 }
110 if (t >= maxT)
111 {
112 return HIGH;
113 }
114 double tMean = (minT + maxT) / 2.0;
115 if (t < tMean)
116 {
117 return ColorInterpolator.interpolateColor(LOW, MIDDLE, (t - minT) / (tMean - minT));
118 }
119 return ColorInterpolator.interpolateColor(MIDDLE, HIGH, (t - tMean) / (maxT - tMean));
120 }
121
122
123 @Override
124 public final List<LegendEntry> getLegend()
125 {
126 return this.legend;
127 }
128
129
130 @Override
131 public final String toString()
132 {
133 return "Desired headway";
134 }
135
136 }