Java Color Create generateNColors(int numColors)

Here you can find the source of generateNColors(int numColors)

Description

Generates a series of colors such that the distribution of the colors is (fairly) evenly spaced throughout the color spectrum.

License

Open Source License

Parameter

Parameter Description
numColors the number of colors to generate

Return

an array of Color objects representing the colors in the table

Declaration

public static Color[] generateNColors(int numColors) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

public class Main {
    /**// w  ww.  j  a  v  a 2 s  .  c  om
     * Generates a series of colors such that the
     * distribution of the colors is (fairly) evenly spaced
     * throughout the color spectrum. This is especially
     * useful for generating unique color codes to be used
     * in a legend or on a graph.
     *
     * @param numColors the number of colors to generate
     * @return an array of Color objects representing the
     * colors in the table
     */
    public static Color[] generateNColors(int numColors) {
        Color[] table = new Color[numColors];

        if (numColors == 1) {
            // Special case for only one color
            table[0] = Color.red;
        } else {
            float hueMax = (float) 0.85;
            float sat = (float) 0.8;

            for (int i = 0; i < numColors; i++) {
                float hue = hueMax * i / (numColors - 1);

                // Here we interleave light colors and dark colors
                // to get a wider distribution of colors.
                if (i % 2 == 0) {
                    table[i] = Color.getHSBColor(hue, sat, (float) 0.9);
                    table[i] = new Color(table[i].getRed(), table[i].getGreen(), table[i].getBlue(), 255);
                } else {
                    table[i] = Color.getHSBColor(hue, sat, (float) 0.7);
                    table[i] = new Color(table[i].getRed(), table[i].getGreen(), table[i].getBlue(), 255);
                }
            }
        }

        return table;
    }
}

Related

  1. generateColourSet(Color[] palette, int numColorBands)
  2. generateFireMapColor(double min, double max, double value)
  3. generateGrayScaleColor(double min, double max, double value)
  4. generateGreyColor(double value, double minValue, double maxValue)
  5. generateHexolor(Color color)
  6. generateNextValidColor()
  7. generatePallete(final int colours)
  8. generateRGBRandomColor()
  9. generateTexturePaint(String text, Font font, Color textColor, Color bgColor, int width, int height)