/*
 *                          DiskSpeed v2.0
 *                                by
 *                           Michael Sinz
 *
 *             Copyright (c) 1989 by MKSoft Development
 *
 *
 * 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...
 *
 ******************************************************************************
 *									      *
 *	Reading legal mush can turn your bain 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	"RenderInfo.h"

/*
 * Calculate a rough brightness hamming distance...
 * This is not very exact at the moment...
 */
SHORT ColourLevel(UWORD rgb)
{
register	SHORT	level;
register	SHORT	tmp;

	tmp=(rgb & 15);
	level=tmp*tmp;
	tmp=((rgb>>4) & 15);
	level+=tmp*tmp;
	tmp=((rgb>>8) & 15);
	level+=tmp*tmp;
	return(level);
}

VOID FillIn_RenderInfo(struct RenderInfo *ri)
{
register	SHORT		numcolours;
register	SHORT		loop;
register	SHORT		loop1;
register	SHORT		tmp;
register	UWORD		*p;
		SHORT		colours[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
	{
		if (screen.ViewPort.ColorMap->Type==0)
		{	/* Check if I know this colour map... */
			p=(UWORD *)(screen.ViewPort.ColorMap->ColorTable);
			for (loop=0;loop<numcolours;loop++)
			{
				colours[loop]=ColourLevel(p[loop]);
				pens[loop]=loop;
			}

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

			/* Now, pick the pens... */
			loop=0;
			while (!(ri->Shadow=pens[loop++]));

/* Remove the next line if colour0 as background is ok... */
			if (!pens[loop]) loop++;

			ri->BackPen=pens[loop1=loop];

			loop=numcolours-1;
			while (!(ri->Highlight=pens[loop--]));

			loop=numcolours-1;
			if ((colours[loop]-colours[loop1]) < (colours[loop1]-colours[0])) loop=0;
			ri->TextPen=pens[loop];
		}
		else
		{	/* So, you have a strange colour map... */
			/* I have no idea, so here is a guess!  */
			ri->BackPen=3;
			ri->Highlight=1;
			ri->Shadow=2;
			ri->TextPen=1;
		}
	}
}
