CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

HSV.CPP (11737B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 /* $Header: /CounterStrike/HSV.CPP 1     3/03/97 10:24a Joe_bostic $ */
     17 /***********************************************************************************************
     18  ***              C O N F I D E N T I A L  ---  W E S T W O O D  S T U D I O S               ***
     19  ***********************************************************************************************
     20  *                                                                                             *
     21  *                 Project Name : Command & Conquer                                            *
     22  *                                                                                             *
     23  *                    File Name : HSV.CPP                                                      *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 12/02/95                                                     *
     28  *                                                                                             *
     29  *                  Last Update : February 20, 1996 [JLB]                                      *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   HSVClass::Adjust -- Adjust an HSV color toward specified color.                           *
     34  *   HSVClass::Difference -- Finds the difference between two HSV color objects.               *
     35  *   HSVClass::Set -- Set the palette for this color object.                                   *
     36  *   HSVClass::operator RGBClass -- Conversion operator for RGBClass object.                   *
     37  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     38 
     39 #include	"watcom.h"
     40 #include	"hsv.h"
     41 #include	"rgb.h"
     42 
     43 HSVClass const HSVClass::BlackColor(0, 0, 0);
     44 
     45 
     46 /***********************************************************************************************
     47  * HSVClass::Adjust -- Adjust an HSV color toward specified color.                             *
     48  *                                                                                             *
     49  *    This routine will adjust the HSV color object toward the color of the specified HSV      *
     50  *    object. Typical users of this would be palette morphing or fading routines.              *
     51  *                                                                                             *
     52  * INPUT:   ratio    -- The ratio to move the HSV object toward the color specified. A value   *
     53  *                      of zero means no movement at all. A value of 255 means move completely *
     54  *                      toward the specified color (changed completely).                       *
     55  *                                                                                             *
     56  *          hsv      -- A reference to the color that the current HSV object is to change      *
     57  *                      toward.                                                                *
     58  *                                                                                             *
     59  * OUTPUT:  none                                                                               *
     60  *                                                                                             *
     61  * WARNINGS:   none                                                                            *
     62  *                                                                                             *
     63  * HISTORY:                                                                                    *
     64  *   02/20/1996 JLB : Created.                                                                 *
     65  *=============================================================================================*/
     66 void HSVClass::Adjust(int ratio, HSVClass const & hsv)
     67 {
     68 	/*
     69 	**	Ratio conversion is limited to 0 through 100%. This is
     70 	**	the range of 0 to 255.
     71 	*/
     72 	ratio &= 0x00FF;
     73 
     74 	/*
     75 	**	Adjust the color guns by the ratio specified toward the
     76 	**	destination color.
     77 	*/
     78 	int value = hsv.Value_Component() - Value_Component();
     79 	Value = Value_Component() + (value * ratio) / 256;
     80 
     81 	int saturation = hsv.Saturation_Component() - Saturation_Component();
     82 	Saturation = Saturation_Component() + (saturation * ratio) / 256;
     83 
     84 	int hue = hsv.Hue_Component() - Hue_Component();
     85 	Hue = Hue_Component() + (hue * ratio) / 256;
     86 }
     87 
     88 
     89 /***********************************************************************************************
     90  * HSVClass::Difference -- Finds the difference between two HSV color objects.                 *
     91  *                                                                                             *
     92  *    This routine will determine a color difference between two HSV objects. The difference   *
     93  *    has no particular meaning other that larger numbers meaning greater difference.          *
     94  *                                                                                             *
     95  * INPUT:   hsv   -- The other HSV object to compare this HSV object to.                       *
     96  *                                                                                             *
     97  * OUTPUT:  Returns with a relative distance (in arbitrary units) between this HSV object and  *
     98  *          the HSV object supplied.                                                           *
     99  *                                                                                             *
    100  * WARNINGS:   none                                                                            *
    101  *                                                                                             *
    102  * HISTORY:                                                                                    *
    103  *   02/20/1996 JLB : Created.                                                                 *
    104  *=============================================================================================*/
    105 int HSVClass::Difference(HSVClass const & hsv) const
    106 {
    107 	int hue = (int)Hue - (int)hsv.Hue;
    108 	if (hue < 0) hue = -hue;
    109 
    110 	int saturation = (int)Saturation - (int)hsv.Saturation;
    111 	if (saturation < 0) saturation = -saturation;
    112 
    113 	int value = (int)Value - (int)hsv.Value;
    114 	if (value < 0) value = -value;
    115 
    116 	return(hue*hue + saturation*saturation + value*value);
    117 }
    118 
    119 
    120 /***********************************************************************************************
    121  * HSVClass::operator RGBClass -- Conversion operator for RGBClass object.                     *
    122  *                                                                                             *
    123  *    This conversion operator will convert the HSV object into an RGB object.                 *
    124  *                                                                                             *
    125  * INPUT:   none                                                                               *
    126  *                                                                                             *
    127  * OUTPUT:  Returns with a reference (implied) of the RGBClass object that most closely        *
    128  *          matches this HSVClass object.                                                      *
    129  *                                                                                             *
    130  * WARNINGS:   none                                                                            *
    131  *                                                                                             *
    132  * HISTORY:                                                                                    *
    133  *   02/20/1996 JLB : Created.                                                                 *
    134  *=============================================================================================*/
    135 HSVClass::operator RGBClass (void) const
    136 {
    137 	unsigned int i;				// Integer part.
    138 	unsigned int f;				// Fractional or remainder part.  f/HSV_BASE gives fraction.
    139 	unsigned int tmp;			// Temporary variable to help with calculations.
    140 	unsigned int values[7];	// Possible rgb values.  Don't use zero.
    141 
    142 	int hue = Hue_Component();
    143 	int saturation = Saturation_Component();
    144 	int value = Value_Component();
    145 	int red, green, blue;
    146 
    147 
    148 	hue *= 6;
    149 	f = hue % 255;
    150 
    151 	// Set up possible red, green and blue values.
    152 	values[1] =
    153 	values[2] = value;
    154 
    155 	//
    156 	// The following lines of code change
    157 	//	values[3] = (v * (255 - ( (s * f) / 255) )) / 255;
    158 	//	values[4] = values[5] = (v * (255 - s)) / 255;
    159 	// values[6] = (v * (255 - (s * (255 - f)) / 255)) / 255;
    160 	// so that the are rounded divides.
    161 	//
    162 
    163 	tmp = (saturation * f) / 255;
    164 	values[3] = (value * (255 - tmp)) / 255;
    165 
    166 	values[4] =
    167 	values[5] = (value * (255 - saturation)) / 255;
    168 
    169 	tmp = 255 - (saturation * (255 - f)) / 255;
    170 	values[6] = (value * tmp) / 255;
    171 
    172 
    173 	// This should not be rounded.
    174 	i = hue / 255;
    175 
    176 	i += (i > 4) ? -4 : 2;
    177 	red = values[i];
    178 
    179 	i += (i > 4) ? -4 : 2;
    180 	blue = values[i];
    181 
    182 	i += (i > 4) ? -4 : 2;
    183 	green = values[i];
    184 
    185 	return(RGBClass(red, green, blue));
    186 }
    187 
    188 
    189 /***********************************************************************************************
    190  * HSVClass::Set -- Set the palette for this color object.                                     *
    191  *                                                                                             *
    192  *    The palette will be set for this color object. Use this routine to set an arbitrary      *
    193  *    color index with the HSVClass object.                                                    *
    194  *                                                                                             *
    195  * INPUT:   color -- The color index to change.                                                *
    196  *                                                                                             *
    197  * OUTPUT:  none                                                                               *
    198  *                                                                                             *
    199  * WARNINGS:   none                                                                            *
    200  *                                                                                             *
    201  * HISTORY:                                                                                    *
    202  *   02/20/1996 JLB : Created.                                                                 *
    203  *=============================================================================================*/
    204 void HSVClass::Set(int color) const
    205 {
    206 	RGBClass rgb = *this;
    207 	rgb.Set(color);
    208 }