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