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