rgb.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. ** Command & Conquer Renegade(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. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Library/RGB.CPP $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 7/22/97 11:37a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * RGBClass::Adjust -- Adjust one RGB value toward another. *
  35. * RGBClass::Difference -- Determines the "distance" between two colors. *
  36. * RGBClass::operator HSVClass -- Conversion operator for RGB to HSV object. *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "always.h"
  39. #include "hsv.h"
  40. #include "palette.h"
  41. #include "rgb.h"
  42. RGBClass const BlackColor(0, 0, 0);
  43. /***********************************************************************************************
  44. * RGBClass::Adjust -- Adjust one RGB value toward another. *
  45. * *
  46. * This routine is used to modify an RGB value to proportionately match another RGB value *
  47. * according to the ratio parameter specified. Typical use of this routine is in palette *
  48. * fading from one palette to another or to black. *
  49. * *
  50. * INPUT: ratio -- The ration of transformation. This value is in the form of 0 to 255, *
  51. * with 0 being no change, and 255 being 100% transformed into the *
  52. * destination color. *
  53. * *
  54. * rgb -- Reference to the destination RGB color to transform this color into. *
  55. * *
  56. * OUTPUT: none *
  57. * *
  58. * WARNINGS: none *
  59. * *
  60. * HISTORY: *
  61. * 12/02/1995 JLB : Created. *
  62. *=============================================================================================*/
  63. void RGBClass::Adjust(int ratio, RGBClass const & rgb)
  64. {
  65. /*
  66. ** Ratio conversion is limited to 0 through 100%. This is
  67. ** the range of 0 to 255.
  68. */
  69. ratio &= 0x00FF;
  70. /*
  71. ** Adjust the color guns by the ratio specified toward the
  72. ** destination color.
  73. */
  74. int value = (int)rgb.Red - (int)Red;
  75. Red = (unsigned char)((int)Red + (value * ratio) / 256);
  76. value = (int)rgb.Green - (int)Green;
  77. Green = (unsigned char)((int)Green + (value * ratio) / 256);
  78. value = (int)rgb.Blue - (int)Blue;
  79. Blue = (unsigned char)((int)Blue + (value * ratio) / 256);
  80. }
  81. /***********************************************************************************************
  82. * RGBClass::Difference -- Determines the "distance" between two colors. *
  83. * *
  84. * This routine is used to calculate a relative distance between two colors. The value is *
  85. * relative only to itself and thus is useful only for determining the magnitude of *
  86. * color difference rather than the nature of the color difference. Palette remapping *
  87. * code uses this routine to find closest matches for colors. *
  88. * *
  89. * INPUT: rgb -- Reference to the color to be compared to this color. *
  90. * *
  91. * OUTPUT: Returns the difference between the two colors. The value returned is zero if the *
  92. * colors exactly match. The greater the positive value the greater the difference *
  93. * between the colors. *
  94. * *
  95. * WARNINGS: none *
  96. * *
  97. * HISTORY: *
  98. * 12/02/1995 JLB : Created. *
  99. *=============================================================================================*/
  100. int RGBClass::Difference(RGBClass const & rgb) const
  101. {
  102. int r = (int)Red - (int)rgb.Red;
  103. if (r < 0) r = -r;
  104. int g = (int)Green - (int)rgb.Green;
  105. if (g < 0) g = -g;
  106. int b = (int)Blue - (int)rgb.Blue;
  107. if (b < 0) b = -b;
  108. /*
  109. ** At first crack, the difference algorithm might be coded as the sum of the color differences
  110. ** (or sum of the square of the color distances). However, this would not take advantage of the
  111. ** fact that the human eye is most sensative to green, followed by the color blue. With this
  112. ** thought in mind, the following difference algorithm is used.
  113. */
  114. return(4*g + 3*b + 2*r);
  115. }
  116. /***********************************************************************************************
  117. * RGBClass::operator HSVClass -- Conversion operator for RGB to HSV object. *
  118. * *
  119. * This conversion operator will convert an RGBClass object into an HSVClass object. *
  120. * *
  121. * INPUT: none *
  122. * *
  123. * OUTPUT: Returns with a reference (implicit) to the HSVClass object that most closely *
  124. * represents the RGBClass object. *
  125. * *
  126. * WARNINGS: none *
  127. * *
  128. * HISTORY: *
  129. * 02/20/1996 JLB : Created. *
  130. *=============================================================================================*/
  131. RGBClass::operator HSVClass (void) const
  132. {
  133. int hue;
  134. int saturation;
  135. int value;
  136. /*
  137. ** Fetch working component values for the color guns.
  138. */
  139. int red = Get_Red();
  140. int green = Get_Green();
  141. int blue = Get_Blue();
  142. /*
  143. ** The hue defaults to none. Only if there is a saturation value will the
  144. ** hue be calculated.
  145. */
  146. hue = 0;
  147. /*
  148. ** Set the value (brightness) to match the brightest color gun.
  149. */
  150. value = (red > green) ? red : green;
  151. if (blue > value) value = blue;
  152. /*
  153. ** Determine the amount of true white present in the color. This is the
  154. ** minimum color gun value. The white component is used to determine
  155. ** color saturation.
  156. */
  157. int white = (red < green) ? red : green;
  158. if (blue < white) white = blue;
  159. /*
  160. ** Determine the saturation (intensity) of the color by comparing the
  161. ** ratio of true white as a component of the overall color. The more
  162. ** white component, the less saturation.
  163. */
  164. saturation = 0;
  165. if (value) {
  166. saturation = ((value - white) * 255) / value;
  167. }
  168. /*
  169. ** If there is any saturation at all, then the hue must be calculated. The
  170. ** hue is based on a six sided color wheel.
  171. */
  172. if (saturation != 0) {
  173. unsigned int tmp = value - white;
  174. unsigned int r1 = ((value - red) * 255) / tmp;
  175. unsigned int g1 = ((value - green) * 255) / tmp;
  176. unsigned int b1 = ((value - blue) * 255) / tmp;
  177. // Find effect of second most predominant color.
  178. // In which section of the hexagon of colors does the color lie?
  179. if (value == red) {
  180. if (white == green) {
  181. tmp = 5 * 256 + b1;
  182. } else {
  183. tmp = 1 * 256 - g1;
  184. }
  185. } else {
  186. if (value == green) {
  187. if (white == blue) {
  188. tmp = 1 * 256 + r1;
  189. } else {
  190. tmp = 3 * 256 - b1;
  191. }
  192. } else {
  193. if (white == red) {
  194. tmp = 3 * 256 + g1;
  195. } else {
  196. tmp = 5 * 256 - r1;
  197. }
  198. }
  199. }
  200. // Divide by six and round.
  201. hue = tmp / 6;
  202. }
  203. HSVClass hsv((unsigned char)hue, (unsigned char)saturation, (unsigned char)value);
  204. return(hsv);
  205. }