package org.skyve.metadata.view.model.chart.colours;

import java.awt.Color;

import org.skyve.metadata.view.model.chart.colours.rainbow.Rainbow;
import org.skyve.metadata.view.model.chart.colours.rainbow.RainbowException;

/**
 * Produce a gradient of colours between the default or a specified spectrum (min 2 colours).
 * 
 * @author ben
 */
public class RainbowColourSeries extends ColourSeries {

	private Rainbow rainbow;
	private int currentRainbowIndex = 1;
	private String[] spectrum = { "ff6385", "ffa040", "ffcc56", "4bc0c0", "36a3eb", "9966ff" };

	public RainbowColourSeries() {
		this.rainbow = new Rainbow();
		rainbow.setSpectrum(spectrum);
		rainbow.setNumberRange(0, size < spectrum.length ? spectrum.length : size);
		current = getColor(0);
	}

	public RainbowColourSeries(String... spectrum) {
		this();
		if (spectrum.length > 0) {
			this.spectrum = spectrum;
			rainbow.setSpectrum(spectrum);
			rainbow.setNumberRange(0, size < spectrum.length ? spectrum.length : size);
		}
		current = getColor(0);
	}

	@Override
	public Color next() {
		current = getColor(++currentRainbowIndex);
		return current;
	}

	@Override
	public void setSize(int size) {
		super.setSize(size);
		rainbow.setNumberRange(0, size < spectrum.length ? spectrum.length : size);
	}

	/**
	 * Sets the spectrum of the Rainbow. By default, the spectrum is a rainbow between the
	 * default chartjs colours. You must have a minimum of two colours, but you can specify
	 * more than two colours.
	 * 
	 * Colours can be in the form "red", "ff0000", or "#ff0000".
	 * For example, <code>rainbowColourSeries.setSpectrum("red", "yellow", "white");</code>
	 * makes the "Rainbow" a colour gradient from red to yellow to white.
	 * 
	 * @param spectrum Two or more Strings representing HTML colours,
	 *        or pass in a whole String array of length 2 or greater
	 * @throws RainbowException if there is less than two arguments or if one of the arguments
	 *         is an invalid colour
	 */
	public void setSpectrum(String... spectrum) throws RainbowException {
		this.rainbow.setSpectrum(spectrum);
	}

	@SuppressWarnings("boxing")
	private Color getColor(int index) {
		String hex = rainbow.colorAt(index);
		return new Color(
				Integer.valueOf(hex.substring(0, 2), 16),
				Integer.valueOf(hex.substring(2, 4), 16),
				Integer.valueOf(hex.substring(4, 6), 16));
	}
}

@@ -69,16 +69,41 @@
 t.Color;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -573,16 +573,26 @@
 %229966ff%22
+, %22c9cbcf%22
  %7D;%0A%0A%09pu
@@ -1201,16 +1201,16 @@
 size) %7B%0A
-
 %09%09super.
@@ -1224,16 +1224,151 @@
 (size);%0A
+%0A%09%09if (size %3C= 10) %7B%0A%09%09%09// trim the spectrum if 10 or fewer%0A%09%09%09rainbow.setSpectrum(Arrays.copyOf(spectrum, spectrum.length - 1));%0A%09%09%7D%0A%0A
 %09%09rainbo

Update RainbowColourSeries with extra colour for default spectrum which is included when the size is > 10