/* Note:  this colour comparison routine was snitched from HamSharp.

   This has the side effect of allowing one to compare colour errors gotten
   with GIFMachine to those gotten with (S)HamSharp.
*/

#include "GIFMachine.h"

static UBYTE Diff[16][16];
static UBYTE Intensity[16][16][16];

extern struct Library *MathIeeeDoubBasBase;

void InitDiff(void)
{
	register int i;
	register int j;
	register int r;
	register int g;
	register int b;

	for (i = 0; i < 16; i++)
		for (j = 0; j < 16; j++)
			Diff[i][j] = (i - j) * (i - j);

	for (r = 0; r < 16; r++)
		for (g = 0; g < 16; g++)
			for (b = 0; b < 16; b++)
				Intensity[r][g][b] = (int)(.299 * r + .587 * g + .114 * b);
}

ULONG RGBdiff(UBYTE r1, UBYTE g1, UBYTE b1, UBYTE r2, UBYTE g2, UBYTE b2)
{
	return (ULONG)(Diff[Intensity[r1][g1][b1]][Intensity[r2][g2][b2]] +
		Diff[r1][r2] + Diff[g1][g2] + Diff[b1][b2]);
}
