/indra/llmath/v4color.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 649 lines · 497 code · 99 blank · 53 comment · 29 complexity · b7f84ac5520377b227a2964271189445 MD5 · raw file

  1. /**
  2. * @file v4color.h
  3. * @brief LLColor4 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_V4COLOR_H
  27. #define LL_V4COLOR_H
  28. #include "llerror.h"
  29. //#include "vmath.h"
  30. #include "llmath.h"
  31. #include "llsd.h"
  32. class LLColor3;
  33. class LLColor4U;
  34. class LLVector4;
  35. // LLColor4 = |x y z w|
  36. static const U32 LENGTHOFCOLOR4 = 4;
  37. static const U32 MAX_LENGTH_OF_COLOR_NAME = 15; //Give plenty of room for additional colors...
  38. class LLColor4
  39. {
  40. public:
  41. F32 mV[LENGTHOFCOLOR4];
  42. LLColor4(); // Initializes LLColor4 to (0, 0, 0, 1)
  43. LLColor4(F32 r, F32 g, F32 b); // Initializes LLColor4 to (r, g, b, 1)
  44. LLColor4(F32 r, F32 g, F32 b, F32 a); // Initializes LLColor4 to (r. g, b, a)
  45. LLColor4(U32 clr); // Initializes LLColor4 to (r=clr>>24, etc))
  46. LLColor4(const F32 *vec); // Initializes LLColor4 to (vec[0]. vec[1], vec[2], 1)
  47. LLColor4(const LLColor3 &vec, F32 a = 1.f); // Initializes LLColor4 to (vec, a)
  48. explicit LLColor4(const LLSD& sd);
  49. explicit LLColor4(const LLColor4U& color4u); // "explicit" to avoid automatic conversion
  50. explicit LLColor4(const LLVector4& vector4); // "explicit" to avoid automatic conversion
  51. LLSD getValue() const
  52. {
  53. LLSD ret;
  54. ret[0] = mV[0];
  55. ret[1] = mV[1];
  56. ret[2] = mV[2];
  57. ret[3] = mV[3];
  58. return ret;
  59. }
  60. void setValue(const LLSD& sd);
  61. void setHSL(F32 hue, F32 saturation, F32 luminance);
  62. void calcHSL(F32* hue, F32* saturation, F32* luminance) const;
  63. const LLColor4& setToBlack(); // zero LLColor4 to (0, 0, 0, 1)
  64. const LLColor4& setToWhite(); // zero LLColor4 to (0, 0, 0, 1)
  65. const LLColor4& setVec(F32 r, F32 g, F32 b, F32 a); // deprecated -- use set()
  66. const LLColor4& setVec(F32 r, F32 g, F32 b); // deprecated -- use set()
  67. const LLColor4& setVec(const LLColor4 &vec); // deprecated -- use set()
  68. const LLColor4& setVec(const LLColor3 &vec); // deprecated -- use set()
  69. const LLColor4& setVec(const LLColor3 &vec, F32 a); // deprecated -- use set()
  70. const LLColor4& setVec(const F32 *vec); // deprecated -- use set()
  71. const LLColor4& setVec(const LLColor4U& color4u); // deprecated -- use set()
  72. const LLColor4& set(F32 r, F32 g, F32 b, F32 a); // Sets LLColor4 to (r, g, b, a)
  73. const LLColor4& set(F32 r, F32 g, F32 b); // Sets LLColor4 to (r, g, b) (no change in a)
  74. const LLColor4& set(const LLColor4 &vec); // Sets LLColor4 to vec
  75. const LLColor4& set(const LLColor3 &vec); // Sets LLColor4 to LLColor3 vec (no change in alpha)
  76. const LLColor4& set(const LLColor3 &vec, F32 a); // Sets LLColor4 to LLColor3 vec, with alpha specified
  77. const LLColor4& set(const F32 *vec); // Sets LLColor4 to vec
  78. const LLColor4& set(const LLColor4U& color4u); // Sets LLColor4 to color4u, rescaled.
  79. const LLColor4& setAlpha(F32 a);
  80. F32 magVec() const; // deprecated -- use length()
  81. F32 magVecSquared() const; // deprecated -- use lengthSquared()
  82. F32 normVec(); // deprecated -- use normalize()
  83. F32 length() const; // Returns magnitude of LLColor4
  84. F32 lengthSquared() const; // Returns magnitude squared of LLColor4
  85. F32 normalize(); // deprecated -- use normalize()
  86. BOOL isOpaque() { return mV[VALPHA] == 1.f; }
  87. F32 operator[](int idx) const { return mV[idx]; }
  88. F32 &operator[](int idx) { return mV[idx]; }
  89. const LLColor4& operator=(const LLColor3 &a); // Assigns vec3 to vec4 and returns vec4
  90. bool operator<(const LLColor4& rhs) const;
  91. friend std::ostream& operator<<(std::ostream& s, const LLColor4 &a); // Print a
  92. friend LLColor4 operator+(const LLColor4 &a, const LLColor4 &b); // Return vector a + b
  93. friend LLColor4 operator-(const LLColor4 &a, const LLColor4 &b); // Return vector a minus b
  94. friend LLColor4 operator*(const LLColor4 &a, const LLColor4 &b); // Return component wise a * b
  95. friend LLColor4 operator*(const LLColor4 &a, F32 k); // Return rgb times scaler k (no alpha change)
  96. friend LLColor4 operator*(F32 k, const LLColor4 &a); // Return rgb times scaler k (no alpha change)
  97. friend LLColor4 operator%(const LLColor4 &a, F32 k); // Return alpha times scaler k (no rgb change)
  98. friend LLColor4 operator%(F32 k, const LLColor4 &a); // Return alpha times scaler k (no rgb change)
  99. friend bool operator==(const LLColor4 &a, const LLColor4 &b); // Return a == b
  100. friend bool operator!=(const LLColor4 &a, const LLColor4 &b); // Return a != b
  101. friend bool operator==(const LLColor4 &a, const LLColor3 &b); // Return a == b
  102. friend bool operator!=(const LLColor4 &a, const LLColor3 &b); // Return a != b
  103. friend const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b); // Return vector a + b
  104. friend const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b); // Return vector a minus b
  105. friend const LLColor4& operator*=(LLColor4 &a, F32 k); // Return rgb times scaler k (no alpha change)
  106. friend const LLColor4& operator%=(LLColor4 &a, F32 k); // Return alpha times scaler k (no rgb change)
  107. friend const LLColor4& operator*=(LLColor4 &a, const LLColor4 &b); // Doesn't multiply alpha! (for lighting)
  108. // conversion
  109. operator const LLColor4U() const;
  110. // Basic color values.
  111. static LLColor4 red;
  112. static LLColor4 green;
  113. static LLColor4 blue;
  114. static LLColor4 black;
  115. static LLColor4 white;
  116. static LLColor4 yellow;
  117. static LLColor4 magenta;
  118. static LLColor4 cyan;
  119. static LLColor4 smoke;
  120. static LLColor4 grey;
  121. static LLColor4 orange;
  122. static LLColor4 purple;
  123. static LLColor4 pink;
  124. static LLColor4 transparent;
  125. // Extra color values.
  126. static LLColor4 grey1;
  127. static LLColor4 grey2;
  128. static LLColor4 grey3;
  129. static LLColor4 grey4;
  130. static LLColor4 red1;
  131. static LLColor4 red2;
  132. static LLColor4 red3;
  133. static LLColor4 red4;
  134. static LLColor4 red5;
  135. static LLColor4 green1;
  136. static LLColor4 green2;
  137. static LLColor4 green3;
  138. static LLColor4 green4;
  139. static LLColor4 green5;
  140. static LLColor4 green6;
  141. static LLColor4 blue1;
  142. static LLColor4 blue2;
  143. static LLColor4 blue3;
  144. static LLColor4 blue4;
  145. static LLColor4 blue5;
  146. static LLColor4 blue6;
  147. static LLColor4 yellow1;
  148. static LLColor4 yellow2;
  149. static LLColor4 yellow3;
  150. static LLColor4 yellow4;
  151. static LLColor4 yellow5;
  152. static LLColor4 yellow6;
  153. static LLColor4 yellow7;
  154. static LLColor4 yellow8;
  155. static LLColor4 yellow9;
  156. static LLColor4 orange1;
  157. static LLColor4 orange2;
  158. static LLColor4 orange3;
  159. static LLColor4 orange4;
  160. static LLColor4 orange5;
  161. static LLColor4 orange6;
  162. static LLColor4 magenta1;
  163. static LLColor4 magenta2;
  164. static LLColor4 magenta3;
  165. static LLColor4 magenta4;
  166. static LLColor4 purple1;
  167. static LLColor4 purple2;
  168. static LLColor4 purple3;
  169. static LLColor4 purple4;
  170. static LLColor4 purple5;
  171. static LLColor4 purple6;
  172. static LLColor4 pink1;
  173. static LLColor4 pink2;
  174. static LLColor4 cyan1;
  175. static LLColor4 cyan2;
  176. static LLColor4 cyan3;
  177. static LLColor4 cyan4;
  178. static LLColor4 cyan5;
  179. static LLColor4 cyan6;
  180. static BOOL parseColor(const std::string& buf, LLColor4* color);
  181. static BOOL parseColor4(const std::string& buf, LLColor4* color);
  182. inline void clamp();
  183. };
  184. // Non-member functions
  185. F32 distVec(const LLColor4 &a, const LLColor4 &b); // Returns distance between a and b
  186. F32 distVec_squared(const LLColor4 &a, const LLColor4 &b); // Returns distance squared between a and b
  187. LLColor3 vec4to3(const LLColor4 &vec);
  188. LLColor4 vec3to4(const LLColor3 &vec);
  189. LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u);
  190. inline LLColor4::LLColor4(void)
  191. {
  192. mV[VX] = 0.f;
  193. mV[VY] = 0.f;
  194. mV[VZ] = 0.f;
  195. mV[VW] = 1.f;
  196. }
  197. inline LLColor4::LLColor4(const LLSD& sd)
  198. {
  199. this->setValue(sd);
  200. }
  201. inline LLColor4::LLColor4(F32 r, F32 g, F32 b)
  202. {
  203. mV[VX] = r;
  204. mV[VY] = g;
  205. mV[VZ] = b;
  206. mV[VW] = 1.f;
  207. }
  208. inline LLColor4::LLColor4(F32 r, F32 g, F32 b, F32 a)
  209. {
  210. mV[VX] = r;
  211. mV[VY] = g;
  212. mV[VZ] = b;
  213. mV[VW] = a;
  214. }
  215. inline LLColor4::LLColor4(U32 clr)
  216. {
  217. mV[VX] = (clr&0xff) * (1.0f/255.0f);
  218. mV[VY] = ((clr>>8)&0xff) * (1.0f/255.0f);
  219. mV[VZ] = ((clr>>16)&0xff) * (1.0f/255.0f);
  220. mV[VW] = (clr>>24) * (1.0f/255.0f);
  221. }
  222. inline LLColor4::LLColor4(const F32 *vec)
  223. {
  224. mV[VX] = vec[VX];
  225. mV[VY] = vec[VY];
  226. mV[VZ] = vec[VZ];
  227. mV[VW] = vec[VW];
  228. }
  229. inline const LLColor4& LLColor4::setToBlack(void)
  230. {
  231. mV[VX] = 0.f;
  232. mV[VY] = 0.f;
  233. mV[VZ] = 0.f;
  234. mV[VW] = 1.f;
  235. return (*this);
  236. }
  237. inline const LLColor4& LLColor4::setToWhite(void)
  238. {
  239. mV[VX] = 1.f;
  240. mV[VY] = 1.f;
  241. mV[VZ] = 1.f;
  242. mV[VW] = 1.f;
  243. return (*this);
  244. }
  245. inline const LLColor4& LLColor4::set(F32 x, F32 y, F32 z)
  246. {
  247. mV[VX] = x;
  248. mV[VY] = y;
  249. mV[VZ] = z;
  250. // no change to alpha!
  251. // mV[VW] = 1.f;
  252. return (*this);
  253. }
  254. inline const LLColor4& LLColor4::set(F32 x, F32 y, F32 z, F32 a)
  255. {
  256. mV[VX] = x;
  257. mV[VY] = y;
  258. mV[VZ] = z;
  259. mV[VW] = a;
  260. return (*this);
  261. }
  262. inline const LLColor4& LLColor4::set(const LLColor4 &vec)
  263. {
  264. mV[VX] = vec.mV[VX];
  265. mV[VY] = vec.mV[VY];
  266. mV[VZ] = vec.mV[VZ];
  267. mV[VW] = vec.mV[VW];
  268. return (*this);
  269. }
  270. inline const LLColor4& LLColor4::set(const F32 *vec)
  271. {
  272. mV[VX] = vec[VX];
  273. mV[VY] = vec[VY];
  274. mV[VZ] = vec[VZ];
  275. mV[VW] = vec[VW];
  276. return (*this);
  277. }
  278. // deprecated
  279. inline const LLColor4& LLColor4::setVec(F32 x, F32 y, F32 z)
  280. {
  281. mV[VX] = x;
  282. mV[VY] = y;
  283. mV[VZ] = z;
  284. // no change to alpha!
  285. // mV[VW] = 1.f;
  286. return (*this);
  287. }
  288. // deprecated
  289. inline const LLColor4& LLColor4::setVec(F32 x, F32 y, F32 z, F32 a)
  290. {
  291. mV[VX] = x;
  292. mV[VY] = y;
  293. mV[VZ] = z;
  294. mV[VW] = a;
  295. return (*this);
  296. }
  297. // deprecated
  298. inline const LLColor4& LLColor4::setVec(const LLColor4 &vec)
  299. {
  300. mV[VX] = vec.mV[VX];
  301. mV[VY] = vec.mV[VY];
  302. mV[VZ] = vec.mV[VZ];
  303. mV[VW] = vec.mV[VW];
  304. return (*this);
  305. }
  306. // deprecated
  307. inline const LLColor4& LLColor4::setVec(const F32 *vec)
  308. {
  309. mV[VX] = vec[VX];
  310. mV[VY] = vec[VY];
  311. mV[VZ] = vec[VZ];
  312. mV[VW] = vec[VW];
  313. return (*this);
  314. }
  315. inline const LLColor4& LLColor4::setAlpha(F32 a)
  316. {
  317. mV[VW] = a;
  318. return (*this);
  319. }
  320. // LLColor4 Magnitude and Normalization Functions
  321. inline F32 LLColor4::length(void) const
  322. {
  323. return (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
  324. }
  325. inline F32 LLColor4::lengthSquared(void) const
  326. {
  327. return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ];
  328. }
  329. inline F32 LLColor4::normalize(void)
  330. {
  331. F32 mag = (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
  332. F32 oomag;
  333. if (mag)
  334. {
  335. oomag = 1.f/mag;
  336. mV[VX] *= oomag;
  337. mV[VY] *= oomag;
  338. mV[VZ] *= oomag;
  339. }
  340. return (mag);
  341. }
  342. // deprecated
  343. inline F32 LLColor4::magVec(void) const
  344. {
  345. return (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
  346. }
  347. // deprecated
  348. inline F32 LLColor4::magVecSquared(void) const
  349. {
  350. return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ];
  351. }
  352. // deprecated
  353. inline F32 LLColor4::normVec(void)
  354. {
  355. F32 mag = (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
  356. F32 oomag;
  357. if (mag)
  358. {
  359. oomag = 1.f/mag;
  360. mV[VX] *= oomag;
  361. mV[VY] *= oomag;
  362. mV[VZ] *= oomag;
  363. }
  364. return (mag);
  365. }
  366. // LLColor4 Operators
  367. inline LLColor4 operator+(const LLColor4 &a, const LLColor4 &b)
  368. {
  369. return LLColor4(
  370. a.mV[VX] + b.mV[VX],
  371. a.mV[VY] + b.mV[VY],
  372. a.mV[VZ] + b.mV[VZ],
  373. a.mV[VW] + b.mV[VW]);
  374. }
  375. inline LLColor4 operator-(const LLColor4 &a, const LLColor4 &b)
  376. {
  377. return LLColor4(
  378. a.mV[VX] - b.mV[VX],
  379. a.mV[VY] - b.mV[VY],
  380. a.mV[VZ] - b.mV[VZ],
  381. a.mV[VW] - b.mV[VW]);
  382. }
  383. inline LLColor4 operator*(const LLColor4 &a, const LLColor4 &b)
  384. {
  385. return LLColor4(
  386. a.mV[VX] * b.mV[VX],
  387. a.mV[VY] * b.mV[VY],
  388. a.mV[VZ] * b.mV[VZ],
  389. a.mV[VW] * b.mV[VW]);
  390. }
  391. inline LLColor4 operator*(const LLColor4 &a, F32 k)
  392. {
  393. // only affects rgb (not a!)
  394. return LLColor4(
  395. a.mV[VX] * k,
  396. a.mV[VY] * k,
  397. a.mV[VZ] * k,
  398. a.mV[VW]);
  399. }
  400. inline LLColor4 operator*(F32 k, const LLColor4 &a)
  401. {
  402. // only affects rgb (not a!)
  403. return LLColor4(
  404. a.mV[VX] * k,
  405. a.mV[VY] * k,
  406. a.mV[VZ] * k,
  407. a.mV[VW]);
  408. }
  409. inline LLColor4 operator%(F32 k, const LLColor4 &a)
  410. {
  411. // only affects alpha (not rgb!)
  412. return LLColor4(
  413. a.mV[VX],
  414. a.mV[VY],
  415. a.mV[VZ],
  416. a.mV[VW] * k);
  417. }
  418. inline LLColor4 operator%(const LLColor4 &a, F32 k)
  419. {
  420. // only affects alpha (not rgb!)
  421. return LLColor4(
  422. a.mV[VX],
  423. a.mV[VY],
  424. a.mV[VZ],
  425. a.mV[VW] * k);
  426. }
  427. inline bool operator==(const LLColor4 &a, const LLColor4 &b)
  428. {
  429. return ( (a.mV[VX] == b.mV[VX])
  430. &&(a.mV[VY] == b.mV[VY])
  431. &&(a.mV[VZ] == b.mV[VZ])
  432. &&(a.mV[VW] == b.mV[VW]));
  433. }
  434. inline bool operator!=(const LLColor4 &a, const LLColor4 &b)
  435. {
  436. return ( (a.mV[VX] != b.mV[VX])
  437. ||(a.mV[VY] != b.mV[VY])
  438. ||(a.mV[VZ] != b.mV[VZ])
  439. ||(a.mV[VW] != b.mV[VW]));
  440. }
  441. inline const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b)
  442. {
  443. a.mV[VX] += b.mV[VX];
  444. a.mV[VY] += b.mV[VY];
  445. a.mV[VZ] += b.mV[VZ];
  446. a.mV[VW] += b.mV[VW];
  447. return a;
  448. }
  449. inline const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b)
  450. {
  451. a.mV[VX] -= b.mV[VX];
  452. a.mV[VY] -= b.mV[VY];
  453. a.mV[VZ] -= b.mV[VZ];
  454. a.mV[VW] -= b.mV[VW];
  455. return a;
  456. }
  457. inline const LLColor4& operator*=(LLColor4 &a, F32 k)
  458. {
  459. // only affects rgb (not a!)
  460. a.mV[VX] *= k;
  461. a.mV[VY] *= k;
  462. a.mV[VZ] *= k;
  463. return a;
  464. }
  465. inline const LLColor4& operator *=(LLColor4 &a, const LLColor4 &b)
  466. {
  467. a.mV[VX] *= b.mV[VX];
  468. a.mV[VY] *= b.mV[VY];
  469. a.mV[VZ] *= b.mV[VZ];
  470. // a.mV[VW] *= b.mV[VW];
  471. return a;
  472. }
  473. inline const LLColor4& operator%=(LLColor4 &a, F32 k)
  474. {
  475. // only affects alpha (not rgb!)
  476. a.mV[VW] *= k;
  477. return a;
  478. }
  479. // Non-member functions
  480. inline F32 distVec(const LLColor4 &a, const LLColor4 &b)
  481. {
  482. LLColor4 vec = a - b;
  483. return (vec.length());
  484. }
  485. inline F32 distVec_squared(const LLColor4 &a, const LLColor4 &b)
  486. {
  487. LLColor4 vec = a - b;
  488. return (vec.lengthSquared());
  489. }
  490. inline LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u)
  491. {
  492. return LLColor4(
  493. a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
  494. a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
  495. a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u,
  496. a.mV[VW] + (b.mV[VW] - a.mV[VW]) * u);
  497. }
  498. inline bool LLColor4::operator<(const LLColor4& rhs) const
  499. {
  500. if (mV[0] != rhs.mV[0])
  501. {
  502. return mV[0] < rhs.mV[0];
  503. }
  504. if (mV[1] != rhs.mV[1])
  505. {
  506. return mV[1] < rhs.mV[1];
  507. }
  508. if (mV[2] != rhs.mV[2])
  509. {
  510. return mV[2] < rhs.mV[2];
  511. }
  512. return mV[3] < rhs.mV[3];
  513. }
  514. void LLColor4::clamp()
  515. {
  516. // Clamp the color...
  517. if (mV[0] < 0.f)
  518. {
  519. mV[0] = 0.f;
  520. }
  521. else if (mV[0] > 1.f)
  522. {
  523. mV[0] = 1.f;
  524. }
  525. if (mV[1] < 0.f)
  526. {
  527. mV[1] = 0.f;
  528. }
  529. else if (mV[1] > 1.f)
  530. {
  531. mV[1] = 1.f;
  532. }
  533. if (mV[2] < 0.f)
  534. {
  535. mV[2] = 0.f;
  536. }
  537. else if (mV[2] > 1.f)
  538. {
  539. mV[2] = 1.f;
  540. }
  541. if (mV[3] < 0.f)
  542. {
  543. mV[3] = 0.f;
  544. }
  545. else if (mV[3] > 1.f)
  546. {
  547. mV[3] = 1.f;
  548. }
  549. }
  550. #endif