1 package org.opentrafficsim.animation;
2
3 import java.awt.Color;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6
7 import org.opentrafficsim.core.animation.Drawable;
8
9
10
11
12
13
14
15
16
17
18 public class FixedColorer<D extends Drawable> implements Colorer<D>
19 {
20
21 private final Color color;
22
23
24 private static final Map<Color, FixedColorer<?>> CACHE = new LinkedHashMap<>();
25
26
27 private static final FixedColorer<? extends Drawable> WHITE = new FixedColorer<>(Color.WHITE);
28
29
30 private static final FixedColorer<? extends Drawable> LIGHT_GRAY = new FixedColorer<>(Color.LIGHT_GRAY);
31
32
33 private static final FixedColorer<? extends Drawable> GRAY = new FixedColorer<>(Color.GRAY);
34
35
36 private static final FixedColorer<? extends Drawable> DARK_GRAY = new FixedColorer<>(Color.DARK_GRAY);
37
38
39 private static final FixedColorer<? extends Drawable> BLACK = new FixedColorer<>(Color.BLACK);
40
41
42 private static final FixedColorer<? extends Drawable> RED = new FixedColorer<>(Color.RED);
43
44
45 private static final FixedColorer<? extends Drawable> PINK = new FixedColorer<>(Color.PINK);
46
47
48 private static final FixedColorer<? extends Drawable> ORANGE = new FixedColorer<>(Color.ORANGE);
49
50
51 private static final FixedColorer<? extends Drawable> YELLOW = new FixedColorer<>(Color.YELLOW);
52
53
54 private static final FixedColorer<? extends Drawable> GREEN = new FixedColorer<>(Color.GREEN);
55
56
57 private static final FixedColorer<? extends Drawable> MAGENTA = new FixedColorer<>(Color.MAGENTA);
58
59
60 private static final FixedColorer<? extends Drawable> CYAN = new FixedColorer<>(Color.CYAN);
61
62
63 private static final FixedColorer<? extends Drawable> BLUE = new FixedColorer<>(Color.BLUE);
64
65
66
67
68
69 protected FixedColorer(final Color color)
70 {
71 this.color = color;
72 CACHE.put(color, this);
73 }
74
75
76 @Override
77 public Color getColor(final D drawable)
78 {
79 return this.color;
80 }
81
82
83
84
85
86
87
88 @SuppressWarnings("unchecked")
89 public static <D extends Drawable> FixedColorer<D> create(final Color color)
90 {
91 if (CACHE.containsKey(color))
92 {
93 return (FixedColorer<D>) CACHE.get(color);
94 }
95 return new FixedColorer<D>(color);
96 }
97
98
99
100
101
102 @SuppressWarnings("unchecked")
103 public static <D extends Drawable> FixedColorer<D> black()
104 {
105 return (FixedColorer<D>) BLACK;
106 }
107
108
109
110
111
112 @SuppressWarnings("unchecked")
113 public static <D extends Drawable> FixedColorer<D> blue()
114 {
115 return (FixedColorer<D>) BLUE;
116 }
117
118
119
120
121
122 @SuppressWarnings("unchecked")
123 public static <D extends Drawable> FixedColorer<D> cyan()
124 {
125 return (FixedColorer<D>) CYAN;
126 }
127
128
129
130
131
132 @SuppressWarnings("unchecked")
133 public static <D extends Drawable> FixedColorer<D> darkGray()
134 {
135 return (FixedColorer<D>) DARK_GRAY;
136 }
137
138
139
140
141
142 @SuppressWarnings("unchecked")
143 public static <D extends Drawable> FixedColorer<D> gray()
144 {
145 return (FixedColorer<D>) GRAY;
146 }
147
148
149
150
151
152 @SuppressWarnings("unchecked")
153 public static <D extends Drawable> FixedColorer<D> green()
154 {
155 return (FixedColorer<D>) GREEN;
156 }
157
158
159
160
161
162 @SuppressWarnings("unchecked")
163 public static <D extends Drawable> FixedColorer<D> lightGray()
164 {
165 return (FixedColorer<D>) LIGHT_GRAY;
166 }
167
168
169
170
171
172 @SuppressWarnings("unchecked")
173 public static <D extends Drawable> FixedColorer<D> magenta()
174 {
175 return (FixedColorer<D>) MAGENTA;
176 }
177
178
179
180
181
182 @SuppressWarnings("unchecked")
183 public static <D extends Drawable> FixedColorer<D> orange()
184 {
185 return (FixedColorer<D>) ORANGE;
186 }
187
188
189
190
191
192 @SuppressWarnings("unchecked")
193 public static <D extends Drawable> FixedColorer<D> pink()
194 {
195 return (FixedColorer<D>) PINK;
196 }
197
198
199
200
201
202 @SuppressWarnings("unchecked")
203 public static <D extends Drawable> FixedColorer<D> red()
204 {
205 return (FixedColorer<D>) RED;
206 }
207
208
209
210
211
212 @SuppressWarnings("unchecked")
213 public static <D extends Drawable> FixedColorer<D> white()
214 {
215 return (FixedColorer<D>) WHITE;
216 }
217
218
219
220
221
222 @SuppressWarnings("unchecked")
223 public static <D extends Drawable> FixedColorer<D> yellow()
224 {
225 return (FixedColorer<D>) YELLOW;
226 }
227
228 }