Cry_Color.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : 4D Color template.
  9. #pragma once
  10. #include <platform.h>
  11. #include <AzCore/std/containers/array.h>
  12. #include "Cry_Math.h"
  13. template <class T>
  14. struct Color_tpl;
  15. typedef Color_tpl<uint8> ColorB; // [ 0, 255]
  16. typedef Color_tpl<float> ColorF; // [0.0, 1.0]
  17. //////////////////////////////////////////////////////////////////////////////////////////////
  18. // RGBA Color structure.
  19. //////////////////////////////////////////////////////////////////////////////////////////////
  20. template <class T>
  21. struct Color_tpl
  22. {
  23. T r, g, b, a;
  24. ILINE Color_tpl() {};
  25. ILINE Color_tpl(T _x, T _y, T _z, T _w);
  26. ILINE Color_tpl(T _x, T _y, T _z);
  27. /* inline Color_tpl(const Color_tpl<T> & v) {
  28. r = v.r; g = v.g; b = v.b; a = v.a;
  29. }*/
  30. // works together with pack_abgr8888
  31. ILINE Color_tpl(const unsigned int abgr);
  32. ILINE Color_tpl(const f32 c);
  33. ILINE Color_tpl(const ColorF& c);
  34. ILINE Color_tpl(const ColorF& c, float fAlpha);
  35. ILINE Color_tpl(const Vec3& c, float fAlpha);
  36. ILINE Color_tpl(const Vec4& c);
  37. ILINE Color_tpl(const Vec3& vVec)
  38. {
  39. r = (T)vVec.x;
  40. g = (T)vVec.y;
  41. b = (T)vVec.z;
  42. a = (T)1.f;
  43. }
  44. ILINE Color_tpl& operator = (const Vec3& v) { r = (T)v.x; g = (T)v.y; b = (T)v.z; a = (T)1.0f; return *this; }
  45. ILINE Color_tpl& operator = (const Color_tpl& c) { r = (T)c.r; g = (T)c.g; b = (T)c.b; a = (T)c.a; return *this; }
  46. ILINE T& operator [] (int index) { assert(index >= 0 && index <= 3); return ((T*)this)[index]; }
  47. ILINE T operator [] (int index) const { assert(index >= 0 && index <= 3); return ((T*)this)[index]; }
  48. ILINE void Set(float fR, float fG, float fB, float fA = 1.0f)
  49. {
  50. r = static_cast<T>(fR);
  51. g = static_cast<T>(fG);
  52. b = static_cast<T>(fB);
  53. a = static_cast<T>(fA);
  54. }
  55. ILINE Color_tpl operator + () const
  56. {
  57. return *this;
  58. }
  59. ILINE Color_tpl operator - () const
  60. {
  61. return Color_tpl<T>(-r, -g, -b, -a);
  62. }
  63. ILINE Color_tpl& operator += (const Color_tpl& v)
  64. {
  65. T _r = r, _g = g, _b = b, _a = a;
  66. _r += v.r;
  67. _g += v.g;
  68. _b += v.b;
  69. _a += v.a;
  70. r = _r;
  71. g = _g;
  72. b = _b;
  73. a = _a;
  74. return *this;
  75. }
  76. ILINE Color_tpl& operator -= (const Color_tpl& v)
  77. {
  78. r -= v.r;
  79. g -= v.g;
  80. b -= v.b;
  81. a -= v.a;
  82. return *this;
  83. }
  84. ILINE Color_tpl& operator *= (const Color_tpl& v)
  85. {
  86. r *= v.r;
  87. g *= v.g;
  88. b *= v.b;
  89. a *= v.a;
  90. return *this;
  91. }
  92. ILINE Color_tpl& operator /= (const Color_tpl& v)
  93. {
  94. r /= v.r;
  95. g /= v.g;
  96. b /= v.b;
  97. a /= v.a;
  98. return *this;
  99. }
  100. ILINE Color_tpl& operator *= (T s)
  101. {
  102. r *= s;
  103. g *= s;
  104. b *= s;
  105. a *= s;
  106. return *this;
  107. }
  108. ILINE Color_tpl& operator /= (T s)
  109. {
  110. s = 1.0f / s;
  111. r *= s;
  112. g *= s;
  113. b *= s;
  114. a *= s;
  115. return *this;
  116. }
  117. ILINE Color_tpl operator + (const Color_tpl& v) const
  118. {
  119. return Color_tpl(r + v.r, g + v.g, b + v.b, a + v.a);
  120. }
  121. ILINE Color_tpl operator - (const Color_tpl& v) const
  122. {
  123. return Color_tpl(r - v.r, g - v.g, b - v.b, a - v.a);
  124. }
  125. ILINE Color_tpl operator * (const Color_tpl& v) const
  126. {
  127. return Color_tpl(r * v.r, g * v.g, b * v.b, a * v.a);
  128. }
  129. ILINE Color_tpl operator / (const Color_tpl& v) const
  130. {
  131. return Color_tpl(r / v.r, g / v.g, b / v.b, a / v.a);
  132. }
  133. ILINE Color_tpl operator * (T s) const
  134. {
  135. return Color_tpl(r * s, g * s, b * s, a * s);
  136. }
  137. ILINE Color_tpl operator / (T s) const
  138. {
  139. s = 1.0f / s;
  140. return Color_tpl(r * s, g * s, b * s, a * s);
  141. }
  142. ILINE bool operator == (const Color_tpl& v) const
  143. {
  144. return (r == v.r) && (g == v.g) && (b == v.b) && (a == v.a);
  145. }
  146. ILINE bool operator != (const Color_tpl& v) const
  147. {
  148. return (r != v.r) || (g != v.g) || (b != v.b) || (a != v.a);
  149. }
  150. ILINE unsigned int pack_abgr8888() const;
  151. ILINE unsigned int pack_argb8888() const;
  152. inline Vec3 toVec3() const { return Vec3(r, g, b); }
  153. inline void clamp(T bottom = 0.0f, T top = 1.0f);
  154. void srgb2rgb()
  155. {
  156. for (int i = 0; i < 3; i++)
  157. {
  158. T& c = (*this)[i];
  159. if (c <= 0.040448643f)
  160. {
  161. c = c / 12.92f;
  162. }
  163. else
  164. {
  165. c = pow((c + 0.055f) / 1.055f, 2.4f);
  166. }
  167. }
  168. }
  169. AZStd::array<T, 4> GetAsArray() const
  170. {
  171. AZStd::array<T, 4> primitiveArray = { { r, g, b, a } };
  172. return primitiveArray;
  173. }
  174. };
  175. //////////////////////////////////////////////////////////////////////////////////////////////
  176. // template specialization
  177. ///////////////////////////////////////////////
  178. template<>
  179. ILINE Color_tpl<f32>::Color_tpl(f32 _x, f32 _y, f32 _z, f32 _w)
  180. {
  181. r = _x;
  182. g = _y;
  183. b = _z;
  184. a = _w;
  185. }
  186. template<>
  187. ILINE Color_tpl<f32>::Color_tpl(f32 _x, f32 _y, f32 _z)
  188. {
  189. r = _x;
  190. g = _y;
  191. b = _z;
  192. a = 1.f;
  193. }
  194. template<>
  195. ILINE Color_tpl<uint8>::Color_tpl(uint8 _x, uint8 _y, uint8 _z, uint8 _w)
  196. {
  197. r = _x;
  198. g = _y;
  199. b = _z;
  200. a = _w;
  201. }
  202. template<>
  203. ILINE Color_tpl<uint8>::Color_tpl(uint8 _x, uint8 _y, uint8 _z)
  204. {
  205. r = _x;
  206. g = _y;
  207. b = _z;
  208. a = 255;
  209. }
  210. //-----------------------------------------------------------------------------
  211. template<>
  212. ILINE Color_tpl<f32>::Color_tpl(const unsigned int abgr)
  213. {
  214. r = (abgr & 0xff) / 255.0f;
  215. g = ((abgr >> 8) & 0xff) / 255.0f;
  216. b = ((abgr >> 16) & 0xff) / 255.0f;
  217. a = ((abgr >> 24) & 0xff) / 255.0f;
  218. }
  219. template<>
  220. ILINE Color_tpl<uint8>::Color_tpl(const unsigned int c)
  221. {
  222. *(unsigned int*)(&r) = c;
  223. } //use this with RGBA8 macro!
  224. //-----------------------------------------------------------------------------
  225. template<>
  226. ILINE Color_tpl<f32>::Color_tpl(const float c)
  227. {
  228. r = c;
  229. g = c;
  230. b = c;
  231. a = c;
  232. }
  233. template<>
  234. ILINE Color_tpl<uint8>::Color_tpl(const float c)
  235. {
  236. r = (uint8)(c * 255);
  237. g = (uint8)(c * 255);
  238. b = (uint8)(c * 255);
  239. a = (uint8)(c * 255);
  240. }
  241. //-----------------------------------------------------------------------------
  242. template<>
  243. ILINE Color_tpl<f32>::Color_tpl(const ColorF& c)
  244. {
  245. r = c.r;
  246. g = c.g;
  247. b = c.b;
  248. a = c.a;
  249. }
  250. template<>
  251. ILINE Color_tpl<uint8>::Color_tpl(const ColorF& c)
  252. {
  253. r = (uint8)(c.r * 255);
  254. g = (uint8)(c.g * 255);
  255. b = (uint8)(c.b * 255);
  256. a = (uint8)(c.a * 255);
  257. }
  258. template<>
  259. ILINE Color_tpl<f32>::Color_tpl(const ColorF& c, float fAlpha)
  260. {
  261. r = c.r;
  262. g = c.g;
  263. b = c.b;
  264. a = fAlpha;
  265. }
  266. template<>
  267. ILINE Color_tpl<f32>::Color_tpl(const Vec3& c, float fAlpha)
  268. {
  269. r = c.x;
  270. g = c.y;
  271. b = c.z;
  272. a = fAlpha;
  273. }
  274. template<>
  275. ILINE Color_tpl<f32>::Color_tpl(const Vec4& c)
  276. {
  277. r = c.x;
  278. g = c.y;
  279. b = c.z;
  280. a = c.w;
  281. }
  282. template<>
  283. ILINE Color_tpl<uint8>::Color_tpl(const ColorF& c, float fAlpha)
  284. {
  285. r = (uint8)(c.r * 255);
  286. g = (uint8)(c.g * 255);
  287. b = (uint8)(c.b * 255);
  288. a = (uint8)(fAlpha * 255);
  289. }
  290. template<>
  291. ILINE Color_tpl<uint8>::Color_tpl(const Vec3& c, float fAlpha)
  292. {
  293. r = (uint8)(c.x * 255);
  294. g = (uint8)(c.y * 255);
  295. b = (uint8)(c.z * 255);
  296. a = (uint8)(fAlpha * 255);
  297. }
  298. template<>
  299. ILINE Color_tpl<uint8>::Color_tpl(const Vec4& c)
  300. {
  301. r = (uint8)(c.x * 255);
  302. g = (uint8)(c.y * 255);
  303. b = (uint8)(c.z * 255);
  304. a = (uint8)(c.w * 255);
  305. }
  306. //////////////////////////////////////////////////////////////////////////////////////////////
  307. // functions implementation
  308. ///////////////////////////////////////////////
  309. ///////////////////////////////////////////////
  310. ///////////////////////////////////////////////
  311. template <class T>
  312. ILINE Color_tpl<T> operator * (T s, const Color_tpl<T>& v)
  313. {
  314. return Color_tpl<T>(v.r * s, v.g * s, v.b * s, v.a * s);
  315. }
  316. ///////////////////////////////////////////////
  317. template <class T>
  318. ILINE unsigned int Color_tpl<T>::pack_abgr8888() const
  319. {
  320. unsigned char cr;
  321. unsigned char cg;
  322. unsigned char cb;
  323. unsigned char ca;
  324. if constexpr (sizeof(r) == 1) // char and unsigned char
  325. {
  326. cr = (unsigned char)r;
  327. cg = (unsigned char)g;
  328. cb = (unsigned char)b;
  329. ca = (unsigned char)a;
  330. }
  331. else if constexpr (sizeof(r) == 2) // short and unsigned short
  332. {
  333. cr = (unsigned short)(r) >> 8;
  334. cg = (unsigned short)(g) >> 8;
  335. cb = (unsigned short)(b) >> 8;
  336. ca = (unsigned short)(a) >> 8;
  337. }
  338. else // float or double
  339. {
  340. cr = (unsigned char)(r * 255.0f);
  341. cg = (unsigned char)(g * 255.0f);
  342. cb = (unsigned char)(b * 255.0f);
  343. ca = (unsigned char)(a * 255.0f);
  344. }
  345. return (ca << 24) | (cb << 16) | (cg << 8) | cr;
  346. }
  347. ///////////////////////////////////////////////
  348. template <class T>
  349. ILINE unsigned int Color_tpl<T>::pack_argb8888() const
  350. {
  351. unsigned char cr;
  352. unsigned char cg;
  353. unsigned char cb;
  354. unsigned char ca;
  355. if constexpr (sizeof(r) == 1) // char and unsigned char
  356. {
  357. cr = (unsigned char)r;
  358. cg = (unsigned char)g;
  359. cb = (unsigned char)b;
  360. ca = (unsigned char)a;
  361. }
  362. else if constexpr (sizeof(r) == 2) // short and unsigned short
  363. {
  364. cr = (unsigned short)(r) >> 8;
  365. cg = (unsigned short)(g) >> 8;
  366. cb = (unsigned short)(b) >> 8;
  367. ca = (unsigned short)(a) >> 8;
  368. }
  369. else // float or double
  370. {
  371. cr = (unsigned char)(r * 255.0f);
  372. cg = (unsigned char)(g * 255.0f);
  373. cb = (unsigned char)(b * 255.0f);
  374. ca = (unsigned char)(a * 255.0f);
  375. }
  376. return (ca << 24) | (cr << 16) | (cg << 8) | cb;
  377. }
  378. ///////////////////////////////////////////////
  379. template <class T>
  380. inline void Color_tpl<T>::clamp(T bottom, T top)
  381. {
  382. r = min(top, max(bottom, r));
  383. g = min(top, max(bottom, g));
  384. b = min(top, max(bottom, b));
  385. a = min(top, max(bottom, a));
  386. }
  387. #define Col_TrackviewDefault ColorF (0.187820792f, 0.187820792f, 1.0f)
  388. #define Clr_Empty ColorF(0.0f, 0.0f, 0.0f, 1.0f)