HSV.CPP 11 KB

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