HSV.CPP 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/HSV.CPP 1 3/03/97 10:24a 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 : HSV.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. * HSVClass::Adjust -- Adjust an HSV color toward specified color. *
  36. * HSVClass::Difference -- Finds the difference between two HSV color objects. *
  37. * HSVClass::Set -- Set the palette for this color object. *
  38. * HSVClass::operator RGBClass -- Conversion operator for RGBClass object. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "watcom.h"
  41. #include "hsv.h"
  42. #include "rgb.h"
  43. HSVClass const HSVClass::BlackColor(0, 0, 0);
  44. /***********************************************************************************************
  45. * HSVClass::Adjust -- Adjust an HSV color toward specified color. *
  46. * *
  47. * This routine will adjust the HSV color object toward the color of the specified HSV *
  48. * object. Typical users of this would be palette morphing or fading routines. *
  49. * *
  50. * INPUT: ratio -- The ratio to move the HSV object toward the color specified. A value *
  51. * of zero means no movement at all. A value of 255 means move completely *
  52. * toward the specified color (changed completely). *
  53. * *
  54. * hsv -- A reference to the color that the current HSV object is to change *
  55. * toward. *
  56. * *
  57. * OUTPUT: none *
  58. * *
  59. * WARNINGS: none *
  60. * *
  61. * HISTORY: *
  62. * 02/20/1996 JLB : Created. *
  63. *=============================================================================================*/
  64. void HSVClass::Adjust(int ratio, HSVClass const & hsv)
  65. {
  66. /*
  67. ** Ratio conversion is limited to 0 through 100%. This is
  68. ** the range of 0 to 255.
  69. */
  70. ratio &= 0x00FF;
  71. /*
  72. ** Adjust the color guns by the ratio specified toward the
  73. ** destination color.
  74. */
  75. int value = hsv.Value_Component() - Value_Component();
  76. Value = Value_Component() + (value * ratio) / 256;
  77. int saturation = hsv.Saturation_Component() - Saturation_Component();
  78. Saturation = Saturation_Component() + (saturation * ratio) / 256;
  79. int hue = hsv.Hue_Component() - Hue_Component();
  80. Hue = Hue_Component() + (hue * ratio) / 256;
  81. }
  82. /***********************************************************************************************
  83. * HSVClass::Difference -- Finds the difference between two HSV color objects. *
  84. * *
  85. * This routine will determine a color difference between two HSV objects. The difference *
  86. * has no particular meaning other that larger numbers meaning greater difference. *
  87. * *
  88. * INPUT: hsv -- The other HSV object to compare this HSV object to. *
  89. * *
  90. * OUTPUT: Returns with a relative distance (in arbitrary units) between this HSV object and *
  91. * the HSV object supplied. *
  92. * *
  93. * WARNINGS: none *
  94. * *
  95. * HISTORY: *
  96. * 02/20/1996 JLB : Created. *
  97. *=============================================================================================*/
  98. int HSVClass::Difference(HSVClass const & hsv) const
  99. {
  100. int hue = (int)Hue - (int)hsv.Hue;
  101. if (hue < 0) hue = -hue;
  102. int saturation = (int)Saturation - (int)hsv.Saturation;
  103. if (saturation < 0) saturation = -saturation;
  104. int value = (int)Value - (int)hsv.Value;
  105. if (value < 0) value = -value;
  106. return(hue*hue + saturation*saturation + value*value);
  107. }
  108. /***********************************************************************************************
  109. * HSVClass::operator RGBClass -- Conversion operator for RGBClass object. *
  110. * *
  111. * This conversion operator will convert the HSV object into an RGB object. *
  112. * *
  113. * INPUT: none *
  114. * *
  115. * OUTPUT: Returns with a reference (implied) of the RGBClass object that most closely *
  116. * matches this HSVClass object. *
  117. * *
  118. * WARNINGS: none *
  119. * *
  120. * HISTORY: *
  121. * 02/20/1996 JLB : Created. *
  122. *=============================================================================================*/
  123. HSVClass::operator RGBClass (void) const
  124. {
  125. unsigned int i; // Integer part.
  126. unsigned int f; // Fractional or remainder part. f/HSV_BASE gives fraction.
  127. unsigned int tmp; // Temporary variable to help with calculations.
  128. unsigned int values[7]; // Possible rgb values. Don't use zero.
  129. int hue = Hue_Component();
  130. int saturation = Saturation_Component();
  131. int value = Value_Component();
  132. int red, green, blue;
  133. hue *= 6;
  134. f = hue % 255;
  135. // Set up possible red, green and blue values.
  136. values[1] =
  137. values[2] = value;
  138. //
  139. // The following lines of code change
  140. // values[3] = (v * (255 - ( (s * f) / 255) )) / 255;
  141. // values[4] = values[5] = (v * (255 - s)) / 255;
  142. // values[6] = (v * (255 - (s * (255 - f)) / 255)) / 255;
  143. // so that the are rounded divides.
  144. //
  145. tmp = (saturation * f) / 255;
  146. values[3] = (value * (255 - tmp)) / 255;
  147. values[4] =
  148. values[5] = (value * (255 - saturation)) / 255;
  149. tmp = 255 - (saturation * (255 - f)) / 255;
  150. values[6] = (value * tmp) / 255;
  151. // This should not be rounded.
  152. i = hue / 255;
  153. i += (i > 4) ? -4 : 2;
  154. red = values[i];
  155. i += (i > 4) ? -4 : 2;
  156. blue = values[i];
  157. i += (i > 4) ? -4 : 2;
  158. green = values[i];
  159. return(RGBClass(red, green, blue));
  160. }
  161. /***********************************************************************************************
  162. * HSVClass::Set -- Set the palette for this color object. *
  163. * *
  164. * The palette will be set for this color object. Use this routine to set an arbitrary *
  165. * color index with the HSVClass object. *
  166. * *
  167. * INPUT: color -- The color index to change. *
  168. * *
  169. * OUTPUT: none *
  170. * *
  171. * WARNINGS: none *
  172. * *
  173. * HISTORY: *
  174. * 02/20/1996 JLB : Created. *
  175. *=============================================================================================*/
  176. void HSVClass::Set(int color) const
  177. {
  178. RGBClass rgb = *this;
  179. rgb.Set(color);
  180. }