DesireColorer.java

  1. package org.opentrafficsim.road.gtu.colorer;

  2. import java.awt.Color;
  3. import java.io.Serializable;
  4. import java.util.ArrayList;
  5. import java.util.List;

  6. import org.opentrafficsim.core.animation.ColorInterpolator;
  7. import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;

  8. /**
  9.  * Super class with default coloring of left and right desire value.
  10.  * <p>
  11.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 13 apr. 2017 <br>
  15.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  17.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  18.  */
  19. public abstract class DesireColorer implements GTUColorer, Serializable
  20. {

  21.     /** */
  22.     private static final long serialVersionUID = 20171304L;

  23.     /** The legend. */
  24.     private static final List<LegendEntry> LEGEND;

  25.     /** Left color. */
  26.     private static final Color LEFT = Color.RED;

  27.     /** No-left color. */
  28.     private static final Color NO_LEFT = Color.MAGENTA;

  29.     /** Right color. */
  30.     private static final Color RIGHT = Color.BLUE;

  31.     /** No-right color. */
  32.     private static final Color NO_RIGHT = Color.CYAN;

  33.     /** None color. */
  34.     private static final Color NONE = Color.WHITE;

  35.     /** N/A color. */
  36.     protected static final Color NA = Color.YELLOW;

  37.     static
  38.     {
  39.         LEGEND = new ArrayList<>(6);
  40.         LEGEND.add(new LegendEntry(LEFT, "Left", "Full left: 1.0"));
  41.         LEGEND.add(new LegendEntry(NO_LEFT, "Not left", "Full no-left: -1.0"));
  42.         LEGEND.add(new LegendEntry(NONE, "None", "None: 0.0"));
  43.         LEGEND.add(new LegendEntry(RIGHT, "Right", "Full right: 1.0"));
  44.         LEGEND.add(new LegendEntry(NO_RIGHT, "Not right", "Full no-right: -1.0"));
  45.         LEGEND.add(new LegendEntry(NA, "N/A", "N/A"));
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     public final List<LegendEntry> getLegend()
  50.     {
  51.         return LEGEND;
  52.     }

  53.     /**
  54.      * Returns a color based on desire.
  55.      * @param dLeft double; left desire
  56.      * @param dRight double; right desire
  57.      * @return color based on desire
  58.      */
  59.     protected final Color getColor(final double dLeft, final double dRight)
  60.     {
  61.         Color target;
  62.         double f;
  63.         if (Math.abs(dLeft) >= Math.abs(dRight))
  64.         {
  65.             if (dLeft < 0)
  66.             {
  67.                 target = NO_LEFT;
  68.                 f = dLeft < -1.0 ? 1.0 : -dLeft;
  69.             }
  70.             else
  71.             {
  72.                 target = LEFT;
  73.                 f = dLeft > 1.0 ? 1.0 : dLeft;
  74.             }
  75.         }
  76.         else
  77.         {
  78.             if (dRight < 0)
  79.             {
  80.                 target = NO_RIGHT;
  81.                 f = dRight < -1.0 ? 1.0 : -dRight;
  82.             }
  83.             else
  84.             {
  85.                 target = RIGHT;
  86.                 f = dRight > 1.0 ? 1.0 : dRight;
  87.             }
  88.         }
  89.         return ColorInterpolator.interpolateColor(NONE, target, f);
  90.     }

  91. }