PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v3color.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 461 lines | 352 code | 74 blank | 35 comment | 18 complexity | dd53a09c7f6c91bb931c20620a4348c4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v3color.h
  3. * @brief LLColor3 class header file.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_V3COLOR_H
  27. #define LL_V3COLOR_H
  28. class LLColor4;
  29. class LLVector4;
  30. #include "llerror.h"
  31. #include "llmath.h"
  32. #include "llsd.h"
  33. // LLColor3 = |r g b|
  34. static const U32 LENGTHOFCOLOR3 = 3;
  35. class LLColor3
  36. {
  37. public:
  38. F32 mV[LENGTHOFCOLOR3];
  39. static LLColor3 white;
  40. static LLColor3 black;
  41. static LLColor3 grey;
  42. public:
  43. LLColor3(); // Initializes LLColor3 to (0, 0, 0)
  44. LLColor3(F32 r, F32 g, F32 b); // Initializes LLColor3 to (r, g, b)
  45. LLColor3(const F32 *vec); // Initializes LLColor3 to (vec[0]. vec[1], vec[2])
  46. LLColor3(const char *color_string); // html format color ie "#FFDDEE"
  47. explicit LLColor3(const LLColor4& color4); // "explicit" to avoid automatic conversion
  48. explicit LLColor3(const LLVector4& vector4); // "explicit" to avoid automatic conversion
  49. LLColor3(const LLSD& sd);
  50. LLSD getValue() const
  51. {
  52. LLSD ret;
  53. ret[0] = mV[0];
  54. ret[1] = mV[1];
  55. ret[2] = mV[2];
  56. return ret;
  57. }
  58. void setValue(const LLSD& sd)
  59. {
  60. mV[0] = (F32) sd[0].asReal();;
  61. mV[1] = (F32) sd[1].asReal();;
  62. mV[2] = (F32) sd[2].asReal();;
  63. }
  64. void setHSL(F32 hue, F32 saturation, F32 luminance);
  65. void calcHSL(F32* hue, F32* saturation, F32* luminance) const;
  66. const LLColor3& setToBlack(); // Clears LLColor3 to (0, 0, 0)
  67. const LLColor3& setToWhite(); // Zero LLColor3 to (0, 0, 0)
  68. const LLColor3& setVec(F32 x, F32 y, F32 z); // deprecated
  69. const LLColor3& setVec(const LLColor3 &vec); // deprecated
  70. const LLColor3& setVec(const F32 *vec); // deprecated
  71. const LLColor3& set(F32 x, F32 y, F32 z); // Sets LLColor3 to (x, y, z)
  72. const LLColor3& set(const LLColor3 &vec); // Sets LLColor3 to vec
  73. const LLColor3& set(const F32 *vec); // Sets LLColor3 to vec
  74. F32 magVec() const; // deprecated
  75. F32 magVecSquared() const; // deprecated
  76. F32 normVec(); // deprecated
  77. F32 length() const; // Returns magnitude of LLColor3
  78. F32 lengthSquared() const; // Returns magnitude squared of LLColor3
  79. F32 normalize(); // Normalizes and returns the magnitude of LLColor3
  80. F32 brightness() const; // Returns brightness of LLColor3
  81. const LLColor3& operator=(const LLColor4 &a);
  82. friend std::ostream& operator<<(std::ostream& s, const LLColor3 &a); // Print a
  83. friend LLColor3 operator+(const LLColor3 &a, const LLColor3 &b); // Return vector a + b
  84. friend LLColor3 operator-(const LLColor3 &a, const LLColor3 &b); // Return vector a minus b
  85. friend const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b); // Return vector a + b
  86. friend const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b); // Return vector a minus b
  87. friend const LLColor3& operator*=(LLColor3 &a, const LLColor3 &b);
  88. friend LLColor3 operator*(const LLColor3 &a, const LLColor3 &b); // Return component wise a * b
  89. friend LLColor3 operator*(const LLColor3 &a, F32 k); // Return a times scaler k
  90. friend LLColor3 operator*(F32 k, const LLColor3 &a); // Return a times scaler k
  91. friend bool operator==(const LLColor3 &a, const LLColor3 &b); // Return a == b
  92. friend bool operator!=(const LLColor3 &a, const LLColor3 &b); // Return a != b
  93. friend const LLColor3& operator*=(LLColor3 &a, F32 k); // Return a times scaler k
  94. friend LLColor3 operator-(const LLColor3 &a); // Return vector 1-rgb (inverse)
  95. inline void clamp();
  96. inline void exp(); // Do an exponential on the color
  97. };
  98. LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u);
  99. void LLColor3::clamp()
  100. {
  101. // Clamp the color...
  102. if (mV[0] < 0.f)
  103. {
  104. mV[0] = 0.f;
  105. }
  106. else if (mV[0] > 1.f)
  107. {
  108. mV[0] = 1.f;
  109. }
  110. if (mV[1] < 0.f)
  111. {
  112. mV[1] = 0.f;
  113. }
  114. else if (mV[1] > 1.f)
  115. {
  116. mV[1] = 1.f;
  117. }
  118. if (mV[2] < 0.f)
  119. {
  120. mV[2] = 0.f;
  121. }
  122. else if (mV[2] > 1.f)
  123. {
  124. mV[2] = 1.f;
  125. }
  126. }
  127. // Non-member functions
  128. F32 distVec(const LLColor3 &a, const LLColor3 &b); // Returns distance between a and b
  129. F32 distVec_squared(const LLColor3 &a, const LLColor3 &b);// Returns distance squared between a and b
  130. inline LLColor3::LLColor3(void)
  131. {
  132. mV[0] = 0.f;
  133. mV[1] = 0.f;
  134. mV[2] = 0.f;
  135. }
  136. inline LLColor3::LLColor3(F32 r, F32 g, F32 b)
  137. {
  138. mV[VX] = r;
  139. mV[VY] = g;
  140. mV[VZ] = b;
  141. }
  142. inline LLColor3::LLColor3(const F32 *vec)
  143. {
  144. mV[VX] = vec[VX];
  145. mV[VY] = vec[VY];
  146. mV[VZ] = vec[VZ];
  147. }
  148. #if LL_WINDOWS
  149. # pragma warning( disable : 4996 ) // strncpy teh sux0r
  150. #endif
  151. inline LLColor3::LLColor3(const char* color_string) // takes a string of format "RRGGBB" where RR is hex 00..FF
  152. {
  153. if (strlen(color_string) < 6) /* Flawfinder: ignore */
  154. {
  155. mV[0] = 0.f;
  156. mV[1] = 0.f;
  157. mV[2] = 0.f;
  158. return;
  159. }
  160. char tempstr[7];
  161. strncpy(tempstr,color_string,6); /* Flawfinder: ignore */
  162. tempstr[6] = '\0';
  163. mV[VZ] = (F32)strtol(&tempstr[4],NULL,16)/255.f;
  164. tempstr[4] = '\0';
  165. mV[VY] = (F32)strtol(&tempstr[2],NULL,16)/255.f;
  166. tempstr[2] = '\0';
  167. mV[VX] = (F32)strtol(&tempstr[0],NULL,16)/255.f;
  168. }
  169. inline const LLColor3& LLColor3::setToBlack(void)
  170. {
  171. mV[0] = 0.f;
  172. mV[1] = 0.f;
  173. mV[2] = 0.f;
  174. return (*this);
  175. }
  176. inline const LLColor3& LLColor3::setToWhite(void)
  177. {
  178. mV[0] = 1.f;
  179. mV[1] = 1.f;
  180. mV[2] = 1.f;
  181. return (*this);
  182. }
  183. inline const LLColor3& LLColor3::set(F32 r, F32 g, F32 b)
  184. {
  185. mV[0] = r;
  186. mV[1] = g;
  187. mV[2] = b;
  188. return (*this);
  189. }
  190. inline const LLColor3& LLColor3::set(const LLColor3 &vec)
  191. {
  192. mV[0] = vec.mV[0];
  193. mV[1] = vec.mV[1];
  194. mV[2] = vec.mV[2];
  195. return (*this);
  196. }
  197. inline const LLColor3& LLColor3::set(const F32 *vec)
  198. {
  199. mV[0] = vec[0];
  200. mV[1] = vec[1];
  201. mV[2] = vec[2];
  202. return (*this);
  203. }
  204. // deprecated
  205. inline const LLColor3& LLColor3::setVec(F32 r, F32 g, F32 b)
  206. {
  207. mV[0] = r;
  208. mV[1] = g;
  209. mV[2] = b;
  210. return (*this);
  211. }
  212. // deprecated
  213. inline const LLColor3& LLColor3::setVec(const LLColor3 &vec)
  214. {
  215. mV[0] = vec.mV[0];
  216. mV[1] = vec.mV[1];
  217. mV[2] = vec.mV[2];
  218. return (*this);
  219. }
  220. // deprecated
  221. inline const LLColor3& LLColor3::setVec(const F32 *vec)
  222. {
  223. mV[0] = vec[0];
  224. mV[1] = vec[1];
  225. mV[2] = vec[2];
  226. return (*this);
  227. }
  228. inline F32 LLColor3::brightness(void) const
  229. {
  230. return (mV[0] + mV[1] + mV[2]) / 3.0f;
  231. }
  232. inline F32 LLColor3::length(void) const
  233. {
  234. return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
  235. }
  236. inline F32 LLColor3::lengthSquared(void) const
  237. {
  238. return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2];
  239. }
  240. inline F32 LLColor3::normalize(void)
  241. {
  242. F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
  243. F32 oomag;
  244. if (mag)
  245. {
  246. oomag = 1.f/mag;
  247. mV[0] *= oomag;
  248. mV[1] *= oomag;
  249. mV[2] *= oomag;
  250. }
  251. return (mag);
  252. }
  253. // deprecated
  254. inline F32 LLColor3::magVec(void) const
  255. {
  256. return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
  257. }
  258. // deprecated
  259. inline F32 LLColor3::magVecSquared(void) const
  260. {
  261. return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2];
  262. }
  263. // deprecated
  264. inline F32 LLColor3::normVec(void)
  265. {
  266. F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
  267. F32 oomag;
  268. if (mag)
  269. {
  270. oomag = 1.f/mag;
  271. mV[0] *= oomag;
  272. mV[1] *= oomag;
  273. mV[2] *= oomag;
  274. }
  275. return (mag);
  276. }
  277. inline void LLColor3::exp()
  278. {
  279. #if 0
  280. mV[0] = ::exp(mV[0]);
  281. mV[1] = ::exp(mV[1]);
  282. mV[2] = ::exp(mV[2]);
  283. #else
  284. mV[0] = (F32)LL_FAST_EXP(mV[0]);
  285. mV[1] = (F32)LL_FAST_EXP(mV[1]);
  286. mV[2] = (F32)LL_FAST_EXP(mV[2]);
  287. #endif
  288. }
  289. inline LLColor3 operator+(const LLColor3 &a, const LLColor3 &b)
  290. {
  291. return LLColor3(
  292. a.mV[0] + b.mV[0],
  293. a.mV[1] + b.mV[1],
  294. a.mV[2] + b.mV[2]);
  295. }
  296. inline LLColor3 operator-(const LLColor3 &a, const LLColor3 &b)
  297. {
  298. return LLColor3(
  299. a.mV[0] - b.mV[0],
  300. a.mV[1] - b.mV[1],
  301. a.mV[2] - b.mV[2]);
  302. }
  303. inline LLColor3 operator*(const LLColor3 &a, const LLColor3 &b)
  304. {
  305. return LLColor3(
  306. a.mV[0] * b.mV[0],
  307. a.mV[1] * b.mV[1],
  308. a.mV[2] * b.mV[2]);
  309. }
  310. inline LLColor3 operator*(const LLColor3 &a, F32 k)
  311. {
  312. return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k );
  313. }
  314. inline LLColor3 operator*(F32 k, const LLColor3 &a)
  315. {
  316. return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k );
  317. }
  318. inline bool operator==(const LLColor3 &a, const LLColor3 &b)
  319. {
  320. return ( (a.mV[0] == b.mV[0])
  321. &&(a.mV[1] == b.mV[1])
  322. &&(a.mV[2] == b.mV[2]));
  323. }
  324. inline bool operator!=(const LLColor3 &a, const LLColor3 &b)
  325. {
  326. return ( (a.mV[0] != b.mV[0])
  327. ||(a.mV[1] != b.mV[1])
  328. ||(a.mV[2] != b.mV[2]));
  329. }
  330. inline const LLColor3 &operator*=(LLColor3 &a, const LLColor3 &b)
  331. {
  332. a.mV[0] *= b.mV[0];
  333. a.mV[1] *= b.mV[1];
  334. a.mV[2] *= b.mV[2];
  335. return a;
  336. }
  337. inline const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b)
  338. {
  339. a.mV[0] += b.mV[0];
  340. a.mV[1] += b.mV[1];
  341. a.mV[2] += b.mV[2];
  342. return a;
  343. }
  344. inline const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b)
  345. {
  346. a.mV[0] -= b.mV[0];
  347. a.mV[1] -= b.mV[1];
  348. a.mV[2] -= b.mV[2];
  349. return a;
  350. }
  351. inline const LLColor3& operator*=(LLColor3 &a, F32 k)
  352. {
  353. a.mV[0] *= k;
  354. a.mV[1] *= k;
  355. a.mV[2] *= k;
  356. return a;
  357. }
  358. inline LLColor3 operator-(const LLColor3 &a)
  359. {
  360. return LLColor3(
  361. 1.f - a.mV[0],
  362. 1.f - a.mV[1],
  363. 1.f - a.mV[2] );
  364. }
  365. // Non-member functions
  366. inline F32 distVec(const LLColor3 &a, const LLColor3 &b)
  367. {
  368. F32 x = a.mV[0] - b.mV[0];
  369. F32 y = a.mV[1] - b.mV[1];
  370. F32 z = a.mV[2] - b.mV[2];
  371. return (F32) sqrt( x*x + y*y + z*z );
  372. }
  373. inline F32 distVec_squared(const LLColor3 &a, const LLColor3 &b)
  374. {
  375. F32 x = a.mV[0] - b.mV[0];
  376. F32 y = a.mV[1] - b.mV[1];
  377. F32 z = a.mV[2] - b.mV[2];
  378. return x*x + y*y + z*z;
  379. }
  380. inline LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u)
  381. {
  382. return LLColor3(
  383. a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
  384. a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
  385. a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u);
  386. }
  387. #endif