package transformer;

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

public class TriChromeSum extends AbstractTransformer implements ITransform {
//	private static final float delta_hue = -70f / 180f;
	private static final float delta_hue = 60f / 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();
		float r_sum = 0;
		float g_sum = 0;
		float b_sum = 0;

		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);
		r_sum += rgb.r;
		g_sum += rgb.g;
		b_sum += rgb.b;
		
		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 += 2 * delta_hue;
		ColorF32.hsb2rgb(hsb, rgb);
		r_sum += rgb.r;
		g_sum += rgb.g;
		b_sum += rgb.b;
		
		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 += 3 * delta_hue;
		ColorF32.hsb2rgb(hsb, rgb);
		r_sum += rgb.r;
		g_sum += rgb.g;
		b_sum += rgb.b;

		p.r = color_mult * r_sum / 3f;
		p.g = color_mult * g_sum / 3f;
		p.b = color_mult * b_sum / 3f;
	}

}
