PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/v4coloru.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 570 lines | 397 code | 80 blank | 93 comment | 28 complexity | a363eb378f5e041e4dad2e8d4b3a6cae MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v4coloru.h
  3. * @brief The LLColor4U class.
  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_V4COLORU_H
  27. #define LL_V4COLORU_H
  28. #include "llerror.h"
  29. //#include "vmath.h"
  30. #include "llmath.h"
  31. //#include "v4color.h"
  32. #include "v3color.h"
  33. #include "v4color.h"
  34. //class LLColor3U;
  35. class LLColor4;
  36. // LLColor4U = | red green blue alpha |
  37. static const U32 LENGTHOFCOLOR4U = 4;
  38. class LLColor4U
  39. {
  40. public:
  41. union
  42. {
  43. U8 mV[LENGTHOFCOLOR4U];
  44. U32 mAll;
  45. LLColor4* mSources;
  46. LLColor4U* mSourcesU;
  47. };
  48. LLColor4U(); // Initializes LLColor4U to (0, 0, 0, 1)
  49. LLColor4U(U8 r, U8 g, U8 b); // Initializes LLColor4U to (r, g, b, 1)
  50. LLColor4U(U8 r, U8 g, U8 b, U8 a); // Initializes LLColor4U to (r. g, b, a)
  51. LLColor4U(const U8 *vec); // Initializes LLColor4U to (vec[0]. vec[1], vec[2], 1)
  52. explicit LLColor4U(const LLSD& sd)
  53. {
  54. setValue(sd);
  55. }
  56. void setValue(const LLSD& sd)
  57. {
  58. mV[0] = sd[0].asInteger();
  59. mV[1] = sd[1].asInteger();
  60. mV[2] = sd[2].asInteger();
  61. mV[3] = sd[3].asInteger();
  62. }
  63. LLSD getValue() const
  64. {
  65. LLSD ret;
  66. ret[0] = mV[0];
  67. ret[1] = mV[1];
  68. ret[2] = mV[2];
  69. ret[3] = mV[3];
  70. return ret;
  71. }
  72. const LLColor4U& setToBlack(); // zero LLColor4U to (0, 0, 0, 1)
  73. const LLColor4U& setToWhite(); // zero LLColor4U to (0, 0, 0, 1)
  74. const LLColor4U& set(U8 r, U8 g, U8 b, U8 a);// Sets LLColor4U to (r, g, b, a)
  75. const LLColor4U& set(U8 r, U8 g, U8 b); // Sets LLColor4U to (r, g, b) (no change in a)
  76. const LLColor4U& set(const LLColor4U &vec); // Sets LLColor4U to vec
  77. const LLColor4U& set(const U8 *vec); // Sets LLColor4U to vec
  78. const LLColor4U& setVec(U8 r, U8 g, U8 b, U8 a); // deprecated -- use set()
  79. const LLColor4U& setVec(U8 r, U8 g, U8 b); // deprecated -- use set()
  80. const LLColor4U& setVec(const LLColor4U &vec); // deprecated -- use set()
  81. const LLColor4U& setVec(const U8 *vec); // deprecated -- use set()
  82. const LLColor4U& setAlpha(U8 a);
  83. F32 magVec() const; // deprecated -- use length()
  84. F32 magVecSquared() const; // deprecated -- use lengthSquared()
  85. F32 length() const; // Returns magnitude squared of LLColor4U
  86. F32 lengthSquared() const; // Returns magnitude squared of LLColor4U
  87. friend std::ostream& operator<<(std::ostream& s, const LLColor4U &a); // Print a
  88. friend LLColor4U operator+(const LLColor4U &a, const LLColor4U &b); // Return vector a + b
  89. friend LLColor4U operator-(const LLColor4U &a, const LLColor4U &b); // Return vector a minus b
  90. friend LLColor4U operator*(const LLColor4U &a, const LLColor4U &b); // Return a * b
  91. friend bool operator==(const LLColor4U &a, const LLColor4U &b); // Return a == b
  92. friend bool operator!=(const LLColor4U &a, const LLColor4U &b); // Return a != b
  93. friend const LLColor4U& operator+=(LLColor4U &a, const LLColor4U &b); // Return vector a + b
  94. friend const LLColor4U& operator-=(LLColor4U &a, const LLColor4U &b); // Return vector a minus b
  95. friend const LLColor4U& operator*=(LLColor4U &a, U8 k); // Return rgb times scaler k (no alpha change)
  96. friend const LLColor4U& operator%=(LLColor4U &a, U8 k); // Return alpha times scaler k (no rgb change)
  97. LLColor4U addClampMax(const LLColor4U &color); // Add and clamp the max
  98. LLColor4U multAll(const F32 k); // Multiply ALL channels by scalar k
  99. const LLColor4U& combine();
  100. inline void setVecScaleClamp(const LLColor3 &color);
  101. inline void setVecScaleClamp(const LLColor4 &color);
  102. static BOOL parseColor4U(const std::string& buf, LLColor4U* value);
  103. // conversion
  104. operator const LLColor4() const
  105. {
  106. return LLColor4(*this);
  107. }
  108. static LLColor4U white;
  109. static LLColor4U black;
  110. static LLColor4U red;
  111. static LLColor4U green;
  112. static LLColor4U blue;
  113. };
  114. // Non-member functions
  115. F32 distVec(const LLColor4U &a, const LLColor4U &b); // Returns distance between a and b
  116. F32 distVec_squared(const LLColor4U &a, const LLColor4U &b); // Returns distance squared between a and b
  117. inline LLColor4U::LLColor4U()
  118. {
  119. mV[VX] = 0;
  120. mV[VY] = 0;
  121. mV[VZ] = 0;
  122. mV[VW] = 255;
  123. }
  124. inline LLColor4U::LLColor4U(U8 r, U8 g, U8 b)
  125. {
  126. mV[VX] = r;
  127. mV[VY] = g;
  128. mV[VZ] = b;
  129. mV[VW] = 255;
  130. }
  131. inline LLColor4U::LLColor4U(U8 r, U8 g, U8 b, U8 a)
  132. {
  133. mV[VX] = r;
  134. mV[VY] = g;
  135. mV[VZ] = b;
  136. mV[VW] = a;
  137. }
  138. inline LLColor4U::LLColor4U(const U8 *vec)
  139. {
  140. mV[VX] = vec[VX];
  141. mV[VY] = vec[VY];
  142. mV[VZ] = vec[VZ];
  143. mV[VW] = vec[VW];
  144. }
  145. /*
  146. inline LLColor4U::operator LLColor4()
  147. {
  148. return(LLColor4((F32)mV[VRED]/255.f,(F32)mV[VGREEN]/255.f,(F32)mV[VBLUE]/255.f,(F32)mV[VALPHA]/255.f));
  149. }
  150. */
  151. inline const LLColor4U& LLColor4U::setToBlack(void)
  152. {
  153. mV[VX] = 0;
  154. mV[VY] = 0;
  155. mV[VZ] = 0;
  156. mV[VW] = 255;
  157. return (*this);
  158. }
  159. inline const LLColor4U& LLColor4U::setToWhite(void)
  160. {
  161. mV[VX] = 255;
  162. mV[VY] = 255;
  163. mV[VZ] = 255;
  164. mV[VW] = 255;
  165. return (*this);
  166. }
  167. inline const LLColor4U& LLColor4U::set(const U8 x, const U8 y, const U8 z)
  168. {
  169. mV[VX] = x;
  170. mV[VY] = y;
  171. mV[VZ] = z;
  172. // no change to alpha!
  173. // mV[VW] = 255;
  174. return (*this);
  175. }
  176. inline const LLColor4U& LLColor4U::set(const U8 r, const U8 g, const U8 b, U8 a)
  177. {
  178. mV[0] = r;
  179. mV[1] = g;
  180. mV[2] = b;
  181. mV[3] = a;
  182. return (*this);
  183. }
  184. inline const LLColor4U& LLColor4U::set(const LLColor4U &vec)
  185. {
  186. mV[VX] = vec.mV[VX];
  187. mV[VY] = vec.mV[VY];
  188. mV[VZ] = vec.mV[VZ];
  189. mV[VW] = vec.mV[VW];
  190. return (*this);
  191. }
  192. inline const LLColor4U& LLColor4U::set(const U8 *vec)
  193. {
  194. mV[VX] = vec[VX];
  195. mV[VY] = vec[VY];
  196. mV[VZ] = vec[VZ];
  197. mV[VW] = vec[VW];
  198. return (*this);
  199. }
  200. // deprecated
  201. inline const LLColor4U& LLColor4U::setVec(const U8 x, const U8 y, const U8 z)
  202. {
  203. mV[VX] = x;
  204. mV[VY] = y;
  205. mV[VZ] = z;
  206. // no change to alpha!
  207. // mV[VW] = 255;
  208. return (*this);
  209. }
  210. // deprecated
  211. inline const LLColor4U& LLColor4U::setVec(const U8 r, const U8 g, const U8 b, U8 a)
  212. {
  213. mV[0] = r;
  214. mV[1] = g;
  215. mV[2] = b;
  216. mV[3] = a;
  217. return (*this);
  218. }
  219. // deprecated
  220. inline const LLColor4U& LLColor4U::setVec(const LLColor4U &vec)
  221. {
  222. mV[VX] = vec.mV[VX];
  223. mV[VY] = vec.mV[VY];
  224. mV[VZ] = vec.mV[VZ];
  225. mV[VW] = vec.mV[VW];
  226. return (*this);
  227. }
  228. // deprecated
  229. inline const LLColor4U& LLColor4U::setVec(const U8 *vec)
  230. {
  231. mV[VX] = vec[VX];
  232. mV[VY] = vec[VY];
  233. mV[VZ] = vec[VZ];
  234. mV[VW] = vec[VW];
  235. return (*this);
  236. }
  237. inline const LLColor4U& LLColor4U::setAlpha(U8 a)
  238. {
  239. mV[VW] = a;
  240. return (*this);
  241. }
  242. // LLColor4U Magnitude and Normalization Functions
  243. inline F32 LLColor4U::length(void) const
  244. {
  245. return (F32) sqrt( ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ] );
  246. }
  247. inline F32 LLColor4U::lengthSquared(void) const
  248. {
  249. return ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ];
  250. }
  251. // deprecated
  252. inline F32 LLColor4U::magVec(void) const
  253. {
  254. return (F32) sqrt( ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ] );
  255. }
  256. // deprecated
  257. inline F32 LLColor4U::magVecSquared(void) const
  258. {
  259. return ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ];
  260. }
  261. inline LLColor4U operator+(const LLColor4U &a, const LLColor4U &b)
  262. {
  263. return LLColor4U(
  264. a.mV[VX] + b.mV[VX],
  265. a.mV[VY] + b.mV[VY],
  266. a.mV[VZ] + b.mV[VZ],
  267. a.mV[VW] + b.mV[VW]);
  268. }
  269. inline LLColor4U operator-(const LLColor4U &a, const LLColor4U &b)
  270. {
  271. return LLColor4U(
  272. a.mV[VX] - b.mV[VX],
  273. a.mV[VY] - b.mV[VY],
  274. a.mV[VZ] - b.mV[VZ],
  275. a.mV[VW] - b.mV[VW]);
  276. }
  277. inline LLColor4U operator*(const LLColor4U &a, const LLColor4U &b)
  278. {
  279. return LLColor4U(
  280. a.mV[VX] * b.mV[VX],
  281. a.mV[VY] * b.mV[VY],
  282. a.mV[VZ] * b.mV[VZ],
  283. a.mV[VW] * b.mV[VW]);
  284. }
  285. inline LLColor4U LLColor4U::addClampMax(const LLColor4U &color)
  286. {
  287. return LLColor4U(llmin((S32)mV[VX] + color.mV[VX], 255),
  288. llmin((S32)mV[VY] + color.mV[VY], 255),
  289. llmin((S32)mV[VZ] + color.mV[VZ], 255),
  290. llmin((S32)mV[VW] + color.mV[VW], 255));
  291. }
  292. inline LLColor4U LLColor4U::multAll(const F32 k)
  293. {
  294. // Round to nearest
  295. return LLColor4U(
  296. (U8)llround(mV[VX] * k),
  297. (U8)llround(mV[VY] * k),
  298. (U8)llround(mV[VZ] * k),
  299. (U8)llround(mV[VW] * k));
  300. }
  301. /*
  302. inline LLColor4U operator*(const LLColor4U &a, U8 k)
  303. {
  304. // only affects rgb (not a!)
  305. return LLColor4U(
  306. a.mV[VX] * k,
  307. a.mV[VY] * k,
  308. a.mV[VZ] * k,
  309. a.mV[VW]);
  310. }
  311. inline LLColor4U operator*(U8 k, const LLColor4U &a)
  312. {
  313. // only affects rgb (not a!)
  314. return LLColor4U(
  315. a.mV[VX] * k,
  316. a.mV[VY] * k,
  317. a.mV[VZ] * k,
  318. a.mV[VW]);
  319. }
  320. inline LLColor4U operator%(U8 k, const LLColor4U &a)
  321. {
  322. // only affects alpha (not rgb!)
  323. return LLColor4U(
  324. a.mV[VX],
  325. a.mV[VY],
  326. a.mV[VZ],
  327. a.mV[VW] * k );
  328. }
  329. inline LLColor4U operator%(const LLColor4U &a, U8 k)
  330. {
  331. // only affects alpha (not rgb!)
  332. return LLColor4U(
  333. a.mV[VX],
  334. a.mV[VY],
  335. a.mV[VZ],
  336. a.mV[VW] * k );
  337. }
  338. */
  339. inline bool operator==(const LLColor4U &a, const LLColor4U &b)
  340. {
  341. return ( (a.mV[VX] == b.mV[VX])
  342. &&(a.mV[VY] == b.mV[VY])
  343. &&(a.mV[VZ] == b.mV[VZ])
  344. &&(a.mV[VW] == b.mV[VW]));
  345. }
  346. inline bool operator!=(const LLColor4U &a, const LLColor4U &b)
  347. {
  348. return ( (a.mV[VX] != b.mV[VX])
  349. ||(a.mV[VY] != b.mV[VY])
  350. ||(a.mV[VZ] != b.mV[VZ])
  351. ||(a.mV[VW] != b.mV[VW]));
  352. }
  353. inline const LLColor4U& operator+=(LLColor4U &a, const LLColor4U &b)
  354. {
  355. a.mV[VX] += b.mV[VX];
  356. a.mV[VY] += b.mV[VY];
  357. a.mV[VZ] += b.mV[VZ];
  358. a.mV[VW] += b.mV[VW];
  359. return a;
  360. }
  361. inline const LLColor4U& operator-=(LLColor4U &a, const LLColor4U &b)
  362. {
  363. a.mV[VX] -= b.mV[VX];
  364. a.mV[VY] -= b.mV[VY];
  365. a.mV[VZ] -= b.mV[VZ];
  366. a.mV[VW] -= b.mV[VW];
  367. return a;
  368. }
  369. inline const LLColor4U& operator*=(LLColor4U &a, U8 k)
  370. {
  371. // only affects rgb (not a!)
  372. a.mV[VX] *= k;
  373. a.mV[VY] *= k;
  374. a.mV[VZ] *= k;
  375. return a;
  376. }
  377. inline const LLColor4U& operator%=(LLColor4U &a, U8 k)
  378. {
  379. // only affects alpha (not rgb!)
  380. a.mV[VW] *= k;
  381. return a;
  382. }
  383. inline F32 distVec(const LLColor4U &a, const LLColor4U &b)
  384. {
  385. LLColor4U vec = a - b;
  386. return (vec.length());
  387. }
  388. inline F32 distVec_squared(const LLColor4U &a, const LLColor4U &b)
  389. {
  390. LLColor4U vec = a - b;
  391. return (vec.lengthSquared());
  392. }
  393. void LLColor4U::setVecScaleClamp(const LLColor4& color)
  394. {
  395. F32 color_scale_factor = 255.f;
  396. F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]);
  397. if (max_color > 1.f)
  398. {
  399. color_scale_factor /= max_color;
  400. }
  401. const S32 MAX_COLOR = 255;
  402. S32 r = llround(color.mV[0] * color_scale_factor);
  403. if (r > MAX_COLOR)
  404. {
  405. r = MAX_COLOR;
  406. }
  407. else if (r < 0)
  408. {
  409. r = 0;
  410. }
  411. mV[0] = r;
  412. S32 g = llround(color.mV[1] * color_scale_factor);
  413. if (g > MAX_COLOR)
  414. {
  415. g = MAX_COLOR;
  416. }
  417. else if (g < 0)
  418. {
  419. g = 0;
  420. }
  421. mV[1] = g;
  422. S32 b = llround(color.mV[2] * color_scale_factor);
  423. if (b > MAX_COLOR)
  424. {
  425. b = MAX_COLOR;
  426. }
  427. else if (b < 0)
  428. {
  429. b = 0;
  430. }
  431. mV[2] = b;
  432. // Alpha shouldn't be scaled, just clamped...
  433. S32 a = llround(color.mV[3] * MAX_COLOR);
  434. if (a > MAX_COLOR)
  435. {
  436. a = MAX_COLOR;
  437. }
  438. else if (a < 0)
  439. {
  440. a = 0;
  441. }
  442. mV[3] = a;
  443. }
  444. void LLColor4U::setVecScaleClamp(const LLColor3& color)
  445. {
  446. F32 color_scale_factor = 255.f;
  447. F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]);
  448. if (max_color > 1.f)
  449. {
  450. color_scale_factor /= max_color;
  451. }
  452. const S32 MAX_COLOR = 255;
  453. S32 r = llround(color.mV[0] * color_scale_factor);
  454. if (r > MAX_COLOR)
  455. {
  456. r = MAX_COLOR;
  457. }
  458. else
  459. if (r < 0)
  460. {
  461. r = 0;
  462. }
  463. mV[0] = r;
  464. S32 g = llround(color.mV[1] * color_scale_factor);
  465. if (g > MAX_COLOR)
  466. {
  467. g = MAX_COLOR;
  468. }
  469. else
  470. if (g < 0)
  471. {
  472. g = 0;
  473. }
  474. mV[1] = g;
  475. S32 b = llround(color.mV[2] * color_scale_factor);
  476. if (b > MAX_COLOR)
  477. {
  478. b = MAX_COLOR;
  479. }
  480. if (b < 0)
  481. {
  482. b = 0;
  483. }
  484. mV[2] = b;
  485. mV[3] = 255;
  486. }
  487. #endif