View Javadoc
1   package org.opentrafficsim.draw;
2   
3   import java.awt.Color;
4   
5   /**
6    * List of colors to use for various legends.
7    * <p>
8    * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
10   * </p>
11   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
12   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
13   */
14  public final class Colors
15  {
16  
17      /** List of colors. */
18      public static Color[] COLORS = new Color[] {Color.BLACK, new Color(0xa5, 0x2a, 0x2a), Color.RED, Color.ORANGE, Color.YELLOW,
19              Color.GREEN, Color.BLUE, Color.MAGENTA, Color.GRAY, Color.WHITE};
20  
21      /** Names of the colors. */
22      public static String[] NAMES =
23              new String[] {"black", "brown", "red", "orange", "yellow", "green", "blue", "magenta", "gray", "white"};
24  
25      /**
26       * Returns a color for the index. Modulo is applied for indices outside of the normal range.
27       * @param index int; index.
28       * @return Color; color for index.
29       */
30      public static Color get(final int index)
31      {
32          return COLORS[mod(index)];
33      }
34  
35      /**
36       * Returns the name of a color for the index. Modulo is applied for indices outside of the normal range.
37       * @param index int; index.
38       * @return String; name of color for index.
39       */
40      public static String name(final int index)
41      {
42          return NAMES[mod(index)];
43      }
44  
45      /**
46       * Returns the modulo of the index given the number of colors we have.
47       * @param index int; index.
48       * @return int; index in range of colors.
49       */
50      private static int mod(final int index)
51      {
52          return Math.abs(index % COLORS.length);
53      }
54  
55  }