CnC_Remastered_Collection

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

RGB.CPP (12611B)


      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/RGB.CPP 1     3/03/97 10:25a 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 : RGB.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  *   RGBClass::Adjust -- Adjust one RGB value toward another.                                  *
     34  *   RGBClass::Difference -- Determines the "distance" between two colors.                     *
     35  *   RGBClass::Set -- Set the color index to the RGB object specified.                         *
     36  *   RGBClass::operator HSVClass -- Conversion operator for RGB to HSV object.                 *
     37  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     38 
     39 #include	"watcom.h"
     40 #include "rgb.h"
     41 #include	"hsv.h"
     42 #include	"palettec.h"
     43 
     44 RGBClass const BlackColor(0, 0, 0);
     45 
     46 
     47 /***********************************************************************************************
     48  * RGBClass::Adjust -- Adjust one RGB value toward another.                                    *
     49  *                                                                                             *
     50  *    This routine is used to modify an RGB value to proportionately match another RGB value   *
     51  *    according to the ratio parameter specified. Typical use of this routine is in palette    *
     52  *    fading from one palette to another or to black.                                          *
     53  *                                                                                             *
     54  * INPUT:   ratio    -- The ration of transformation. This value is in the form of 0 to 255,   *
     55  *                      with 0 being no change, and 255 being 100% transformed into the        *
     56  *                      destination color.                                                     *
     57  *                                                                                             *
     58  *          rgb      -- Reference to the destination RGB color to transform this color into.   *
     59  *                                                                                             *
     60  * OUTPUT:  none                                                                               *
     61  *                                                                                             *
     62  * WARNINGS:   none                                                                            *
     63  *                                                                                             *
     64  * HISTORY:                                                                                    *
     65  *   12/02/1995 JLB : Created.                                                                 *
     66  *=============================================================================================*/
     67 void RGBClass::Adjust(int ratio, RGBClass const & rgb)
     68 {
     69 	/*
     70 	**	Ratio conversion is limited to 0 through 100%. This is
     71 	**	the range of 0 to 255.
     72 	*/
     73 	ratio &= 0x00FF;
     74 
     75 	/*
     76 	**	Adjust the color guns by the ratio specified toward the
     77 	**	destination color.
     78 	*/
     79 	int value = (int)rgb.Red - (int)Red;
     80 	Red = (int)Red + (value * ratio) / 256;
     81 
     82 	value = (int)rgb.Green - (int)Green;
     83 	Green = (int)Green + (value * ratio) / 256;
     84 
     85 	value = (int)rgb.Blue - (int)Blue;
     86 	Blue = (int)Blue + (value * ratio) / 256;
     87 }
     88 
     89 
     90 /***********************************************************************************************
     91  * RGBClass::Difference -- Determines the "distance" between two colors.                       *
     92  *                                                                                             *
     93  *    This routine is used to calculate a relative distance between two colors. The value is   *
     94  *    relative only to itself and thus is useful only for determining the magnitude of         *
     95  *    color difference rather than the nature of the color difference. Palette remapping       *
     96  *    code uses this routine to find closest matches for colors.                               *
     97  *                                                                                             *
     98  * INPUT:   rgb   -- Reference to the color to be compared to this color.                      *
     99  *                                                                                             *
    100  * OUTPUT:  Returns the difference between the two colors. The value returned is zero if the   *
    101  *          colors exactly match. The greater the positive value the greater the difference    *
    102  *          between the colors.                                                                *
    103  *                                                                                             *
    104  * WARNINGS:   none                                                                            *
    105  *                                                                                             *
    106  * HISTORY:                                                                                    *
    107  *   12/02/1995 JLB : Created.                                                                 *
    108  *=============================================================================================*/
    109 int RGBClass::Difference(RGBClass const & rgb) const
    110 {
    111 	int r = (int)Red - (int)rgb.Red;
    112 	if (r < 0) r = -r;
    113 
    114 	int g = (int)Green - (int)rgb.Green;
    115 	if (g < 0) g = -g;
    116 
    117 	int b = (int)Blue - (int)rgb.Blue;
    118 	if (b < 0) b = -b;
    119 
    120 	return(r*r + g*g + b*b);
    121 }
    122 
    123 
    124 /***********************************************************************************************
    125  * RGBClass::operator HSVClass -- Conversion operator for RGB to HSV object.                   *
    126  *                                                                                             *
    127  *    This conversion operator will convert an RGBClass object into an HSVClass object.        *
    128  *                                                                                             *
    129  * INPUT:   none                                                                               *
    130  *                                                                                             *
    131  * OUTPUT:  Returns with a reference (implicit) to the HSVClass object that most closely       *
    132  *          represents the RGBClass object.                                                    *
    133  *                                                                                             *
    134  * WARNINGS:   none                                                                            *
    135  *                                                                                             *
    136  * HISTORY:                                                                                    *
    137  *   02/20/1996 JLB : Created.                                                                 *
    138  *=============================================================================================*/
    139 RGBClass::operator HSVClass (void) const
    140 {
    141 	int hue;
    142 	int saturation;
    143 	int value;
    144 
    145 	/*
    146 	**	Fetch working component values for the color guns.
    147 	*/
    148 	int red = Red_Component();
    149 	int green = Green_Component();
    150 	int blue = Blue_Component();
    151 
    152 	/*
    153 	**	The hue defaults to none. Only if there is a saturation value will the
    154 	**	hue be calculated.
    155 	*/
    156 	hue = 0;
    157 
    158 	/*
    159 	**	Set the value (brightness) to match the brightest color gun.
    160 	*/
    161 	value = (red > green) ? red : green;
    162 	if (blue > value) value = blue;
    163 
    164 	/*
    165 	**	Determine the amount of true white present in the color. This is the
    166 	**	minimum color gun value. The white component is used to determine
    167 	**	color saturation.
    168 	*/
    169 	int white = (red < green) ? red : green;
    170 	if (blue < white) white = blue;
    171 
    172 	/*
    173 	**	Determine the saturation (intensity) of the color by comparing the
    174 	**	ratio of true white as a component of the overall color. The more
    175 	**	white component, the less saturation.
    176 	*/
    177 	saturation = 0;
    178 	if (value) {
    179 		saturation = ((value - white) * 255) / value;
    180 	}
    181 
    182 	/*
    183 	** If there is any saturation at all, then the hue must be calculated. The
    184 	**	hue is based on a six sided color wheel.
    185 	*/
    186 	if (saturation != 0) {
    187 		unsigned int tmp = value - white;
    188 	 	unsigned int r1 = ((value - red) * 255) / tmp;
    189 	 	unsigned int g1 = ((value - green) * 255) / tmp;
    190 	 	unsigned int b1 = ((value - blue) * 255) / tmp;
    191 
    192 		// Find effect of second most predominant color.
    193 		// In which section of the hexagon of colors does the color lie?
    194 		if (value == red) {
    195 		 	if (white == green) {
    196 				tmp = 5 * 256 + b1;
    197 			} else {
    198 				tmp = 1 * 256 - g1;
    199 			}
    200 		} else {
    201 			if (value == green) {
    202 			 	if (white == blue) {
    203 					tmp = 1 * 256 + r1;
    204 				} else {
    205 					tmp = 3 * 256 - b1;
    206 				}
    207 			} else {
    208 			 	if (white == red) {
    209 					tmp = 3 * 256 + g1;
    210 				} else {
    211 					tmp = 5 * 256 - r1;
    212 				}
    213 			}
    214 		}
    215 
    216 		// Divide by six and round.
    217 		hue = tmp / 6;
    218 	}
    219 
    220 	return(HSVClass(hue, saturation, value));
    221 }
    222 
    223 
    224 /***********************************************************************************************
    225  * RGBClass::Set -- Set the color index to the RGB object specified.                           *
    226  *                                                                                             *
    227  *    This routine will set the specified color index with this RGBClass object. Use this      *
    228  *    routine to set one color.                                                                *
    229  *                                                                                             *
    230  * INPUT:   color -- The color index to set with this RGBClass object.                         *
    231  *                                                                                             *
    232  * OUTPUT:  none                                                                               *
    233  *                                                                                             *
    234  * WARNINGS:   none                                                                            *
    235  *                                                                                             *
    236  * HISTORY:                                                                                    *
    237  *   02/20/1996 JLB : Created.                                                                 *
    238  *=============================================================================================*/
    239 void RGBClass::Set(int color) const
    240 {
    241 	Raw_Color_Prep(color);
    242 	Raw_Set();
    243 
    244 	((RGBClass &)PaletteClass::CurrentPalette[color]) = *this;
    245 }
    246