#include "AnimationInterpolate.h"
#include "AnimationObject.h"

#define NUM_INTERPOLATE(a, b, perc) (a * (1.0 - (perc)) + b * (perc))

color
colorInterpolate(color ina, color inb, double percentage) {
	ARGB a, b;
	ARGB_set(&a, ina);
	ARGB_set(&b, inb);

	a.a = NUM_INTERPOLATE(a.a, b.a, percentage);
	a.r = NUM_INTERPOLATE(a.r, b.r, percentage);
	a.g = NUM_INTERPOLATE(a.g, b.g, percentage);
	a.b = NUM_INTERPOLATE(a.b, b.b, percentage);

	return a.a << 24 | a.r << 16 | a.g << 8 | a.b;
}

ARGB
A_Interpolate(const Animation* anim, u32 frameIndex, ARGB pixel, i32 r, i32 c, void* priv) {
	struct A_Interpolate in = *(struct A_Interpolate*)priv;

	double perc = (double)(frameIndex - in.startFrame) / (in.endFrame - in.startFrame);
	if (perc < 0.0) perc = 0.0;
	if (perc > 1.0) perc = 1.0;

	in.interpolate(in.value, in.initialState, in.finalState, perc);

	return in.callback(anim, frameIndex, pixel, r, c, in.priv);
}

void
AO_LineInterpolate(void* valueLine, const void* initialLine, const void* finalLine, double percentage) {
	struct AO_Line* value = valueLine;
	const struct AO_Line* initial = initialLine, *final = finalLine;
	value->a = NUM_INTERPOLATE(initial->a, final->a, percentage);
	value->b = NUM_INTERPOLATE(initial->b, final->b, percentage);
	value->c = NUM_INTERPOLATE(initial->c, final->c, percentage);
	value->width = NUM_INTERPOLATE(initial->width, final->width, percentage);
	value->color = colorInterpolate(initial->color, final->color, percentage);
}
