hsv.cpp 9.7 KB

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