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://github.com/peter-knoppers">Peter Knoppers</a>
12   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
13   */
14  public final class Colors
15  {
16  
17      /** 3-color scale from green to red. */
18      public static final Color[] GREEN_RED = new Color[] {Color.GREEN, Color.YELLOW, Color.RED};
19  
20      /** 5-color scale from green to red with dark edges. */
21      public static final Color[] GREEN_RED_DARK =
22              new Color[] {Color.GREEN.darker(), Color.GREEN, Color.YELLOW, Color.RED, Color.RED.darker()};
23  
24      /** 6-color scale from magenta, through red-green, to blue. */
25      public static final Color[] ULTRA =
26              new Color[] {Color.MAGENTA, Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE};
27  
28      /** 10-color scale for enumeration. */
29      public static final Color[] ENUMERATE = new Color[] {Color.BLACK, new Color(0xa5, 0x2a, 0x2a), Color.RED, Color.ORANGE,
30              Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.GRAY, Color.WHITE};
31  
32      /** Names of the enumerated colors. */
33      public static final String[] ENUMERATE_NAMES =
34              new String[] {"black", "brown", "red", "orange", "yellow", "green", "blue", "magenta", "gray", "white"};
35  
36      /**
37       * Constructor.
38       */
39      private Colors()
40      {
41          //
42      }
43  
44      /**
45       * Reverses the color array.
46       * @param colors array of colors
47       * @return reversed color array
48       */
49      public static Color[] reverse(final Color[] colors)
50      {
51          Color[] out = new Color[colors.length];
52          for (int i = 0; i < colors.length; i++)
53          {
54              out[colors.length - i - 1] = colors[i];
55          }
56          return out;
57      }
58  
59      /**
60       * Creates an array of {@code n} colors with varying hue.
61       * @param n number of colors.
62       * @return array of {@code n} colors with varying hue
63       */
64      public static Color[] hue(final int n)
65      {
66          Color[] out = new Color[n];
67          for (int i = 0; i < n; i++)
68          {
69              out[i] = new Color(Color.HSBtoRGB(((float) i) / n, 1.0f, 1.0f));
70          }
71          return out;
72      }
73  
74      /**
75       * Returns a color for the index. Modulo is applied for indices outside of the normal range.
76       * @param index index.
77       * @return color for index.
78       */
79      public static Color getEnumerated(final int index)
80      {
81          return ENUMERATE[mod(index)];
82      }
83  
84      /**
85       * Returns the name of a color for the index. Modulo is applied for indices outside of the normal range.
86       * @param index index.
87       * @return name of color for index.
88       */
89      public static String nameEnumerated(final int index)
90      {
91          return ENUMERATE_NAMES[mod(index)];
92      }
93  
94      /**
95       * Returns the modulo of the index given the number of colors we have.
96       * @param index index.
97       * @return index in range of colors.
98       */
99      private static int mod(final int index)
100     {
101         return Math.abs(index % ENUMERATE.length);
102     }
103 
104     /**
105      * Returns a color from an array, where the used index is determined based on the id. If the last character of the id is a
106      * digit, that value is used. Otherwise it is the absolute hash code of the id. The modulo of either of these values given
107      * the number of colors is the index in the array of the color returned.
108      * @param id object id
109      * @param colors colors to select from
110      * @return color
111      */
112     public static Color getIdColor(final String id, final Color[] colors)
113     {
114         return colors[Character.isDigit(id.charAt(id.length() - 1)) ? id.charAt(id.length() - 1) - '0'
115                 : Math.abs(id.hashCode()) % colors.length];
116     }
117 
118 }