/*
 *                          DiskSpeed v3.0
 *                                by
 *                           Michael Sinz
 *
 *             Copyright (c) 1989 by MKSoft Development
 *
 *			MKSoft Development
 *			163 Appledore Drive
 *			Downingtown, PA 19335
 *
 * Yes, this is yet another disk speed testing program, but with a few
 * differences.  It was designed to give the most accurate results of the
 * true disk performance in the system.  For this reason many of
 * DiskSpeed's results may look either lower or higher than current disk
 * performance tests.
 *
 * This program was thrown together in a few hours because I needed more
 * accurate and consistent results for disk performance as seen from the
 * application's standpoint.  This program has now served its purpose and
 * I am now giving it to the rest of the Amiga world to play with as long
 * as all of the files remain together in unmodified form.  (That is, the
 * files DiskSpeed, DiskSpeed.info, DiskSpeed.c, DiskSpeedWindow.c,
 * DiskSpeedWindow.h, MakeBoxes.c, MakeBoxes.h, StandardGadgets.c,
 * StandardGadgets.h, RenderInfo.c, RenderInfo.h, DiskSpeed.doc, and
 * MakeFile)
 *
 * Version 2.0 of this program added a few features and cleaned up the
 * user interface.  I hope you like this...
 *
 * Version 3.0 of this program added the performance stress and cleaned up
 * some parts of the older code.  (Fix to RenderInfo.c)
 *
 ******************************************************************************
 *									      *
 *	Reading legal mush can turn your brain into guacamole!		      *
 *									      *
 *		So here is some of that legal mush:			      *
 *									      *
 * Permission is hereby granted to distribute this program's source	      *
 * executable, and documentation for non-commercial purposes, so long as the  *
 * copyright notices are not removed from the sources, executable or	      *
 * documentation.  This program may not be distributed for a profit without   *
 * the express written consent of the author Michael Sinz.		      *
 *									      *
 * This program is not in the public domain.				      *
 *									      *
 * Fred Fish is expressly granted permission to distribute this program's     *
 * source and executable as part of the "Fred Fish freely redistributable     *
 * Amiga software library."						      *
 *									      *
 * Permission is expressly granted for this program and it's source to be     *
 * distributed as part of the Amicus Amiga software disks, and the	      *
 * First Amiga User Group's Hot Mix disks.				      *
 *									      *
 ******************************************************************************
 *
 * This file contains the definition of the rendering information
 * for elements on the screen.  This information is used to generate
 * the correct pen colours for items on the screen...
 */

#include	<exec/types.h>
#include	<graphics/view.h>
#include	<intuition/intuition.h>

#include	<proto/intuition.h>
#include	<proto/graphics.h>

#include	"RenderInfo.h"


/*
 * This returns the colour difference hamming value...
 */
SHORT ColourDifference(UWORD rgb0, UWORD rgb1)
{
register	SHORT	level;
register	SHORT	tmp;

	tmp=(rgb0 & 15) - (rgb1 & 15);
	level=tmp*tmp;
	tmp=((rgb0>>4) & 15) - ((rgb1>>4) & 15);
	level+=tmp*tmp;
	tmp=((rgb0>>8) & 15) - ((rgb1>>8) & 15);
	level+=tmp*tmp;
	return(level);
}

/*
 * Calculate a rough brightness hamming value...
 */
SHORT ColourLevel(UWORD rgb)
{
	return(ColourDifference(rgb,0));
}

#define	MAX_COLOURS	16

VOID FillIn_RenderInfo(struct RenderInfo *ri)
{
register	SHORT		numcolours;
register	SHORT		loop;
register	SHORT		loop1;
register	SHORT		backpen;
register	SHORT		tmp;
		SHORT		colours[16];
		SHORT		colourlevels[16];
		SHORT		pens[16];
	struct	Screen		screen;

	GetScreenData((UBYTE *)&screen,sizeof(struct Screen),WBENCHSCREEN,NULL);
	numcolours=1 << (screen.RastPort.BitMap->Depth);
	if (numcolours>16) numcolours=16;

	if (numcolours<3)
	{	/* Some silly person is running with 2 colours... */
		ri->BackPen=1;
		ri->Highlight=0;
		ri->Shadow=1;
		ri->TextPen=0;
	}
	else
	{
		for (loop=0;loop<numcolours;loop++)
		{
			colours[loop]=GetRGB4(screen.ViewPort.ColorMap,(LONG)loop);
			colourlevels[loop]=ColourLevel(colours[loop]);
			pens[loop]=loop;
		}

		/* Sort darkest to brightest... */
		for (loop=0;loop<(numcolours-1);loop++)
		 for (loop1=loop+1;loop1<numcolours;loop1++)
		 {
			if (colourlevels[loop]>colourlevels[loop1])
			{
				tmp=colourlevels[loop];
				colourlevels[loop]=colourlevels[loop1];
				colourlevels[loop1]=tmp;

				tmp=colours[loop];
				colours[loop]=colours[loop1];
				colours[loop1]=tmp;

				tmp=pens[loop];
				pens[loop]=pens[loop1];
				pens[loop1]=tmp;
			}
		 }

		/* Now, pick the pens... HightLight... */
		loop=numcolours-1;
		while (!(ri->Highlight=pens[loop--]));

		/* and Shadow... */
		loop=0;
		while (!(ri->Shadow=pens[loop++]));

		/* The BackGround pen... */
		if (!pens[loop]) loop++;
		ri->BackPen=pens[backpen=loop];

		loop1=0;
		for (loop=0;loop<numcolours;loop++)
		{
			tmp=ColourDifference(colours[loop],colours[backpen]);
			if (tmp>loop1)
			{
				loop1=tmp;
				ri->TextPen=pens[loop];
			}
		}
	}
}
