llcolor3.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @file llcolor3.cpp
  3. * @brief LLColor3 class implementation.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llcolor3.h"
  34. #include "llcolor4.h"
  35. #include "llvector4.h"
  36. #if LL_WINDOWS
  37. # pragma warning(disable : 4996) // strncpy the sux0r
  38. #endif
  39. LLColor3 LLColor3::white(1.f, 1.f, 1.f);
  40. LLColor3 LLColor3::black(0.f, 0.f, 0.f);
  41. LLColor3 LLColor3::grey(0.5f, 0.5f, 0.5f);
  42. LLColor3::LLColor3(const LLColor4& a) noexcept
  43. {
  44. mV[0] = a.mV[0];
  45. mV[1] = a.mV[1];
  46. mV[2] = a.mV[2];
  47. }
  48. LLColor3::LLColor3(const LLVector4& a) noexcept
  49. {
  50. mV[0] = a.mV[0];
  51. mV[1] = a.mV[1];
  52. mV[2] = a.mV[2];
  53. }
  54. const LLColor3& LLColor3::operator=(const LLColor4& a) noexcept
  55. {
  56. mV[0] = a.mV[0];
  57. mV[1] = a.mV[1];
  58. mV[2] = a.mV[2];
  59. return *this;
  60. }
  61. LLColor3::LLColor3(const char* color_string) noexcept
  62. {
  63. if (strlen(color_string) < 6)
  64. {
  65. mV[0] = mV[1] = mV[2] = 0.f;
  66. return;
  67. }
  68. constexpr F32 ONE255TH = 1.f / 255.f;
  69. char tempstr[7];
  70. strncpy(tempstr, color_string, 6);
  71. tempstr[6] = '\0';
  72. mV[VZ] = (F32)strtol(&tempstr[4], NULL, 16) * ONE255TH;
  73. tempstr[4] = '\0';
  74. mV[VY] = (F32)strtol(&tempstr[2], NULL, 16) * ONE255TH;
  75. tempstr[2] = '\0';
  76. mV[VX] = (F32)strtol(&tempstr[0], NULL, 16) * ONE255TH;
  77. }
  78. std::ostream& operator<<(std::ostream& s, const LLColor3& a)
  79. {
  80. s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << " }";
  81. return s;
  82. }
  83. static F32 hueToRgb(F32 val1, F32 val2, F32 hue)
  84. {
  85. if (hue < 0.f)
  86. {
  87. hue += 1.f;
  88. }
  89. else if (hue > 1.f)
  90. {
  91. hue -= 1.f;
  92. }
  93. if (6.f * hue < 1.f)
  94. {
  95. return val1 + (val2 - val1) * 6.f * hue;
  96. }
  97. if (2.f * hue < 1.f)
  98. {
  99. return val2;
  100. }
  101. if (3.f * hue < 2.f)
  102. {
  103. constexpr F32 TWO3RD = 2.f / 3.f;
  104. return val1 + (val2 - val1) * (TWO3RD - hue) * 6.f;
  105. }
  106. return val1;
  107. }
  108. void LLColor3::setHSL(F32 hue, F32 sat, F32 lum)
  109. {
  110. if (sat < 0.00001f)
  111. {
  112. mV[VRED] = mV[VGREEN] = mV[VBLUE] = lum;
  113. }
  114. else
  115. {
  116. F32 interval2 = lum < 0.5f ? lum * (1.f + sat) : lum + sat - sat * lum;
  117. F32 interval1 = 2.f * lum - interval2;
  118. constexpr F32 ONE3RD = 1.f / 3.f;
  119. mV[VRED] = hueToRgb(interval1, interval2, hue + ONE3RD);
  120. mV[VGREEN] = hueToRgb(interval1, interval2, hue);
  121. mV[VBLUE] = hueToRgb(interval1, interval2, hue - ONE3RD);
  122. }
  123. }
  124. void LLColor3::calcHSL(F32* hue, F32* saturation, F32* luminance) const
  125. {
  126. const F32& r = mV[VRED];
  127. const F32& g = mV[VGREEN];
  128. const F32& b = mV[VBLUE];
  129. F32 var_min = (r < (g < b ? g : b) ? r : (g < b ? g : b));
  130. F32 var_max = (r > (g > b ? g : b) ? r : (g > b ? g : b));
  131. F32 l = (var_max + var_min) * 0.5f;
  132. if (luminance)
  133. {
  134. *luminance = l;
  135. }
  136. F32 delta = var_max - var_min;
  137. if (saturation)
  138. {
  139. F32 s = 0.f;
  140. if (delta != 0.f)
  141. {
  142. if (l < 0.5f)
  143. {
  144. s = delta / (var_max + var_min);
  145. }
  146. else
  147. {
  148. s = delta / (2.f - var_max - var_min);
  149. }
  150. }
  151. *saturation = s;
  152. }
  153. if (hue)
  154. {
  155. F32 h = 0.f;
  156. if (delta != 0.f)
  157. {
  158. constexpr F32 ONE6TH = 1.f / 6.f;
  159. F32 half_delta = delta * 0.5f;
  160. F32 del_r = ((var_max - r) * ONE6TH + half_delta) / delta;
  161. F32 del_g = ((var_max - g) * ONE6TH + half_delta) / delta;
  162. F32 del_b = ((var_max - b) * ONE6TH + half_delta) / delta;
  163. if (r >= var_max)
  164. {
  165. h = del_b - del_g;
  166. }
  167. else if (g >= var_max)
  168. {
  169. constexpr F32 ONE3RD = 1.f / 3.f;
  170. h = ONE3RD + del_r - del_b;
  171. }
  172. else if (b >= var_max)
  173. {
  174. constexpr F32 TWO3RD = 2.f / 3.f;
  175. h = TWO3RD + del_g - del_r;
  176. }
  177. if (h < 0.f)
  178. {
  179. h += 1.f;
  180. }
  181. else if (h > 1.f)
  182. {
  183. h -= 1.f;
  184. }
  185. }
  186. *hue = h;
  187. }
  188. }
  189. void LLColor3::adjust(F32 factor)
  190. {
  191. if (factor < 0.f)
  192. {
  193. return;
  194. }
  195. F32 max = llmax(mV[0], mV[1], mV[2]);
  196. if (max > 0.f && max * factor > 1.f)
  197. {
  198. factor = 1.f / max;
  199. }
  200. mV[0] *= factor;
  201. mV[1] *= factor;
  202. mV[2] *= factor;
  203. }