RGB.CPP 12 KB

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