View Javadoc
1   package org.opentrafficsim.road.gtu.animation;
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.gtu.GTU;
11  import org.opentrafficsim.core.gtu.GTUType;
12  import org.opentrafficsim.core.gtu.animation.GTUColorer;
13  
14  /**
15   * Color by GTU type.
16   * <p>
17   * Copyright (c) 2013-2018 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<GTUType, 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.CAR, Color.BLUE).add(GTUType.TRUCK, Color.RED)
54              .add(GTUType.VEHICLE, Color.GRAY).add(GTUType.PEDESTRIAN, Color.YELLOW).add(GTUType.BICYCLE, Color.GREEN);
55  
56      /**
57       * Adds a GTU type to the list with color from a default list.
58       * @param gtuType GTUType; GTU type
59       * @return this GTUTypeColorer
60       */
61      public GTUTypeColorer add(final GTUType gtuType)
62      {
63          this.map.put(gtuType, standardColors[this.nextDefault]);
64          this.nextDefault++;
65          if (this.nextDefault == standardColors.length)
66          {
67              this.nextDefault = 0;
68          }
69          return this;
70      }
71  
72      /**
73       * Adds a GTU type to the list with given color.
74       * @param gtuType GTUType; GTU type
75       * @param color Color; color
76       * @return this GTUTypeColorer
77       */
78      public GTUTypeColorer add(final GTUType gtuType, final Color color)
79      {
80          this.map.put(gtuType, color);
81          return this;
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public Color getColor(final GTU gtu)
87      {
88          GTUType gtuType = gtu.getGTUType();
89          Color color = this.map.get(gtuType);
90          while (gtuType != null && color == null)
91          {
92              gtuType = gtuType.getParent();
93              color = this.map.get(gtuType);
94          }
95          if (color == null)
96          {
97              return Color.white;
98          }
99          return color;
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public List<LegendEntry> getLegend()
105     {
106         List<LegendEntry> legend = new ArrayList<>();
107         for (GTUType gtuType : this.map.keySet())
108         {
109             String name = gtuType.getId();
110             name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
111             legend.add(new LegendEntry(this.map.get(gtuType), name, name));
112         }
113         return legend;
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public String toString()
119     {
120         return "GTU Type";
121     }
122 
123 }