001    package org.hackystat.projectbrowser.page.telemetry.datapanel;
002    
003    import java.awt.Color;
004    import java.util.ArrayList;
005    import java.util.List;
006    import java.util.Random;
007    
008    /**
009     * Color generator to generate colors with maximum distinguish.
010     * @author Shaoxuan Zhang
011     *
012     */
013    public class RandomColorGenerator {
014    
015      /**
016       * Generate n random colors, guarantee them have maximum difference from each other.
017       * @param n the number of colors to generate.
018       * @return a list of Color instances.
019       */
020      public static List<Color> generateRandomColor(int n) {
021        float diff = (float)1 / n;
022        Random rand = new Random();
023        float h = (float)rand.nextDouble();
024        List<Color> colors = new ArrayList<Color>();
025        for (int i = 0; i < n; ++i) {
026          colors.add(Color.getHSBColor(
027              h, (float)(rand.nextDouble() / 2 + 0.5), (float)(rand.nextDouble() / 2 + 0.5)));
028          h += diff;
029        }
030        return colors;
031      }
032      
033      /**
034       * Generate n random colors as Strings in RRGGBB format, 
035       * guarantee them have maximum difference from each other.
036       * @param n the number of colors to generate.
037       * @return a list of Strings that represent the colors.
038       */
039      public static List<String> generateRandomColorInHex(int n) {
040        List<Color> colors = generateRandomColor(n);
041        List<String> hexColors = new ArrayList<String>();
042        for (Color color : colors) {
043          hexColors.add(Integer.toHexString(color.getRGB()).substring(2));
044        }
045        return hexColors;
046      }
047    }