#include "fdf.h"

t_color	color_init(int red, int green, int blue)
{
	t_color c;

	c.red = red;
	c.blue = blue;
	c.green = green;
	return (c);
}

t_color	color_lerp(t_color c1, t_color c2, float step)
{
	t_color new;
	new.red = (float)(c2.red - c1.red) * step + c1.red;
	new.green = (float)(c2.green - c1.green) * step  + c1.green;
	new.blue = (float)(c2.blue - c1.blue) * step + c1.blue;
	return (new);
}

int 	color_to_int(t_color c)
{
	return ((c.red << 16) | (c.green << 8) | c.blue);
}

t_color int_to_color(int c)
{
	t_color new;

	new.blue = c & 0x0000FF;
	new.green = (c >> 8) & 0x0000FF;
	new.red = (c >> 16 ) & 0x0000FF;
	c = 0;
	return (new);
}

t_color	choose_color(t_line *line, t_swap *s, t_FDF *FDF)
{
	t_color c;
	float 	z1;
	float 	z2;

	FDF = FDF;
	z1 = line->p1.z;
	z2 = line->p2.z;
	if (z2 == 0 && z1 == 0)
		c = int_to_color(COLOR1);
	else if (z1 == z2)
		c = int_to_color(COLOR2);
	else if (z2 == 0)
		c = int_to_color(COLOR1);
	else if (z1 == 0)
		c = int_to_color(COLOR2);
	else
	{
		if (s->swap_z)
			c = color_lerp(int_to_color(COLOR2), int_to_color(COLOR1), z1 / z2);
		else
			c = color_lerp(int_to_color(COLOR1), int_to_color(COLOR2), z1 / z2);
	}
	return (c);
}
