package gui;

import java.util.ArrayList;
import java.util.List;

public class ColorGenerator {

	public static class Color {
		public float r, g, b;

		public Color(float r, float g, float b) {
			this.r = r;
			this.g = g;
			this.b = b;
		}

	}

	public static List<Color> greatPalette(int colorCount) {
		List<Color> colors = greatScheme(colorCount);
		List<Color> newOrder = new ArrayList<>();
		int newIndex = 0;
		while (!colors.isEmpty()) {
			int randomIndex = newIndex % colors.size();
			newOrder.add(colors.get(randomIndex));
			colors.remove(randomIndex);
			newIndex += Math.sqrt(colorCount) + 1;
		}
		return newOrder;
	}

	public static List<Color> greatScheme(int colorCount) {
		List<Color> colors = new ArrayList<>();
		for (int i = 0; i < colorCount; i++) {
			colors.add(hslToRgb((float) (i) / (float) colorCount, 0.9f, 0.4f));
		}
		return colors;
	}

	public static Color hslToRgb(float h, float s, float l){
		float r, g, b;
	
		if (s == 0f) {
			r = g = b = l; // achromatic
		} else {
			float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
			float p = 2 * l - q;
			r = hueToRgb(p, q, h + 1f/3f);
			g = hueToRgb(p, q, h);
			b = hueToRgb(p, q, h - 1f/3f);
		}
		return new Color(r, g, b);
	}

	/** Helper method that converts hue to rgb */
	public static float hueToRgb(float p, float q, float t) {
		if (t < 0f)
			t += 1f;
		if (t > 1f)
			t -= 1f;
		if (t < 1f / 6f)
			return p + (q - p) * 6f * t;
		if (t < 1f / 2f)
			return q;
		if (t < 2f / 3f)
			return p + (q - p) * (2f / 3f - t) * 6f;
		return p;
	}

}
