palette.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. ** Command & Conquer Generals(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/PALETTE.CPP $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 7/22/97 11:37a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * PaletteClass::Adjust -- Adjusts the palette toward another palette. *
  35. * PaletteClass::Adjust -- Adjusts this palette toward black. *
  36. * PaletteClass::Closest_Color -- Finds closest match to color specified. *
  37. * PaletteClass::PaletteClass -- Constructor that fills palette with color specified. *
  38. * PaletteClass::operator = -- Assignment operator for palette objects. *
  39. * PaletteClass::operator == -- Equality operator for palette objects. *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #include "always.h"
  42. #include "palette.h"
  43. #include <string.h>
  44. /***********************************************************************************************
  45. * PaletteClass::PaletteClass -- Constructor that fills palette with color specified. *
  46. * *
  47. * This constructor will fill the palette with the color specified. *
  48. * *
  49. * INPUT: rgb -- Reference to the color to fill the entire palette with. *
  50. * *
  51. * OUTPUT: none *
  52. * *
  53. * WARNINGS: none *
  54. * *
  55. * HISTORY: *
  56. * 12/02/1995 JLB : Created. *
  57. *=============================================================================================*/
  58. PaletteClass::PaletteClass(RGBClass const & rgb)
  59. {
  60. for (int index = 0; index < COLOR_COUNT; index++) {
  61. Palette[index] = rgb;
  62. }
  63. }
  64. PaletteClass::PaletteClass(unsigned char *binary_palette)
  65. {
  66. memcpy(&Palette[0], binary_palette, sizeof(Palette));
  67. }
  68. /***********************************************************************************************
  69. * PaletteClass::operator == -- Equality operator for palette objects. *
  70. * *
  71. * This is the comparison for equality operator. It will compare palette objects to *
  72. * determine if they are identical. *
  73. * *
  74. * INPUT: palette -- Reference to the palette to compare to this palette. *
  75. * *
  76. * OUTPUT: Are the two palettes identical? *
  77. * *
  78. * WARNINGS: none *
  79. * *
  80. * HISTORY: *
  81. * 12/02/1995 JLB : Created. *
  82. *=============================================================================================*/
  83. int PaletteClass::operator == (PaletteClass const & palette) const
  84. {
  85. if (this == &palette) return(true);
  86. return(memcmp(&Palette[0], &palette.Palette[0], sizeof(Palette)) == 0);
  87. }
  88. /***********************************************************************************************
  89. * PaletteClass::operator = -- Assignment operator for palette objects. *
  90. * *
  91. * This is the assignment operator for palette objects. Although the default C++ generated *
  92. * assignment operator would function correctly, it would not check for self-assignment *
  93. * and thus this routine can be faster. *
  94. * *
  95. * INPUT: palette -- Reference to that palette that will be copied into this palette. *
  96. * *
  97. * OUTPUT: Returns with a reference to the newly copied to palette. *
  98. * *
  99. * WARNINGS: none *
  100. * *
  101. * HISTORY: *
  102. * 12/02/1995 JLB : Created. *
  103. *=============================================================================================*/
  104. PaletteClass & PaletteClass::operator = (PaletteClass const & palette)
  105. {
  106. if (this == &palette) return(*this);
  107. memcpy(&Palette[0], &palette.Palette[0], sizeof(Palette));
  108. return(*this);
  109. }
  110. /***********************************************************************************************
  111. * PaletteClass::Adjust -- Adjusts this palette toward black. *
  112. * *
  113. * This routine is used to adjust this palette toward black. Typical use of this routine *
  114. * is when fading the palette to black. *
  115. * *
  116. * INPUT: ratio -- The ratio to fade this palette to black. 0 means no fading at all. 255 *
  117. * means 100% faded to black. *
  118. * *
  119. * OUTPUT: none *
  120. * *
  121. * WARNINGS: This routine doesn't actually set the palette to the video card. Use the Set() *
  122. * function to achieve that purpose. *
  123. * *
  124. * HISTORY: *
  125. * 12/02/1995 JLB : Created. *
  126. *=============================================================================================*/
  127. void PaletteClass::Adjust(int ratio)
  128. {
  129. for (int index = 0; index < COLOR_COUNT; index++) {
  130. Palette[index].Adjust(ratio, BlackColor);
  131. }
  132. }
  133. /***********************************************************************************************
  134. * PaletteClass::Adjust -- Adjusts the palette toward another palette. *
  135. * *
  136. * This routine is used to adjust a palette toward a destination palette by the ratio *
  137. * specified. This is primarily used by the palette fading routines. *
  138. * *
  139. * INPUT: palette -- Reference to the destination palette. *
  140. * *
  141. * ratio -- The ratio to adjust this palette toward the destination palette. A *
  142. * value of 0 means no adjustment at all. A value of 255 means 100% *
  143. * adjustment. *
  144. * *
  145. * OUTPUT: none *
  146. * *
  147. * WARNINGS: none *
  148. * *
  149. * HISTORY: *
  150. * 12/02/1995 JLB : Created. *
  151. *=============================================================================================*/
  152. void PaletteClass::Adjust(int ratio, PaletteClass const & palette)
  153. {
  154. for (int index = 0; index < COLOR_COUNT; index++) {
  155. Palette[index].Adjust(ratio, palette[index]);
  156. }
  157. }
  158. /***********************************************************************************************
  159. * PaletteClass::Partial_Adjust -- Adjusts the specified parts of this palette toward black. *
  160. * *
  161. * This routine is used to adjust this palette toward black. Typical use of this routine *
  162. * is when fading the palette to black. The input lookup table is used to determine *
  163. * which entries should fade and which should stay the same *
  164. * *
  165. * INPUT: ratio -- The ratio to fade this palette to black. 0 means no fading at all. 255 *
  166. * means 100% faded to black. *
  167. * *
  168. * lookup -- ptr to lookup table *
  169. * *
  170. * OUTPUT: none *
  171. * *
  172. * WARNINGS: This routine doesn't actually set the palette to the video card. Use the Set() *
  173. * function to achieve that purpose. *
  174. * *
  175. * HISTORY: *
  176. * 12/02/1995 JLB : Created. *
  177. *=============================================================================================*/
  178. void PaletteClass::Partial_Adjust(int ratio, char *lut)
  179. {
  180. for (int index = 0; index < COLOR_COUNT; index++) {
  181. if (lut[index]) {
  182. Palette[index].Adjust(ratio, BlackColor);
  183. }
  184. }
  185. }
  186. /***********************************************************************************************
  187. * PaletteClass::Partial_Adjust -- Adjusts the palette toward another palette. *
  188. * *
  189. * This routine is used to adjust a palette toward a destination palette by the ratio *
  190. * specified. This is primarily used by the palette fading routines. The input lookup *
  191. * table is used to determine which entries should fade and which should stay the same *
  192. * *
  193. * *
  194. * INPUT: palette -- Reference to the destination palette. *
  195. * *
  196. * ratio -- The ratio to adjust this palette toward the destination palette. A *
  197. * value of 0 means no adjustment at all. A value of 255 means 100% *
  198. * adjustment. *
  199. * *
  200. * lookup -- ptr to lookup table *
  201. * *
  202. * *
  203. * OUTPUT: none *
  204. * *
  205. * WARNINGS: none *
  206. * *
  207. * HISTORY: *
  208. * 12/02/1995 JLB : Created. *
  209. *=============================================================================================*/
  210. void PaletteClass::Partial_Adjust(int ratio, PaletteClass const & palette, char *lut)
  211. {
  212. for (int index = 0; index < COLOR_COUNT; index++) {
  213. if (lut[index]) {
  214. Palette[index].Adjust(ratio, palette[index]);
  215. }
  216. }
  217. }
  218. /***********************************************************************************************
  219. * PaletteClass::Closest_Color -- Finds closest match to color specified. *
  220. * *
  221. * This routine will examine the palette and return with the color index number for the *
  222. * color that most closely matches the color specified. Remap operations rely heavily on *
  223. * this routine to allow working with a constant palette. *
  224. * *
  225. * INPUT: rgb -- Reference to a color to search for in the current palette. *
  226. * *
  227. * OUTPUT: Returns with a color index value to most closely matches the specified color. *
  228. * *
  229. * WARNINGS: This routine will quite likely not find an exact match. *
  230. * *
  231. * HISTORY: *
  232. * 12/02/1995 JLB : Created. *
  233. *=============================================================================================*/
  234. int PaletteClass::Closest_Color(RGBClass const & rgb) const
  235. {
  236. int closest = 0;
  237. int value = -1;
  238. RGBClass const * ptr = &Palette[0];
  239. for (int index = 0; index < COLOR_COUNT; index++) {
  240. int difference = rgb.Difference(*ptr++);
  241. if (value == -1 || difference < value) {
  242. value = difference;
  243. closest = index;
  244. }
  245. }
  246. return(closest);
  247. }