RGB.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/RGB.CPP 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : RGB.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 12/02/95 *
  30. * *
  31. * Last Update : February 20, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * RGBClass::Adjust -- Adjust one RGB value toward another. *
  36. * RGBClass::Difference -- Determines the "distance" between two colors. *
  37. * RGBClass::Set -- Set the color index to the RGB object specified. *
  38. * RGBClass::operator HSVClass -- Conversion operator for RGB to HSV object. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "watcom.h"
  41. #include "rgb.h"
  42. #include "hsv.h"
  43. #include "palette.h"
  44. RGBClass const BlackColor(0, 0, 0);
  45. /***********************************************************************************************
  46. * RGBClass::Adjust -- Adjust one RGB value toward another. *
  47. * *
  48. * This routine is used to modify an RGB value to proportionately match another RGB value *
  49. * according to the ratio parameter specified. Typical use of this routine is in palette *
  50. * fading from one palette to another or to black. *
  51. * *
  52. * INPUT: ratio -- The ration of transformation. This value is in the form of 0 to 255, *
  53. * with 0 being no change, and 255 being 100% transformed into the *
  54. * destination color. *
  55. * *
  56. * rgb -- Reference to the destination RGB color to transform this color into. *
  57. * *
  58. * OUTPUT: none *
  59. * *
  60. * WARNINGS: none *
  61. * *
  62. * HISTORY: *
  63. * 12/02/1995 JLB : Created. *
  64. *=============================================================================================*/
  65. void RGBClass::Adjust(int ratio, RGBClass const & rgb)
  66. {
  67. /*
  68. ** Ratio conversion is limited to 0 through 100%. This is
  69. ** the range of 0 to 255.
  70. */
  71. ratio &= 0x00FF;
  72. /*
  73. ** Adjust the color guns by the ratio specified toward the
  74. ** destination color.
  75. */
  76. int value = (int)rgb.Red - (int)Red;
  77. Red = (int)Red + (value * ratio) / 256;
  78. value = (int)rgb.Green - (int)Green;
  79. Green = (int)Green + (value * ratio) / 256;
  80. value = (int)rgb.Blue - (int)Blue;
  81. Blue = (int)Blue + (value * ratio) / 256;
  82. }
  83. /***********************************************************************************************
  84. * RGBClass::Difference -- Determines the "distance" between two colors. *
  85. * *
  86. * This routine is used to calculate a relative distance between two colors. The value is *
  87. * relative only to itself and thus is useful only for determining the magnitude of *
  88. * color difference rather than the nature of the color difference. Palette remapping *
  89. * code uses this routine to find closest matches for colors. *
  90. * *
  91. * INPUT: rgb -- Reference to the color to be compared to this color. *
  92. * *
  93. * OUTPUT: Returns the difference between the two colors. The value returned is zero if the *
  94. * colors exactly match. The greater the positive value the greater the difference *
  95. * between the colors. *
  96. * *
  97. * WARNINGS: none *
  98. * *
  99. * HISTORY: *
  100. * 12/02/1995 JLB : Created. *
  101. *=============================================================================================*/
  102. int RGBClass::Difference(RGBClass const & rgb) const
  103. {
  104. int r = (int)Red - (int)rgb.Red;
  105. if (r < 0) r = -r;
  106. int g = (int)Green - (int)rgb.Green;
  107. if (g < 0) g = -g;
  108. int b = (int)Blue - (int)rgb.Blue;
  109. if (b < 0) b = -b;
  110. return(r*r + g*g + b*b);
  111. }
  112. /***********************************************************************************************
  113. * RGBClass::operator HSVClass -- Conversion operator for RGB to HSV object. *
  114. * *
  115. * This conversion operator will convert an RGBClass object into an HSVClass object. *
  116. * *
  117. * INPUT: none *
  118. * *
  119. * OUTPUT: Returns with a reference (implicit) to the HSVClass object that most closely *
  120. * represents the RGBClass object. *
  121. * *
  122. * WARNINGS: none *
  123. * *
  124. * HISTORY: *
  125. * 02/20/1996 JLB : Created. *
  126. *=============================================================================================*/
  127. RGBClass::operator HSVClass (void) const
  128. {
  129. int hue;
  130. int saturation;
  131. int value;
  132. /*
  133. ** Fetch working component values for the color guns.
  134. */
  135. int red = Red_Component();
  136. int green = Green_Component();
  137. int blue = Blue_Component();
  138. /*
  139. ** The hue defaults to none. Only if there is a saturation value will the
  140. ** hue be calculated.
  141. */
  142. hue = 0;
  143. /*
  144. ** Set the value (brightness) to match the brightest color gun.
  145. */
  146. value = (red > green) ? red : green;
  147. if (blue > value) value = blue;
  148. /*
  149. ** Determine the amount of true white present in the color. This is the
  150. ** minimum color gun value. The white component is used to determine
  151. ** color saturation.
  152. */
  153. int white = (red < green) ? red : green;
  154. if (blue < white) white = blue;
  155. /*
  156. ** Determine the saturation (intensity) of the color by comparing the
  157. ** ratio of true white as a component of the overall color. The more
  158. ** white component, the less saturation.
  159. */
  160. saturation = 0;
  161. if (value) {
  162. saturation = ((value - white) * 255) / value;
  163. }
  164. /*
  165. ** If there is any saturation at all, then the hue must be calculated. The
  166. ** hue is based on a six sided color wheel.
  167. */
  168. if (saturation != 0) {
  169. unsigned int tmp = value - white;
  170. unsigned int r1 = ((value - red) * 255) / tmp;
  171. unsigned int g1 = ((value - green) * 255) / tmp;
  172. unsigned int b1 = ((value - blue) * 255) / tmp;
  173. // Find effect of second most predominant color.
  174. // In which section of the hexagon of colors does the color lie?
  175. if (value == red) {
  176. if (white == green) {
  177. tmp = 5 * 256 + b1;
  178. } else {
  179. tmp = 1 * 256 - g1;
  180. }
  181. } else {
  182. if (value == green) {
  183. if (white == blue) {
  184. tmp = 1 * 256 + r1;
  185. } else {
  186. tmp = 3 * 256 - b1;
  187. }
  188. } else {
  189. if (white == red) {
  190. tmp = 3 * 256 + g1;
  191. } else {
  192. tmp = 5 * 256 - r1;
  193. }
  194. }
  195. }
  196. // Divide by six and round.
  197. hue = tmp / 6;
  198. }
  199. return(HSVClass(hue, saturation, value));
  200. }
  201. /***********************************************************************************************
  202. * RGBClass::Set -- Set the color index to the RGB object specified. *
  203. * *
  204. * This routine will set the specified color index with this RGBClass object. Use this *
  205. * routine to set one color. *
  206. * *
  207. * INPUT: color -- The color index to set with this RGBClass object. *
  208. * *
  209. * OUTPUT: none *
  210. * *
  211. * WARNINGS: none *
  212. * *
  213. * HISTORY: *
  214. * 02/20/1996 JLB : Created. *
  215. *=============================================================================================*/
  216. void RGBClass::Set(int color) const
  217. {
  218. Raw_Color_Prep(color);
  219. Raw_Set();
  220. ((RGBClass &)PaletteClass::CurrentPalette[color]) = *this;
  221. }