/*
 * GRAL: GRAphing Library for Java(R)
 *
 * (C) Copyright 2009-2016 Erich Seifert ,
 * Michael Seifert 
 *
 * This file is part of GRAL.
 *
 * GRAL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GRAL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GRAL.  If not, see .
 */
package de.erichseifert.gral.plots.colors;

import java.awt.Color;
import java.awt.Paint;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

import de.erichseifert.gral.util.MathUtils;

/**
 * Class that generates pseudo-random colors for specified index values.
 */
public class RandomColors extends IndexedColorMapper {
	/** Version id for serialization. */
	private static final long serialVersionUID = -4518470000665474457L;

	/** Number of comparisons that will be done before accepting two similar
	random values. */
	private static final int NUM_COMPARISONS = 4;
	/** Minimal distance the causes a random values to be to re-generated. */
	private static final double MIN_DIST = 0.3;

	/** Cache for colors that have already been generated. */
	private final Map colorCache;
	/** Object for generating random values. */
	private final Random random;
	/** Variance settings for hue, saturation and brightness. */
	//FIXME duplicate code! See QuasiRandomColors
	private final float[] colorVariance;

	/**
	 * Creates a new RandomColors object with default seed.
	 */
	public RandomColors() {
		random = new Random();
		colorCache = new LinkedHashMap();
		colorVariance = new float[] {
			0.00f, 1.00f,  // Hue
			0.75f, 0.25f,  // Saturation
			0.25f, 0.75f   // Brightness
		};
	}

	/**
	 * Creates a new instances with the specified seed.
	 * @param seed Random number seed.
	 */
	public RandomColors(long seed) {
		this();
		random.setSeed(seed);
	}

	/**
	 * Returns the Paint associated to the specified index value.
	 * @param index Numeric index.
	 * @return Paint.
	 */
	@Override
	public Paint get(int index) {
		Integer key = index;
		if (colorCache.containsKey(key)) {
			return colorCache.get(key);
		}

		// Use the same random numbers for the same input value
		//long seed = Double.doubleToRawLongBits(value);
		//random.setSeed(seed);

		// Generate a new color that is distant enough from previous colors
		boolean match;
		Color r;
		do {
			r = getRandomColor();
			match = true;
			Iterator colors = colorCache.values().iterator();
			for (int i=0; i 

Ads help maintain this website.