package transformer;

import timedistortion.PixelData;
import timedistortion.Point;
import timedistortion.ColorF32;
import timedistortion.ITransform;

public class TriChrome extends AbstractTransformer implements ITransform {
	private static final float delta_hue = -70f / 180f;
	private static final int frame_distance = 30;
	private static final float color_div = 1f / 256f;
	private static final float color_mult = 255f;

//	@Override
//	public void t(Point p) {
//		ColorF32 rgb = new ColorF32();
//		ColorF32 hsb = new ColorF32();
//
//		int in2d_index = 3 * (p.dy * ictx.width + p.dx);
//		
//		PixelData pd0 = ictx.at(p.ti);
//		rgb.r = color_div * PixelData.unsigned(pd0.at(in2d_index));;
//		rgb.g = color_div * PixelData.unsigned(pd0.at(in2d_index + 1));;
//		rgb.b = color_div * PixelData.unsigned(pd0.at(in2d_index + 2));;
//		ColorF32.rgb2hsb(rgb, hsb);
//		hsb.r += delta_hue;
//		ColorF32.hsb2rgb(hsb, rgb);
//		p.r = color_mult * rgb.r;
//		
//		PixelData pd1 = ictx.at(p.ti + frame_distance);
//		rgb.r = color_div * PixelData.unsigned(pd1.at(in2d_index));;
//		rgb.g = color_div * PixelData.unsigned(pd1.at(in2d_index + 1));;
//		rgb.b = color_div * PixelData.unsigned(pd1.at(in2d_index + 2));;
//		ColorF32.rgb2hsb(rgb, hsb);
//		hsb.r += delta_hue;
//		ColorF32.hsb2rgb(hsb, rgb);
//		p.g = color_mult * rgb.g;
//		
//		PixelData pd2 = ictx.at(p.ti + 2 * frame_distance);
//		rgb.r = color_div * PixelData.unsigned(pd2.at(in2d_index));;
//		rgb.g = color_div * PixelData.unsigned(pd2.at(in2d_index + 1));;
//		rgb.b = color_div * PixelData.unsigned(pd2.at(in2d_index + 2));;
//		ColorF32.rgb2hsb(rgb, hsb);
//		hsb.r += delta_hue;
//		ColorF32.hsb2rgb(hsb, rgb);
//		p.b = color_mult * rgb.b;
//	}

	@Override
	public void t(Point p) {
		ColorF32 rgb = new ColorF32();
		ColorF32 hsb = new ColorF32();
		ColorF32 tmp = new ColorF32();

		int in2d_index = 3 * (p.dy * ictx.width + p.dx);
		
		PixelData pd0 = ictx.at(p.ti);
		rgb.r = color_div * PixelData.unsigned(pd0.at(in2d_index));;
		rgb.g = color_div * PixelData.unsigned(pd0.at(in2d_index + 1));;
		rgb.b = color_div * PixelData.unsigned(pd0.at(in2d_index + 2));;
		ColorF32.rgb2hsb(rgb, hsb);
		hsb.r += delta_hue;
		ColorF32.hsb2rgb(hsb, rgb);
		tmp.r = rgb.r;
		
		PixelData pd1 = ictx.at(p.ti + frame_distance);
		rgb.r = color_div * PixelData.unsigned(pd1.at(in2d_index));;
		rgb.g = color_div * PixelData.unsigned(pd1.at(in2d_index + 1));;
		rgb.b = color_div * PixelData.unsigned(pd1.at(in2d_index + 2));;
		ColorF32.rgb2hsb(rgb, hsb);
		hsb.r += delta_hue;
		ColorF32.hsb2rgb(hsb, rgb);
		tmp.g = rgb.g;
		
		PixelData pd2 = ictx.at(p.ti + 2 * frame_distance);
		rgb.r = color_div * PixelData.unsigned(pd2.at(in2d_index));;
		rgb.g = color_div * PixelData.unsigned(pd2.at(in2d_index + 1));;
		rgb.b = color_div * PixelData.unsigned(pd2.at(in2d_index + 2));;
		ColorF32.rgb2hsb(rgb, hsb);
		hsb.r += delta_hue;
		ColorF32.hsb2rgb(hsb, rgb);
		tmp.b = rgb.b;
		
		ColorF32.rgb2hsb(tmp, hsb);
		hsb.r -= delta_hue;
		ColorF32.hsb2rgb(hsb, tmp);
		p.r = color_mult * tmp.r;
		p.g = color_mult * tmp.g;
		p.b = color_mult * tmp.b;		
	}

}
