View Javadoc
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   * FixedColorer stores a fixed color for drawing.
11   * <p>
12   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
16   * @param <D> the Drawable type
17   */
18  public class FixedColorer<D extends Drawable> implements Colorer<D>
19  {
20      /** the fixed color to use. */
21      private final Color color;
22  
23      /** cache for colors, so they're only stored once. */
24      private static final Map<Color, FixedColorer<?>> CACHE = new LinkedHashMap<>();
25  
26      /** The color white. */
27      private static final FixedColorer<? extends Drawable> WHITE = new FixedColorer<>(Color.WHITE);
28  
29      /** The color light gray. */
30      private static final FixedColorer<? extends Drawable> LIGHT_GRAY = new FixedColorer<>(Color.LIGHT_GRAY);
31  
32      /** The color gray. */
33      private static final FixedColorer<? extends Drawable> GRAY = new FixedColorer<>(Color.GRAY);
34  
35      /** The color dark gray. */
36      private static final FixedColorer<? extends Drawable> DARK_GRAY = new FixedColorer<>(Color.DARK_GRAY);
37  
38      /** The color black. */
39      private static final FixedColorer<? extends Drawable> BLACK = new FixedColorer<>(Color.BLACK);
40  
41      /** The color red. */
42      private static final FixedColorer<? extends Drawable> RED = new FixedColorer<>(Color.RED);
43  
44      /** The color pink. */
45      private static final FixedColorer<? extends Drawable> PINK = new FixedColorer<>(Color.PINK);
46  
47      /** The color orange. */
48      private static final FixedColorer<? extends Drawable> ORANGE = new FixedColorer<>(Color.ORANGE);
49  
50      /** The color yellow. */
51      private static final FixedColorer<? extends Drawable> YELLOW = new FixedColorer<>(Color.YELLOW);
52  
53      /** The color green. */
54      private static final FixedColorer<? extends Drawable> GREEN = new FixedColorer<>(Color.GREEN);
55  
56      /** The color magenta. */
57      private static final FixedColorer<? extends Drawable> MAGENTA = new FixedColorer<>(Color.MAGENTA);
58  
59      /** The color cyan. */
60      private static final FixedColorer<? extends Drawable> CYAN = new FixedColorer<>(Color.CYAN);
61  
62      /** The color blue. */
63      private static final FixedColorer<? extends Drawable> BLUE = new FixedColorer<>(Color.BLUE);
64  
65      /**
66       * Initialize the FixedColorer with a color.
67       * @param color Color; the fixed color to use
68       */
69      protected FixedColorer(final Color color)
70      {
71          this.color = color;
72          CACHE.put(color, this);
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public Color getColor(final D drawable)
78      {
79          return this.color;
80      }
81  
82      /**
83       * Instantiate a singleton fixed colorer for a certain color.
84       * @param color Color; the fixed color to use
85       * @return the FixedColorer
86       * @param <D> the Drawable type
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       * @param <D> the Drawable type
100      * @return black color
101      */
102     @SuppressWarnings("unchecked")
103     public static <D extends Drawable> FixedColorer<D> black()
104     {
105         return (FixedColorer<D>) BLACK;
106     }
107 
108     /**
109      * @param <D> the Drawable type
110      * @return blue color
111      */
112     @SuppressWarnings("unchecked")
113     public static <D extends Drawable> FixedColorer<D> blue()
114     {
115         return (FixedColorer<D>) BLUE;
116     }
117 
118     /**
119      * @param <D> the Drawable type
120      * @return cyan color
121      */
122     @SuppressWarnings("unchecked")
123     public static <D extends Drawable> FixedColorer<D> cyan()
124     {
125         return (FixedColorer<D>) CYAN;
126     }
127 
128     /**
129      * @param <D> the Drawable type
130      * @return darkGray color
131      */
132     @SuppressWarnings("unchecked")
133     public static <D extends Drawable> FixedColorer<D> darkGray()
134     {
135         return (FixedColorer<D>) DARK_GRAY;
136     }
137 
138     /**
139      * @param <D> the Drawable type
140      * @return gray color
141      */
142     @SuppressWarnings("unchecked")
143     public static <D extends Drawable> FixedColorer<D> gray()
144     {
145         return (FixedColorer<D>) GRAY;
146     }
147 
148     /**
149      * @param <D> the Drawable type
150      * @return green color
151      */
152     @SuppressWarnings("unchecked")
153     public static <D extends Drawable> FixedColorer<D> green()
154     {
155         return (FixedColorer<D>) GREEN;
156     }
157 
158     /**
159      * @param <D> the Drawable type
160      * @return lightGray color
161      */
162     @SuppressWarnings("unchecked")
163     public static <D extends Drawable> FixedColorer<D> lightGray()
164     {
165         return (FixedColorer<D>) LIGHT_GRAY;
166     }
167 
168     /**
169      * @param <D> the Drawable type
170      * @return magenta color
171      */
172     @SuppressWarnings("unchecked")
173     public static <D extends Drawable> FixedColorer<D> magenta()
174     {
175         return (FixedColorer<D>) MAGENTA;
176     }
177 
178     /**
179      * @param <D> the Drawable type
180      * @return orange color
181      */
182     @SuppressWarnings("unchecked")
183     public static <D extends Drawable> FixedColorer<D> orange()
184     {
185         return (FixedColorer<D>) ORANGE;
186     }
187 
188     /**
189      * @param <D> the Drawable type
190      * @return pink color
191      */
192     @SuppressWarnings("unchecked")
193     public static <D extends Drawable> FixedColorer<D> pink()
194     {
195         return (FixedColorer<D>) PINK;
196     }
197 
198     /**
199      * @param <D> the Drawable type
200      * @return red color
201      */
202     @SuppressWarnings("unchecked")
203     public static <D extends Drawable> FixedColorer<D> red()
204     {
205         return (FixedColorer<D>) RED;
206     }
207 
208     /**
209      * @param <D> the Drawable type
210      * @return white color
211      */
212     @SuppressWarnings("unchecked")
213     public static <D extends Drawable> FixedColorer<D> white()
214     {
215         return (FixedColorer<D>) WHITE;
216     }
217 
218     /**
219      * @param <D> the Drawable type
220      * @return yellow color
221      */
222     @SuppressWarnings("unchecked")
223     public static <D extends Drawable> FixedColorer<D> yellow()
224     {
225         return (FixedColorer<D>) YELLOW;
226     }
227 
228 }