/******************************************************************************
 * Copyright 1986-2002 by mental images GmbH & Co.KG, Fasanenstr. 81, D-10623
 * Berlin, Germany. All rights reserved.
 ******************************************************************************
 * Created:	26.10.97
 * Module:	baseshader
 * Purpose:	base shaders for Phenomenon writers
 *
 * Exports:
 *	mib_color_alpha
 *	mib_color_alpha_version
 *	mib_color_intensity
 *	mib_color_intensity_version
 *	mib_color_average
 *	mib_color_average_version
 *
 * History:
 *	13.01.98: added factor parameters
 *
 * Description:
 *	Color conversion routines
 *****************************************************************************/

#include <math.h>
#include <shader.h>


struct mc {
	miColor		input;
	miScalar	factor;
};


DLLEXPORT int mib_color_alpha_version(void) {return(2);}

DLLEXPORT miBoolean mib_color_alpha(
	miColor		*result,
	miState		*state,
	struct mc	*paras)
{
	register miScalar factor = *mi_eval_scalar(&paras->factor);
	if (factor != 0.0)
		*result = *mi_eval_color(&paras->input);
	result->r = result->g = result->b = result->a *= factor;
	return(miTRUE);
}



DLLEXPORT int mib_color_average_version(void) {return(2);}

DLLEXPORT miBoolean mib_color_average(
	miColor		*result,
	miState		*state,
	struct mc	*paras)
{
	register miScalar factor = *mi_eval_scalar(&paras->factor);
	if (factor != 0.0)
		*result = *mi_eval_color(&paras->input);

	result->r = result->g = result->b = result->a = factor *
				(result->r + result->g + result->b) / 3.0;
	return(miTRUE);
}


DLLEXPORT int mib_color_intensity_version(void) {return(2);}

DLLEXPORT miBoolean mib_color_intensity(
	miColor		*result,
	miState		*state,
	struct mc	*paras)
{
	register miScalar factor = *mi_eval_scalar(&paras->factor);
	if (factor != 0.0)
		*result = *mi_eval_color(&paras->input);

	result->r = result->g = result->b = result->a = factor *
							(0.299 * result->r +
							 0.587 * result->g +
							 0.114 * result->b);
	return(miTRUE);
}
