colorspace.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 : Colorspace *
  23. * *
  24. * $Archive:: /VSS_Sync/ww3d2/colorspace.h $*
  25. * *
  26. * Original Author:: Hector Yee *
  27. * *
  28. * $Author:: Vss_sync $*
  29. * *
  30. * $Modtime:: 8/29/01 9:50p $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef COLORSPACE_H
  41. #define COLORSPACE_H
  42. #include <wwmath.h>
  43. void RGB_To_HSV(Vector3 &hsv,const Vector3 &rgb);
  44. void HSV_To_RGB(Vector3 &rgb, const Vector3 &hsv);
  45. void Recolor(Vector3 &rgb, const Vector3 &hsv_shift);
  46. //---------------------------------------------------------------------
  47. // Color Conversions
  48. //---------------------------------------------------------------------
  49. inline void RGB_To_HSV(Vector3 &hsv,const Vector3 &rgb)
  50. // modified from Foley et al. page 592
  51. // converts rgb[0..1] to h [0,360), s and v in [0,1]
  52. // negative h values are to signify undefined
  53. {
  54. float max=WWMath::Max(rgb.X,rgb.Y);
  55. max=WWMath::Max(max,rgb.Z);
  56. float min=WWMath::Min(rgb.X,rgb.Y);
  57. min=WWMath::Min(min,rgb.Z);
  58. // value
  59. hsv.Z=max;
  60. // saturation
  61. hsv.Y=(max!=0.0f)?((max-min)/max):0.0f;
  62. if (hsv.Y==0.0f) hsv.X=-1.0f;
  63. else
  64. {
  65. float delta=max-min;
  66. if (rgb.X==max)
  67. hsv.X=(rgb.Y-rgb.Z)/delta;
  68. else if (rgb.Y==max)
  69. hsv.X=2.0f+ (rgb.Z-rgb.X)/delta;
  70. else if (rgb.Z==max)
  71. hsv.X=4.0f+ (rgb.X-rgb.Y)/delta;
  72. hsv.X*=60.0f;
  73. if (hsv.X<0.0f) hsv.X+=360.0f;
  74. }
  75. }
  76. inline void HSV_To_RGB(Vector3 &rgb, const Vector3 &hsv)
  77. {
  78. float h=hsv.X;
  79. float s=hsv.Y;
  80. float v=hsv.Z;
  81. if (hsv.Y==0.0f) {
  82. rgb.Set(v,v,v);
  83. } else {
  84. float f,p,q,t;
  85. int i;
  86. if (h==360.0f) h=0.0f;
  87. h/=60.0f;
  88. i=WWMath::Floor(h);
  89. f=h-i;
  90. p=v*(1.0f-s);
  91. q=v*(1.0f-(s*f));
  92. t=v*(1.0f-(s*(1.0f-f)));
  93. switch (i) {
  94. case 0:
  95. rgb.Set(v,t,p);
  96. break;
  97. case 1:
  98. rgb.Set(q,v,p);
  99. break;
  100. case 2:
  101. rgb.Set(p,v,t);
  102. break;
  103. case 3:
  104. rgb.Set(p,q,v);
  105. break;
  106. case 4:
  107. rgb.Set(t,p,v);
  108. break;
  109. case 5:
  110. rgb.Set(v,p,q);
  111. break;
  112. }
  113. }
  114. }
  115. inline void Recolor(Vector3 &rgb, const Vector3 &hsv_shift)
  116. {
  117. Vector3 hsv;
  118. RGB_To_HSV(hsv,rgb);
  119. // If the Hue has the "undefined flag" (a negative value), this means that the color is pure
  120. // monochrome. In this case do not shift the hue (it is undefined) or the saturation (it is 0
  121. // so it cannot be decreased, and increasing it would cause the undefined hue to actually
  122. // become visible). In this case, we only modify the value.
  123. if (hsv.X<0.0f) hsv+=Vector3(0.0f,0.0f,hsv_shift.Z);
  124. else hsv+=hsv_shift;
  125. // angular mod
  126. if (hsv.X<0.0f) hsv.X+=360.0f;
  127. if (hsv.X>360.0f) hsv.X-=360.0f;
  128. // clamp saturation and value
  129. hsv.Y=WWMath::Clamp(hsv.Y,0.0f,1.0f);
  130. hsv.Z=WWMath::Clamp(hsv.Z,0.0f,1.0f);
  131. HSV_To_RGB(rgb,hsv);
  132. }
  133. #endif