/**ColorOpt.c************************************************************
 *									*
 *   SeePix -- by Hank Schafer						*
 *									*
 *   SeePix is an IFF Picture Viewer.  It works with Lo-Res,		*
 *   Med-Res, Hi-Res, HAM, and EHB formats.  I'm working on support     *
 *   for X-Specs, and Anim5 formats.					*
 *									*
 *   SeePix is based on a program by Olaf Barthel, called		*
 *   'LoadImage'.  As released, LoadImage had a couple of bugs. 	*
 *   The Amiga-Key alternatives to menu use didn't all work.  That's	*
 *   been fixed.  There were two separate print functions in LoadImage	*
 *   which behaved exactly the same.  The redundancy was eliminated.	*
 *   LoadImage used the Topaz ROMFONT, and made no allowances for a	*
 *   different system default font (under 2.04).  I added a built-in	*
 *   font.  I've reworked the code considerably from the original       *
 *   release, to allow for optimizations based on time and space.	*
 *									*
 *   SeePix features a palette tool which allows "tweaking" of colors	*
 *   prior to printing, allowing you to modify the color printout to	*
 *   more closely resemble the original graphics (Blue is Blue, not	*
 *   Purple).  SeePix can be Iconified.  SeePix features an ARP 	*
 *   interface.  SeePix now uses the PathMaster File Selector.		*
 *									*
 ************************************************************************
 *                                                                      *
 *   SeePix Copyright © 1992 by Hank Schafer; all rights reserved.      *
 *                                                                      *
 *   This program is free software; you can redistribute it and/or      *
 *   modify it under the terms of the GNU General Public License as     *
 *   published by the Free Software Foundation, either version 1, or    *
 *   (at your option) any later version.                                *
 *                                                                      *
 *   This program is distributed in the hope that it will be useful,    *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 *   General Public License for more details.                           *
 *                                                                      *
 *   You should have received a copy of the GNU General Public License  *
 *   along with this program; if not, write to:                         *
 *                 Free Software Foundation, Inc.                       *
 *                 675 Massachusetts Ave.                               *
 *                 Cambridge  MA  02139, USA                            *
 *                                                                      *
 **************** Special Function Copyright Notices ********************
 *									*
 ****LoadImage Copyright Notice:					*
 *									*
 *   LoadImage is © Copyright 1988, 1989, 1990 by MXM, all rights	*
 *   reserved, written by Olaf Barthel.  No guarantees of any kind are	*
 *   made that this program is 100% reliable.  Use this program on	*
 *   your own risk!							*
 *									*
 ****Iconify Copyright Notice:						*
 *									*
 *   Copyright 1987 by Leo L. Schwab.					*
 *   Permission is hereby granted for use in any and all programs,	*
 *   both Public Domain and commercial in nature, provided this 	*
 *   Copyright notice is left intact.					*
 *									*
 ****ColorWindow (Palette) Copyright Notice:				*
 *									*
 * ColorWindow Routine	--  Color Window Routines			*
 *     from Book 1 of the Amiga Programmers' Suite by RJ Mical          *
 *									*
 * Copyright (C) 1986, 1987, Robert J. Mical				*
 * All Rights Reserved. 						*
 *									*
 ****PathMaster Copyright Notice:					*
 *									*
 * -------------------------------------------------------------------- *
 *   Copyright © 1989 Justin V. McCormick.  All Rights Reserved.	*
 * -------------------------------------------------------------------- *
 *									*
 *    The PathMaster name is a trademark of Justin V. McCormick.	*
 *									*
 *   PathMaster File Selector source and documentation written by:	*
 *									*
 *			 Justin V. McCormick.				*
 *	       Copyright © 1989 by Justin V. McCormick. 		*
 *			 All Rights Reserved.				*
 *									*
 * -------------------------------------------------------------------- *
 ************************************************************************
 *									*
 *   Hope this is something you can use and enjoy.			*
 *									*
 ************************************************************************/

/*
 * HSLToRGB(); RGBToHSL() :
 *
 * The functions controlling the HSL<->RGB conversions for the Color Palette.
 */

WORD
HSLToRGB(hue, saturation, luminance)
    UWORD	    hue, saturation, luminance;

/*
 * This actually doesn't do true HSL to RGB conversion, but it works close
 * enough.  Also, this code could be optimized quite a bit, but I've left it
 * spread out like this so that several years from now I will be able to
 * figure out what the heck is going on.
 */

{
    LONG	    red, green, blue;
    LONG	    reddiff, greendiff, bluediff;
    LONG	    sixth, rising, falling, invertsaturation;

    /*
     * We're going to hold one color off, a second at full, and ramp  the
     * third.  This allows us to visit all two-color combinations. By
     * modifying saturation, all three-color combinations can be seen.
     */

    sixth = hue / 0x2AAB;
    rising = (hue - (sixth * 0x2AAB)) * 6;
    falling = 0xFFFF - rising;

    switch (sixth) {
    case 0:
	red = 0xFFFF;
	green = rising;
	blue = 0;
	break;
    case 1:
	red = falling;
	green = 0xFFFF;
	blue = 0;
	break;
    case 2:
	red = 0;
	green = 0xFFFF;
	blue = rising;
	break;
    case 3:
	red = 0;
	green = falling;
	blue = 0xFFFF;
	break;
    case 4:
	red = rising;
	green = 0;
	blue = 0xFFFF;
	break;
    case 5:
	red = 0xFFFF;
	green = 0;
	blue = falling;
	break;
    }

    red = (red * luminance) >> 16;
    green = (green * luminance) >> 16;
    blue = (blue * luminance) >> 16;

    /*
     * The closer saturation is to zero, the closer red, green and blue
     * should be to luminance.
     */

    invertsaturation = 0xFFFF - saturation;
    reddiff = luminance - red;
    red = red + ((reddiff * invertsaturation) >> 16);
    greendiff = luminance - green;
    green = green + ((greendiff * invertsaturation) >> 16);
    bluediff = luminance - blue;
    blue = blue + ((bluediff * invertsaturation) >> 16);

    red = (red >> 12) & 0xF;
    green = (green >> 12) & 0xF;
    blue = (blue >> 12) & 0xF;

    return ((WORD) ((red << 8) | (green << 4) | (blue)));
}

VOID
RGBToHSL(rgb, returnhue, returnsat, returnlum)
    UWORD	    rgb;
    UWORD	   *returnhue, *returnsat, *returnlum;
{
    LONG	    min, max, hue, saturation, luminance, differential;
    LONG	    redpart, greenpart, bluepart;
    LONG	    workred, workgreen, workblue;

    workred = ((rgb >> 8) & 0xF) * 0x111;
    workgreen = ((rgb >> 4) & 0xF) * 0x111;
    workblue = (rgb & 0xF) * 0x111;

    if (workred < workgreen)
	min = workred;
    else
	min = workgreen;
    if (workblue < min)
	min = workblue;

    if (workred > workgreen)
	max = workred;
    else
	max = workgreen;
    if (workblue > max)
	max = workblue;

    luminance = max;
    luminance <<= 4;
    differential = max - min;

    if (max != 0) {
	saturation = (differential << 16) / max;
	if (saturation > 0xFFFF)
	    saturation = 0xFFFF;
    } else
	saturation = 0;

    if (saturation == 0)
	hue = 0;
    else {
	redpart = (((max - workred) << 16) / differential) >> 4;
	greenpart = (((max - workgreen) << 16) / differential) >> 4;
	bluepart = (((max - workblue) << 16) / differential) >> 4;

	if (workred == max)
	    hue = bluepart - greenpart;
	else if (workgreen == max)
	    hue = 0x2000 + redpart - bluepart;
	else if (workblue == max)
	    hue = 0x4000 + greenpart - redpart;
	if (hue < 0)
	    hue += 0x6000;
	hue = (hue * 2667) / 1000;
    }

    *returnhue = hue;
    *returnsat = saturation;
    *returnlum = luminance;
}
