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.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
11 import org.opentrafficsim.core.gtu.GTU;
12 import org.opentrafficsim.core.gtu.GTUType;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class GTUTypeColorer implements GTUColorer, Serializable
26 {
27
28
29 private static final long serialVersionUID = 20180117L;
30
31
32 private final Map<String, Color> map = new LinkedHashMap<>();
33
34
35 private int nextDefault = 0;
36
37
38 private static Color[] standardColors = new Color[10];
39 {
40 standardColors[0] = Color.BLACK;
41 standardColors[1] = new Color(0xa5, 0x2a, 0x2a);
42 standardColors[2] = Color.RED;
43 standardColors[3] = Color.ORANGE;
44 standardColors[4] = Color.YELLOW;
45 standardColors[5] = Color.GREEN;
46 standardColors[6] = Color.BLUE;
47 standardColors[7] = Color.MAGENTA;
48 standardColors[8] = Color.GRAY;
49 standardColors[9] = Color.WHITE;
50 }
51
52
53 public static final GTUTypeColorer DEFAULT = new GTUTypeColorer().add(GTUType.DEFAULTS.CAR, Color.BLUE)
54 .add(GTUType.DEFAULTS.TRUCK, Color.RED).add(GTUType.DEFAULTS.VEHICLE, Color.GRAY)
55 .add(GTUType.DEFAULTS.PEDESTRIAN, Color.YELLOW).add(GTUType.DEFAULTS.BICYCLE, Color.GREEN);
56
57
58
59
60
61
62 public GTUTypeColorer add(final GTUType.DEFAULTS gtuTypeEnum)
63 {
64 this.map.put(gtuTypeEnum.getId(), standardColors[this.nextDefault]);
65 this.nextDefault++;
66 if (this.nextDefault == standardColors.length)
67 {
68 this.nextDefault = 0;
69 }
70 return this;
71 }
72
73
74
75
76
77
78 public GTUTypeColorer add(final GTUType gtuType)
79 {
80 this.map.put(gtuType.getId(), standardColors[this.nextDefault]);
81 this.nextDefault++;
82 if (this.nextDefault == standardColors.length)
83 {
84 this.nextDefault = 0;
85 }
86 return this;
87 }
88
89
90
91
92
93
94
95 public GTUTypeColorer add(final GTUType gtuType, final Color color)
96 {
97 this.map.put(gtuType.getId(), color);
98 return this;
99 }
100
101
102
103
104
105
106
107 public GTUTypeColorer add(final GTUType.DEFAULTS gtuTypeEnum, final Color color)
108 {
109 this.map.put(gtuTypeEnum.getId(), color);
110 return this;
111 }
112
113
114 @Override
115 public Color getColor(final GTU gtu)
116 {
117 GTUType gtuType = gtu.getGTUType();
118 Color color = this.map.get(gtuType.getId());
119 while (gtuType != null && color == null)
120 {
121 gtuType = gtuType.getParent();
122 color = this.map.get(gtuType.getId());
123 }
124 if (color == null)
125 {
126 return Color.white;
127 }
128 return color;
129 }
130
131
132 @Override
133 public List<LegendEntry> getLegend()
134 {
135 List<LegendEntry> legend = new ArrayList<>();
136 for (String name : this.map.keySet())
137 {
138 name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
139 legend.add(new LegendEntry(this.map.get(name), name, name));
140 }
141 return legend;
142 }
143
144
145 @Override
146 public String toString()
147 {
148 return "GTU Type";
149 }
150
151 }