Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Color;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Generates a list of colors. The result is always identical.
     * 
     * @param amount The number of colors to calculate.
     * @return A list of colors. Never returns <code>null</code>.
     * @see https://stackoverflow.com/questions/3403826/how-to-dynamically-compute-a-list-of-colors
     */
    private static List<Color> calculateUniqueColors(int amount) {
        final int lowerLimit = 0x10;
        final int upperLimit = 0xE0;
        final int colorStep = (int) ((upperLimit - lowerLimit) / Math.pow(amount, 1f / 3));

        final List<Color> colors = new ArrayList<>(amount);

        for (int R = lowerLimit; R < upperLimit; R += colorStep)
            for (int G = lowerLimit; G < upperLimit; G += colorStep)
                for (int B = lowerLimit; B < upperLimit; B += colorStep) {
                    if (colors.size() >= amount) { // The calculated step is not very precise, so this safeguard is appropriate
                        return colors;
                    } else {
                        int color = (R << 16) + (G << 8) + (B);
                        colors.add(new Color(color));
                    }
                }
        return colors;
    }
}