View Javadoc
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   * Color by GTU type, based on the GTUType id or the enum id from the defaults.
16   * <p>
17   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 17 jan. 2018 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   */
25  public class GTUTypeColorer implements GTUColorer, Serializable
26  {
27  
28      /** */
29      private static final long serialVersionUID = 20180117L;
30  
31      /** The colors per GTU Type. */
32      private final Map<String, Color> map = new LinkedHashMap<>();
33  
34      /** Index for next default color. */
35      private int nextDefault = 0;
36  
37      /** Defaults colors. */
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      /** Default instance with colors for common GTUTypes. */
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       * Adds a GTU type to the list with color from the default list.
59       * @param gtuTypeEnum GTUType; GTU type
60       * @return this GTUTypeColorer
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       * Adds a GTU type to the list with color based on the type.
75       * @param gtuType GTUType; GTU type
76       * @return this GTUTypeColorer
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       * Adds a GTU type to the list with given color.
91       * @param gtuType GTUType; GTU type
92       * @param color Color; color
93       * @return this GTUTypeColorer
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      * Adds a GTU type based on its enum to the list with given color.
103      * @param gtuTypeEnum GTUType.DEFAULTS; GTU type default enum
104      * @param color Color; color
105      * @return this GTUTypeColorer
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
145     @Override
146     public String toString()
147     {
148         return "GTU Type";
149     }
150 
151 }