#ifndef lint
static char *sccsid = "@(#)colour.c	1.1 (Steve Hill) 3/9/90";
#endif

/* colour.c
 *
 * Routines to create and manipulate colours.
 *
 * A colour is a triple of reals representing an RGB value.
 */

#include <stdio.h>

#include "basetype.h"
#include "malloc.h"
#include "error.h"
#include "colour.h"


/* Colour
 *
 * Create and initialise a colour.  Returns pointer to new structure.
 */

colour_t *
Colour(r, g, b)
real_t	r, g, b;
{
	colour_t	*colour;

	colour = (colour_t *) malloc(sizeof(colour_t));

	if (colour == ColourNull)
		FatalError(__FILE__, __LINE__, "Colour: Out of memory");

	colour->r = r;
	colour->g = g;
	colour->b = b;

	return(colour);
}


/* ColourCopy
 *
 * Returns a pointer to a new structure - which is a copy of the
 * argument colour.
 */

colour_t *
ColourCopy(colour)
colour_t	*colour;
{
	if (colour == ColourNull)
		return(ColourNull);

	return(Colour(colour->r, colour->g, colour->b));
}


/* ColourLet
 *
 * Assign one colour to another.
 */

colour_t *
ColourLet(colour1, colour2)
colour_t	*colour1, *colour2;
{
	colour1->r = colour2->r;
	colour1->g = colour2->g;
	colour1->b = colour2->b;

	return(colour1);
}

/* ColourFree
 *
 * Free the memory allocated for a colour.
 */

void
ColourFree(colour)
colour_t	*colour;
{
	if (colour == ColourNull)
		return;

	(void) free((char *) colour);
}


/* ColourPrint
 *
 * Print the specified colour to a file.
 */

void
ColourPrint(file, colour)
FILE		*file;
colour_t	*colour;
{
	if (colour == ColourNull)
	{
		fprintf(file, "Null Colour\n");
		return;
	}
	fprintf(file, "(%f, %f, %f)", colour->r, colour->g, colour->b);
}
