llcolor4u.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @file llcolor4u.h
  3. * @brief The LLColor4U class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-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. #ifndef LL_V4COLORU_H
  33. #define LL_V4COLORU_H
  34. #include "llmath.h"
  35. #include "llcolor3.h"
  36. #include "llcolor4.h"
  37. class LLColor4;
  38. // LLColor4U = | red green blue alpha |
  39. constexpr U32 LENGTHOFCOLOR4U = 4;
  40. class LLColor4U
  41. {
  42. public:
  43. LL_INLINE LLColor4U() noexcept
  44. {
  45. mV[VX] = mV[VY] = mV[VZ] = 0;
  46. mV[VW] = 255;
  47. }
  48. LL_INLINE LLColor4U(U8 r, U8 g, U8 b) noexcept
  49. {
  50. mV[VX] = r;
  51. mV[VY] = g;
  52. mV[VZ] = b;
  53. mV[VW] = 255;
  54. }
  55. LL_INLINE LLColor4U(U8 r, U8 g, U8 b, U8 a) noexcept
  56. {
  57. mV[VX] = r;
  58. mV[VY] = g;
  59. mV[VZ] = b;
  60. mV[VW] = a;
  61. }
  62. LL_INLINE LLColor4U(const U8* vec) noexcept
  63. {
  64. mV[VX] = vec[VX];
  65. mV[VY] = vec[VY];
  66. mV[VZ] = vec[VZ];
  67. mV[VW] = vec[VW];
  68. }
  69. #if 0
  70. LL_INLINE LLColor4U(const LLColor3& vec) noexcept
  71. {
  72. mV[VX] = vec.mV[VX];
  73. mV[VY] = vec.mV[VY];
  74. mV[VZ] = vec.mV[VZ];
  75. mV[VW] = 255;
  76. }
  77. #endif
  78. LL_INLINE void setValue(const LLSD& sd) noexcept
  79. {
  80. mV[0] = sd[0].asInteger();
  81. mV[1] = sd[1].asInteger();
  82. mV[2] = sd[2].asInteger();
  83. mV[3] = sd[3].asInteger();
  84. }
  85. LL_INLINE explicit LLColor4U(const LLSD& sd) { setValue(sd); }
  86. // Allow the use of the default C++11 move constructor and assignation
  87. LLColor4U(LLColor4U&& other) noexcept = default;
  88. LLColor4U& operator=(LLColor4U&& other) noexcept = default;
  89. LLColor4U(const LLColor4U& other) = default;
  90. LLColor4U& operator=(const LLColor4U& other) = default;
  91. LL_INLINE LLSD getValue() const
  92. {
  93. LLSD ret;
  94. ret[0] = mV[0];
  95. ret[1] = mV[1];
  96. ret[2] = mV[2];
  97. ret[3] = mV[3];
  98. return ret;
  99. }
  100. LL_INLINE U32 asRGBA() const
  101. {
  102. U32 rgba = 0;
  103. // Little endian: values are swapped in memory. The original code
  104. // access the array like a U32, so we need to swap here
  105. rgba |= mV[3];
  106. rgba <<= 8;
  107. rgba |= mV[2];
  108. rgba <<= 8;
  109. rgba |= mV[1];
  110. rgba <<= 8;
  111. rgba |= mV[0];
  112. return rgba;
  113. }
  114. LL_INLINE void fromRGBA(U32 rgba)
  115. {
  116. // Little endian: values are swapped in memory. The original code
  117. // access the array like a U32, so we need to swap here
  118. mV[0] = rgba & 0xFF;
  119. rgba >>= 8;
  120. mV[1] = rgba & 0xFF;
  121. rgba >>= 8;
  122. mV[2] = rgba & 0xFF;
  123. rgba >>= 8;
  124. mV[3] = rgba & 0xFF;
  125. }
  126. LL_INLINE const LLColor4U& setToBlack()
  127. {
  128. mV[VX] = mV[VY] = mV[VZ] = 0;
  129. mV[VW] = 255;
  130. return *this;
  131. }
  132. LL_INLINE const LLColor4U& setToWhite()
  133. {
  134. mV[VX] = mV[VY] = mV[VZ] = mV[VW] = 255;
  135. return *this;
  136. }
  137. LL_INLINE const LLColor4U& set(U8 x, U8 y, U8 z)
  138. {
  139. mV[VX] = x;
  140. mV[VY] = y;
  141. mV[VZ] = z;
  142. #if 0 // no change to alpha !
  143. mV[VW] = 255;
  144. #endif
  145. return *this;
  146. }
  147. LL_INLINE const LLColor4U& set(U8 r, U8 g, U8 b, U8 a)
  148. {
  149. mV[0] = r;
  150. mV[1] = g;
  151. mV[2] = b;
  152. mV[3] = a;
  153. return *this;
  154. }
  155. LL_INLINE const LLColor4U& set(const LLColor4U& vec)
  156. {
  157. mV[VX] = vec.mV[VX];
  158. mV[VY] = vec.mV[VY];
  159. mV[VZ] = vec.mV[VZ];
  160. mV[VW] = vec.mV[VW];
  161. return *this;
  162. }
  163. LL_INLINE const LLColor4U& set(const U8* vec)
  164. {
  165. mV[VX] = vec[VX];
  166. mV[VY] = vec[VY];
  167. mV[VZ] = vec[VZ];
  168. mV[VW] = vec[VW];
  169. return *this;
  170. }
  171. LL_INLINE const LLColor4U& setAlpha(U8 a)
  172. {
  173. mV[VW] = a;
  174. return *this;
  175. }
  176. // Returns magnitude of LLColor4U
  177. LL_INLINE F32 length() const
  178. {
  179. return sqrtf(((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] +
  180. ((F32)mV[VZ]) * mV[VZ]);
  181. }
  182. // Returns squared magnitude of LLColor4U
  183. LL_INLINE F32 lengthSquared() const
  184. {
  185. return ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] +
  186. ((F32)mV[VZ]) * mV[VZ];
  187. }
  188. // Prints a
  189. friend std::ostream& operator<<(std::ostream& s, const LLColor4U& a);
  190. // Returns vector a + b
  191. friend LLColor4U operator+(const LLColor4U& a, const LLColor4U& b);
  192. // Returns vector a minus b
  193. friend LLColor4U operator-(const LLColor4U& a, const LLColor4U& b);
  194. // Returns a * b
  195. friend LLColor4U operator*(const LLColor4U& a, const LLColor4U& b);
  196. // Returns a == b
  197. friend bool operator==(const LLColor4U& a, const LLColor4U& b);
  198. // Returns a != b
  199. friend bool operator!=(const LLColor4U& a, const LLColor4U& b);
  200. // Returns vector a + b
  201. friend const LLColor4U& operator+=(LLColor4U& a, const LLColor4U& b);
  202. // Returns vector a minus b
  203. friend const LLColor4U& operator-=(LLColor4U& a, const LLColor4U& b);
  204. // Returns rgb times scaler k (no alpha change)
  205. friend const LLColor4U& operator*=(LLColor4U& a, U8 k);
  206. // Returns alpha times scaler k (no rgb change)
  207. friend const LLColor4U& operator%=(LLColor4U& a, U8 k);
  208. LLColor4U addClampMax(const LLColor4U& color); // Add and clamp the max
  209. LLColor4U multAll(F32 k); // Multiplies ALL channels by scalar k
  210. const LLColor4U& combine();
  211. void setVecScaleClamp(const LLColor3& color);
  212. void setVecScaleClamp(const LLColor4& color);
  213. // Note: when 'strict' is false, a 3 numbers vector is accepted and the
  214. // missing alpha value is then set to 255 (opaque). HB
  215. static bool parseColor4U(const std::string& buf, LLColor4U* value,
  216. bool strict = true);
  217. // Conversion
  218. LL_INLINE operator LLColor4() const
  219. {
  220. return LLColor4(*this);
  221. }
  222. #if 0
  223. LL_INLINE operator LLColor4()
  224. {
  225. return LLColor4((F32)mV[VRED] / 255.f,
  226. (F32)mV[VGREEN] / 255.f,
  227. (F32)mV[VBLUE] / 255.f,
  228. (F32)mV[VALPHA] / 255.f));
  229. }
  230. #endif
  231. public:
  232. union
  233. {
  234. U8 mV[LENGTHOFCOLOR4U];
  235. U32 mAll;
  236. };
  237. public:
  238. static LLColor4U white;
  239. static LLColor4U black;
  240. static LLColor4U red;
  241. static LLColor4U green;
  242. static LLColor4U blue;
  243. };
  244. LL_INLINE LLColor4U operator+(const LLColor4U& a, const LLColor4U& b)
  245. {
  246. return LLColor4U(a.mV[VX] + b.mV[VX], a.mV[VY] + b.mV[VY],
  247. a.mV[VZ] + b.mV[VZ], a.mV[VW] + b.mV[VW]);
  248. }
  249. LL_INLINE LLColor4U operator-(const LLColor4U& a, const LLColor4U& b)
  250. {
  251. return LLColor4U(a.mV[VX] - b.mV[VX], a.mV[VY] - b.mV[VY],
  252. a.mV[VZ] - b.mV[VZ], a.mV[VW] - b.mV[VW]);
  253. }
  254. LL_INLINE LLColor4U operator*(const LLColor4U& a, const LLColor4U& b)
  255. {
  256. return LLColor4U(a.mV[VX] * b.mV[VX], a.mV[VY] * b.mV[VY],
  257. a.mV[VZ] * b.mV[VZ], a.mV[VW] * b.mV[VW]);
  258. }
  259. LL_INLINE LLColor4U LLColor4U::addClampMax(const LLColor4U& color)
  260. {
  261. return LLColor4U(llmin((S32)mV[VX] + color.mV[VX], 255),
  262. llmin((S32)mV[VY] + color.mV[VY], 255),
  263. llmin((S32)mV[VZ] + color.mV[VZ], 255),
  264. llmin((S32)mV[VW] + color.mV[VW], 255));
  265. }
  266. LL_INLINE LLColor4U LLColor4U::multAll(F32 k)
  267. {
  268. // Round to nearest
  269. return LLColor4U((U8)ll_roundp(mV[VX] * k), (U8)ll_roundp(mV[VY] * k),
  270. (U8)ll_roundp(mV[VZ] * k), (U8)ll_roundp(mV[VW] * k));
  271. }
  272. #if 0
  273. LL_INLINE LLColor4U operator*(const LLColor4U& a, U8 k)
  274. {
  275. // Only affects rgb (not a !)
  276. return LLColor4U(a.mV[VX] * k, a.mV[VY] * k, a.mV[VZ] * k, a.mV[VW]);
  277. }
  278. LL_INLINE LLColor4U operator*(U8 k, const LLColor4U& a)
  279. {
  280. // Only affects rgb (not a !)
  281. return LLColor4U(a.mV[VX] * k, a.mV[VY] * k, a.mV[VZ] * k, a.mV[VW]);
  282. }
  283. LL_INLINE LLColor4U operator%(U8 k, const LLColor4U& a)
  284. {
  285. // Only affects alpha (not rgb !)
  286. return LLColor4U(a.mV[VX], a.mV[VY], a.mV[VZ], a.mV[VW] * k);
  287. }
  288. LL_INLINE LLColor4U operator%(const LLColor4U& a, U8 k)
  289. {
  290. // Only affects alpha (not rgb !)
  291. return LLColor4U(a.mV[VX], a.mV[VY], a.mV[VZ], a.mV[VW] * k);
  292. }
  293. LL_INLINE LLColor4U operator=(const LLColor3& a)
  294. {
  295. mV[VX] = a.mV[VX];
  296. mV[VY] = a.mV[VY];
  297. mV[VZ] = a.mV[VZ];
  298. // Converting from an rgb sets a=1 (opaque)
  299. mV[VW] = 255;
  300. return *this;
  301. }
  302. #endif
  303. LL_INLINE bool operator==(const LLColor4U& a, const LLColor4U& b)
  304. {
  305. return (a.mV[VX] == b.mV[VX] && a.mV[VY] == b.mV[VY] &&
  306. a.mV[VZ] == b.mV[VZ] && a.mV[VW] == b.mV[VW]);
  307. }
  308. LL_INLINE bool operator!=(const LLColor4U& a, const LLColor4U& b)
  309. {
  310. return (a.mV[VX] != b.mV[VX] || a.mV[VY] != b.mV[VY] ||
  311. a.mV[VZ] != b.mV[VZ] || a.mV[VW] != b.mV[VW]);
  312. }
  313. LL_INLINE const LLColor4U& operator+=(LLColor4U& a, const LLColor4U& b)
  314. {
  315. a.mV[VX] += b.mV[VX];
  316. a.mV[VY] += b.mV[VY];
  317. a.mV[VZ] += b.mV[VZ];
  318. a.mV[VW] += b.mV[VW];
  319. return a;
  320. }
  321. LL_INLINE const LLColor4U& operator-=(LLColor4U& a, const LLColor4U& b)
  322. {
  323. a.mV[VX] -= b.mV[VX];
  324. a.mV[VY] -= b.mV[VY];
  325. a.mV[VZ] -= b.mV[VZ];
  326. a.mV[VW] -= b.mV[VW];
  327. return a;
  328. }
  329. LL_INLINE const LLColor4U& operator*=(LLColor4U& a, U8 k)
  330. {
  331. // Only affects rgb (not a !)
  332. a.mV[VX] *= k;
  333. a.mV[VY] *= k;
  334. a.mV[VZ] *= k;
  335. return a;
  336. }
  337. LL_INLINE const LLColor4U& operator%=(LLColor4U& a, U8 k)
  338. {
  339. // Only affects alpha (not rgb !)
  340. a.mV[VW] *= k;
  341. return a;
  342. }
  343. // Non-member functions
  344. // Returns distance between a and b
  345. LL_INLINE F32 distVec(const LLColor4U& a, const LLColor4U& b)
  346. {
  347. LLColor4U vec = a - b;
  348. return vec.length();
  349. }
  350. // Returns distance squared between a and b
  351. LL_INLINE F32 distVec_squared(const LLColor4U& a, const LLColor4U& b)
  352. {
  353. LLColor4U vec = a - b;
  354. return vec.lengthSquared();
  355. }
  356. #endif // LL_V4COLORU_H