import java.awt.Color;

public class ColorScale {

	public enum ColorSpace {
		RGB, HSB
	}

	private Color minColor;

	private Color maxColor;

	private ColorSpace colorSpace;

	public ColorScale(Color minC, Color maxC, ColorSpace cSpace) {
		setMinColor(minC);
		setMaxColor(maxC);
		setColorSpace(cSpace);
	}

	public Color getInterpolatedColor(float t) {
		return (colorSpace == ColorSpace.RGB) ? ColorScale.interpolateRGB(minColor, maxColor, t) : ColorScale.interpolateHSB(minColor, maxColor, t);
	}

	public void setMinColor(Color minColor) {
		this.minColor = minColor;
	}

	public Color getMinColor() {
		return minColor;
	}

	public void setMaxColor(Color maxColor) {
		this.maxColor = maxColor;
	}

	public Color getMaxColor() {
		return maxColor;
	}

	public void setColorSpace(ColorSpace colorSpace) {
		this.colorSpace = colorSpace;
	}

	public ColorSpace getColorSpace() {
		return colorSpace;
	}

	public static Color interpolateRGB(Color c0, Color c1, float t) {
		final float[] cc0 = new float[4];
		final float[] cc1 = new float[4];
		c0.getComponents(cc0);
		c1.getComponents(cc1);
		Color ct = new Color(cc0[0] + (cc1[0] - cc0[0]) * t, cc0[1] + (cc1[1] - cc0[1]) * t, cc0[2] + (cc1[2] - cc0[2]) * t);
		return ct;
	}

	public static Color interpolateHSB(Color c0, Color c1, float t) {
		final float[] cc0 = new float[3];
		final float[] cc1 = new float[3];
		Color.RGBtoHSB(c0.getRed(), c0.getGreen(), c0.getBlue(), cc0);
		Color.RGBtoHSB(c1.getRed(), c1.getGreen(), c1.getBlue(), cc1);
		Color ct = Color.getHSBColor(cc0[0] + (cc1[0] - cc0[0]) * t, cc0[1] + (cc1[1] - cc0[1]) * t, cc0[2] + (cc1[2] - cc0[2]) * t);
		return ct;
	}
}
