Cry_Color.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 AZ::Vector3 toVector3() const { return AZ::Vector3(r, g, b); }
  154. inline AZ::Vector4 toVector4() const { return AZ::Vector4(r, g, b, a); }
  155. inline void clamp(T bottom = 0.0f, T top = 1.0f);
  156. void srgb2rgb()
  157. {
  158. for (int i = 0; i < 3; i++)
  159. {
  160. T& c = (*this)[i];
  161. if (c <= 0.040448643f)
  162. {
  163. c = c / 12.92f;
  164. }
  165. else
  166. {
  167. c = pow((c + 0.055f) / 1.055f, 2.4f);
  168. }
  169. }
  170. }
  171. AZStd::array<T, 4> GetAsArray() const
  172. {
  173. AZStd::array<T, 4> primitiveArray = { { r, g, b, a } };
  174. return primitiveArray;
  175. }
  176. };
  177. //////////////////////////////////////////////////////////////////////////////////////////////
  178. // template specialization
  179. ///////////////////////////////////////////////
  180. template<>
  181. ILINE Color_tpl<f32>::Color_tpl(f32 _x, f32 _y, f32 _z, f32 _w)
  182. {
  183. r = _x;
  184. g = _y;
  185. b = _z;
  186. a = _w;
  187. }
  188. template<>
  189. ILINE Color_tpl<f32>::Color_tpl(f32 _x, f32 _y, f32 _z)
  190. {
  191. r = _x;
  192. g = _y;
  193. b = _z;
  194. a = 1.f;
  195. }
  196. template<>
  197. ILINE Color_tpl<uint8>::Color_tpl(uint8 _x, uint8 _y, uint8 _z, uint8 _w)
  198. {
  199. r = _x;
  200. g = _y;
  201. b = _z;
  202. a = _w;
  203. }
  204. template<>
  205. ILINE Color_tpl<uint8>::Color_tpl(uint8 _x, uint8 _y, uint8 _z)
  206. {
  207. r = _x;
  208. g = _y;
  209. b = _z;
  210. a = 255;
  211. }
  212. //-----------------------------------------------------------------------------
  213. template<>
  214. ILINE Color_tpl<f32>::Color_tpl(const unsigned int abgr)
  215. {
  216. r = (abgr & 0xff) / 255.0f;
  217. g = ((abgr >> 8) & 0xff) / 255.0f;
  218. b = ((abgr >> 16) & 0xff) / 255.0f;
  219. a = ((abgr >> 24) & 0xff) / 255.0f;
  220. }
  221. template<>
  222. ILINE Color_tpl<uint8>::Color_tpl(const unsigned int c)
  223. {
  224. *(unsigned int*)(&r) = c;
  225. } //use this with RGBA8 macro!
  226. //-----------------------------------------------------------------------------
  227. template<>
  228. ILINE Color_tpl<f32>::Color_tpl(const float c)
  229. {
  230. r = c;
  231. g = c;
  232. b = c;
  233. a = c;
  234. }
  235. template<>
  236. ILINE Color_tpl<uint8>::Color_tpl(const float c)
  237. {
  238. r = (uint8)(c * 255);
  239. g = (uint8)(c * 255);
  240. b = (uint8)(c * 255);
  241. a = (uint8)(c * 255);
  242. }
  243. //-----------------------------------------------------------------------------
  244. template<>
  245. ILINE Color_tpl<f32>::Color_tpl(const ColorF& c)
  246. {
  247. r = c.r;
  248. g = c.g;
  249. b = c.b;
  250. a = c.a;
  251. }
  252. template<>
  253. ILINE Color_tpl<uint8>::Color_tpl(const ColorF& c)
  254. {
  255. r = (uint8)(c.r * 255);
  256. g = (uint8)(c.g * 255);
  257. b = (uint8)(c.b * 255);
  258. a = (uint8)(c.a * 255);
  259. }
  260. template<>
  261. ILINE Color_tpl<f32>::Color_tpl(const ColorF& c, float fAlpha)
  262. {
  263. r = c.r;
  264. g = c.g;
  265. b = c.b;
  266. a = fAlpha;
  267. }
  268. template<>
  269. ILINE Color_tpl<f32>::Color_tpl(const Vec3& c, float fAlpha)
  270. {
  271. r = c.x;
  272. g = c.y;
  273. b = c.z;
  274. a = fAlpha;
  275. }
  276. template<>
  277. ILINE Color_tpl<f32>::Color_tpl(const Vec4& c)
  278. {
  279. r = c.x;
  280. g = c.y;
  281. b = c.z;
  282. a = c.w;
  283. }
  284. template<>
  285. ILINE Color_tpl<uint8>::Color_tpl(const ColorF& c, float fAlpha)
  286. {
  287. r = (uint8)(c.r * 255);
  288. g = (uint8)(c.g * 255);
  289. b = (uint8)(c.b * 255);
  290. a = (uint8)(fAlpha * 255);
  291. }
  292. template<>
  293. ILINE Color_tpl<uint8>::Color_tpl(const Vec3& c, float fAlpha)
  294. {
  295. r = (uint8)(c.x * 255);
  296. g = (uint8)(c.y * 255);
  297. b = (uint8)(c.z * 255);
  298. a = (uint8)(fAlpha * 255);
  299. }
  300. template<>
  301. ILINE Color_tpl<uint8>::Color_tpl(const Vec4& c)
  302. {
  303. r = (uint8)(c.x * 255);
  304. g = (uint8)(c.y * 255);
  305. b = (uint8)(c.z * 255);
  306. a = (uint8)(c.w * 255);
  307. }
  308. //////////////////////////////////////////////////////////////////////////////////////////////
  309. // functions implementation
  310. ///////////////////////////////////////////////
  311. ///////////////////////////////////////////////
  312. ///////////////////////////////////////////////
  313. template <class T>
  314. ILINE Color_tpl<T> operator * (T s, const Color_tpl<T>& v)
  315. {
  316. return Color_tpl<T>(v.r * s, v.g * s, v.b * s, v.a * s);
  317. }
  318. ///////////////////////////////////////////////
  319. template <class T>
  320. ILINE unsigned int Color_tpl<T>::pack_abgr8888() const
  321. {
  322. unsigned char cr;
  323. unsigned char cg;
  324. unsigned char cb;
  325. unsigned char ca;
  326. if constexpr (sizeof(r) == 1) // char and unsigned char
  327. {
  328. cr = (unsigned char)r;
  329. cg = (unsigned char)g;
  330. cb = (unsigned char)b;
  331. ca = (unsigned char)a;
  332. }
  333. else if constexpr (sizeof(r) == 2) // short and unsigned short
  334. {
  335. cr = (unsigned short)(r) >> 8;
  336. cg = (unsigned short)(g) >> 8;
  337. cb = (unsigned short)(b) >> 8;
  338. ca = (unsigned short)(a) >> 8;
  339. }
  340. else // float or double
  341. {
  342. cr = (unsigned char)(r * 255.0f);
  343. cg = (unsigned char)(g * 255.0f);
  344. cb = (unsigned char)(b * 255.0f);
  345. ca = (unsigned char)(a * 255.0f);
  346. }
  347. return (ca << 24) | (cb << 16) | (cg << 8) | cr;
  348. }
  349. ///////////////////////////////////////////////
  350. template <class T>
  351. ILINE unsigned int Color_tpl<T>::pack_argb8888() const
  352. {
  353. unsigned char cr;
  354. unsigned char cg;
  355. unsigned char cb;
  356. unsigned char ca;
  357. if constexpr (sizeof(r) == 1) // char and unsigned char
  358. {
  359. cr = (unsigned char)r;
  360. cg = (unsigned char)g;
  361. cb = (unsigned char)b;
  362. ca = (unsigned char)a;
  363. }
  364. else if constexpr (sizeof(r) == 2) // short and unsigned short
  365. {
  366. cr = (unsigned short)(r) >> 8;
  367. cg = (unsigned short)(g) >> 8;
  368. cb = (unsigned short)(b) >> 8;
  369. ca = (unsigned short)(a) >> 8;
  370. }
  371. else // float or double
  372. {
  373. cr = (unsigned char)(r * 255.0f);
  374. cg = (unsigned char)(g * 255.0f);
  375. cb = (unsigned char)(b * 255.0f);
  376. ca = (unsigned char)(a * 255.0f);
  377. }
  378. return (ca << 24) | (cr << 16) | (cg << 8) | cb;
  379. }
  380. ///////////////////////////////////////////////
  381. template <class T>
  382. inline void Color_tpl<T>::clamp(T bottom, T top)
  383. {
  384. r = min(top, max(bottom, r));
  385. g = min(top, max(bottom, g));
  386. b = min(top, max(bottom, b));
  387. a = min(top, max(bottom, a));
  388. }
  389. #define Col_TrackviewDefault ColorF (0.187820792f, 0.187820792f, 1.0f)
  390. #define Clr_Empty ColorF(0.0f, 0.0f, 0.0f, 1.0f)