SColor.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "irrTypes.h"
  6. #include "irrMath.h"
  7. namespace irr
  8. {
  9. namespace video
  10. {
  11. //! An enum for the color format of textures used by the Irrlicht Engine.
  12. /** A color format specifies how color information is stored.
  13. NOTE: Byte order in memory is usually flipped (it's probably correct in bitmap files, but flipped on reading).
  14. So for example ECF_A8R8G8B8 is BGRA in memory same as in DX9's D3DFMT_A8R8G8B8 format.
  15. */
  16. enum ECOLOR_FORMAT
  17. {
  18. //! 16 bit color format used by the software driver.
  19. /** It is thus preferred by all other irrlicht engine video drivers.
  20. There are 5 bits for every color component, and a single bit is left
  21. for alpha information. */
  22. ECF_A1R5G5B5 = 0,
  23. //! Standard 16 bit color format.
  24. ECF_R5G6B5,
  25. //! 24 bit color, no alpha channel, but 8 bit for red, green and blue.
  26. //! Warning: 24 bit formats tend to be badly supported. Depending on driver it's usually converted to another
  27. // format or even not working at all. It's mostly better to use 16-bit or 32-bit formats.
  28. ECF_R8G8B8,
  29. //! Default 32 bit color format. 8 bits are used for every component: red, green, blue and alpha.
  30. //! Warning: This tends to be BGRA in memory (it's ARGB on file, but with usual big-endian memory it's flipped)
  31. ECF_A8R8G8B8,
  32. /** The following formats may only be used for render target textures. */
  33. /** Floating point formats. */
  34. //! 16 bit format using 16 bits for the red channel.
  35. ECF_R16F,
  36. //! 32 bit format using 16 bits for the red and green channels.
  37. ECF_G16R16F,
  38. //! 64 bit format using 16 bits for the red, green, blue and alpha channels.
  39. ECF_A16B16G16R16F,
  40. //! 32 bit format using 32 bits for the red channel.
  41. ECF_R32F,
  42. //! 64 bit format using 32 bits for the red and green channels.
  43. ECF_G32R32F,
  44. //! 128 bit format using 32 bits for the red, green, blue and alpha channels.
  45. ECF_A32B32G32R32F,
  46. /** Unsigned normalized integer formats. */
  47. //! 8 bit format using 8 bits for the red channel.
  48. ECF_R8,
  49. //! 16 bit format using 8 bits for the red and green channels.
  50. ECF_R8G8,
  51. //! 16 bit format using 16 bits for the red channel.
  52. ECF_R16,
  53. //! 32 bit format using 16 bits for the red and green channels.
  54. ECF_R16G16,
  55. /** Depth and stencil formats. */
  56. //! 16 bit format using 16 bits for depth.
  57. ECF_D16,
  58. //! 32 bit format using 32 bits for depth.
  59. ECF_D32,
  60. //! 32 bit format using 24 bits for depth and 8 bits for stencil.
  61. ECF_D24S8,
  62. //! Unknown color format:
  63. ECF_UNKNOWN
  64. };
  65. //! Names for ECOLOR_FORMAT types
  66. const c8 *const ColorFormatNames[ECF_UNKNOWN + 2] = {
  67. "A1R5G5B5",
  68. "R5G6B5",
  69. "R8G8B8",
  70. "A8R8G8B8",
  71. "R16F",
  72. "G16R16F",
  73. "A16B16G16R16F",
  74. "R32F",
  75. "G32R32F",
  76. "A32B32G32R32F",
  77. "R8",
  78. "R8G8",
  79. "R16",
  80. "R16G16",
  81. "D16",
  82. "D32",
  83. "D24S8",
  84. "UNKNOWN",
  85. 0,
  86. };
  87. //! Creates a 16 bit A1R5G5B5 color
  88. inline u16 RGBA16(u32 r, u32 g, u32 b, u32 a = 0xFF)
  89. {
  90. return (u16)((a & 0x80) << 8 |
  91. (r & 0xF8) << 7 |
  92. (g & 0xF8) << 2 |
  93. (b & 0xF8) >> 3);
  94. }
  95. //! Creates a 16 bit A1R5G5B5 color
  96. inline u16 RGB16(u32 r, u32 g, u32 b)
  97. {
  98. return RGBA16(r, g, b);
  99. }
  100. //! Creates a 16bit A1R5G5B5 color, based on 16bit input values
  101. inline u16 RGB16from16(u16 r, u16 g, u16 b)
  102. {
  103. return (0x8000 |
  104. (r & 0x1F) << 10 |
  105. (g & 0x1F) << 5 |
  106. (b & 0x1F));
  107. }
  108. //! Converts a 32bit (X8R8G8B8) color to a 16bit A1R5G5B5 color
  109. inline u16 X8R8G8B8toA1R5G5B5(u32 color)
  110. {
  111. return (u16)(0x8000 |
  112. (color & 0x00F80000) >> 9 |
  113. (color & 0x0000F800) >> 6 |
  114. (color & 0x000000F8) >> 3);
  115. }
  116. //! Converts a 32bit (A8R8G8B8) color to a 16bit A1R5G5B5 color
  117. inline u16 A8R8G8B8toA1R5G5B5(u32 color)
  118. {
  119. return (u16)((color & 0x80000000) >> 16 |
  120. (color & 0x00F80000) >> 9 |
  121. (color & 0x0000F800) >> 6 |
  122. (color & 0x000000F8) >> 3);
  123. }
  124. //! Converts a 32bit (A8R8G8B8) color to a 16bit R5G6B5 color
  125. inline u16 A8R8G8B8toR5G6B5(u32 color)
  126. {
  127. return (u16)((color & 0x00F80000) >> 8 |
  128. (color & 0x0000FC00) >> 5 |
  129. (color & 0x000000F8) >> 3);
  130. }
  131. //! Convert A8R8G8B8 Color from A1R5G5B5 color
  132. /** build a nicer 32bit Color by extending dest lower bits with source high bits. */
  133. inline u32 A1R5G5B5toA8R8G8B8(u16 color)
  134. {
  135. return (((-((s32)color & 0x00008000) >> (s32)31) & 0xFF000000) |
  136. ((color & 0x00007C00) << 9) | ((color & 0x00007000) << 4) |
  137. ((color & 0x000003E0) << 6) | ((color & 0x00000380) << 1) |
  138. ((color & 0x0000001F) << 3) | ((color & 0x0000001C) >> 2));
  139. }
  140. //! Returns A8R8G8B8 Color from R5G6B5 color
  141. inline u32 R5G6B5toA8R8G8B8(u16 color)
  142. {
  143. return 0xFF000000 |
  144. ((color & 0xF800) << 8) |
  145. ((color & 0x07E0) << 5) |
  146. ((color & 0x001F) << 3);
  147. }
  148. //! Returns A1R5G5B5 Color from R5G6B5 color
  149. inline u16 R5G6B5toA1R5G5B5(u16 color)
  150. {
  151. return 0x8000 | (((color & 0xFFC0) >> 1) | (color & 0x1F));
  152. }
  153. //! Returns R5G6B5 Color from A1R5G5B5 color
  154. inline u16 A1R5G5B5toR5G6B5(u16 color)
  155. {
  156. return (((color & 0x7FE0) << 1) | (color & 0x1F));
  157. }
  158. //! Returns the alpha component from A1R5G5B5 color
  159. /** In Irrlicht, alpha refers to opacity.
  160. \return The alpha value of the color. 0 is transparent, 1 is opaque. */
  161. inline u32 getAlpha(u16 color)
  162. {
  163. return ((color >> 15) & 0x1);
  164. }
  165. //! Returns the red component from A1R5G5B5 color.
  166. /** Shift left by 3 to get 8 bit value. */
  167. inline u32 getRed(u16 color)
  168. {
  169. return ((color >> 10) & 0x1F);
  170. }
  171. //! Returns the green component from A1R5G5B5 color
  172. /** Shift left by 3 to get 8 bit value. */
  173. inline u32 getGreen(u16 color)
  174. {
  175. return ((color >> 5) & 0x1F);
  176. }
  177. //! Returns the blue component from A1R5G5B5 color
  178. /** Shift left by 3 to get 8 bit value. */
  179. inline u32 getBlue(u16 color)
  180. {
  181. return (color & 0x1F);
  182. }
  183. //! Returns the average from a 16 bit A1R5G5B5 color
  184. inline s32 getAverage(s16 color)
  185. {
  186. return ((getRed(color) << 3) + (getGreen(color) << 3) + (getBlue(color) << 3)) / 3;
  187. }
  188. //! Class representing a 32 bit ARGB color.
  189. /** The color values for alpha, red, green, and blue are
  190. stored in a single u32. So all four values may be between 0 and 255.
  191. Alpha in Irrlicht is opacity, so 0 is fully transparent, 255 is fully opaque (solid).
  192. This class is used by most parts of the Irrlicht Engine
  193. to specify a color. Another way is using the class SColorf, which
  194. stores the color values in 4 floats.
  195. This class must consist of only one u32 and must not use virtual functions.
  196. */
  197. class SColor
  198. {
  199. public:
  200. //! Constructor of the Color. Does nothing.
  201. /** The color value is not initialized to save time. */
  202. SColor() {}
  203. //! Constructs the color from 4 values representing the alpha, red, green and blue component.
  204. /** Must be values between 0 and 255. */
  205. constexpr SColor(u32 a, u32 r, u32 g, u32 b) :
  206. color(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)) {}
  207. //! Constructs the color from a 32 bit value. Could be another color.
  208. constexpr SColor(u32 clr) :
  209. color(clr) {}
  210. //! Returns the alpha component of the color.
  211. /** The alpha component defines how opaque a color is.
  212. \return The alpha value of the color. 0 is fully transparent, 255 is fully opaque. */
  213. u32 getAlpha() const { return color >> 24; }
  214. //! Returns the red component of the color.
  215. /** \return Value between 0 and 255, specifying how red the color is.
  216. 0 means no red, 255 means full red. */
  217. u32 getRed() const { return (color >> 16) & 0xff; }
  218. //! Returns the green component of the color.
  219. /** \return Value between 0 and 255, specifying how green the color is.
  220. 0 means no green, 255 means full green. */
  221. u32 getGreen() const { return (color >> 8) & 0xff; }
  222. //! Returns the blue component of the color.
  223. /** \return Value between 0 and 255, specifying how blue the color is.
  224. 0 means no blue, 255 means full blue. */
  225. u32 getBlue() const { return color & 0xff; }
  226. //! Get lightness of the color in the range [0,255]
  227. f32 getLightness() const
  228. {
  229. return 0.5f * (core::max_(core::max_(getRed(), getGreen()), getBlue()) + core::min_(core::min_(getRed(), getGreen()), getBlue()));
  230. }
  231. //! Get luminance of the color in the range [0,255].
  232. f32 getLuminance() const
  233. {
  234. return 0.3f * getRed() + 0.59f * getGreen() + 0.11f * getBlue();
  235. }
  236. //! Get average intensity of the color in the range [0,255].
  237. u32 getAverage() const
  238. {
  239. return (getRed() + getGreen() + getBlue()) / 3;
  240. }
  241. //! Sets the alpha component of the Color.
  242. /** The alpha component defines how transparent a color should be.
  243. \param a The alpha value of the color. 0 is fully transparent, 255 is fully opaque. */
  244. void setAlpha(u32 a) { color = ((a & 0xff) << 24) | (color & 0x00ffffff); }
  245. //! Sets the red component of the Color.
  246. /** \param r: Has to be a value between 0 and 255.
  247. 0 means no red, 255 means full red. */
  248. void setRed(u32 r) { color = ((r & 0xff) << 16) | (color & 0xff00ffff); }
  249. //! Sets the green component of the Color.
  250. /** \param g: Has to be a value between 0 and 255.
  251. 0 means no green, 255 means full green. */
  252. void setGreen(u32 g) { color = ((g & 0xff) << 8) | (color & 0xffff00ff); }
  253. //! Sets the blue component of the Color.
  254. /** \param b: Has to be a value between 0 and 255.
  255. 0 means no blue, 255 means full blue. */
  256. void setBlue(u32 b) { color = (b & 0xff) | (color & 0xffffff00); }
  257. //! Calculates a 16 bit A1R5G5B5 value of this color.
  258. /** \return 16 bit A1R5G5B5 value of this color. */
  259. u16 toA1R5G5B5() const { return A8R8G8B8toA1R5G5B5(color); }
  260. //! Converts color to OpenGL color format
  261. /** From ARGB to RGBA in 4 byte components for endian aware
  262. passing to OpenGL
  263. \param dest: address where the 4x8 bit OpenGL color is stored. */
  264. void toOpenGLColor(u8 *dest) const
  265. {
  266. *dest = (u8)getRed();
  267. *++dest = (u8)getGreen();
  268. *++dest = (u8)getBlue();
  269. *++dest = (u8)getAlpha();
  270. }
  271. //! Sets all four components of the color at once.
  272. /** Constructs the color from 4 values representing the alpha,
  273. red, green and blue components of the color. Must be values
  274. between 0 and 255.
  275. \param a: Alpha component of the color. The alpha component
  276. defines how transparent a color should be. Has to be a value
  277. between 0 and 255. 255 means not transparent (opaque), 0 means
  278. fully transparent.
  279. \param r: Sets the red component of the Color. Has to be a
  280. value between 0 and 255. 0 means no red, 255 means full red.
  281. \param g: Sets the green component of the Color. Has to be a
  282. value between 0 and 255. 0 means no green, 255 means full
  283. green.
  284. \param b: Sets the blue component of the Color. Has to be a
  285. value between 0 and 255. 0 means no blue, 255 means full blue. */
  286. void set(u32 a, u32 r, u32 g, u32 b)
  287. {
  288. color = (((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff));
  289. }
  290. void set(u32 col) { color = col; }
  291. //! Compares the color to another color.
  292. /** \return True if the colors are the same, and false if not. */
  293. bool operator==(const SColor &other) const { return other.color == color; }
  294. //! Compares the color to another color.
  295. /** \return True if the colors are different, and false if they are the same. */
  296. bool operator!=(const SColor &other) const { return other.color != color; }
  297. //! comparison operator
  298. /** \return True if this color is smaller than the other one */
  299. bool operator<(const SColor &other) const { return (color < other.color); }
  300. //! Adds two colors, result is clamped to 0..255 values
  301. /** \param other Color to add to this color
  302. \return Addition of the two colors, clamped to 0..255 values */
  303. SColor operator+(const SColor &other) const
  304. {
  305. return SColor(core::min_(getAlpha() + other.getAlpha(), 255u),
  306. core::min_(getRed() + other.getRed(), 255u),
  307. core::min_(getGreen() + other.getGreen(), 255u),
  308. core::min_(getBlue() + other.getBlue(), 255u));
  309. }
  310. //! Interpolates the color with a f32 value to another color
  311. /** \param other: Other color
  312. \param d: value between 0.0f and 1.0f. d=0 returns other, d=1 returns this, values between interpolate.
  313. \return Interpolated color. */
  314. SColor getInterpolated(const SColor &other, f32 d) const
  315. {
  316. d = core::clamp(d, 0.f, 1.f);
  317. const f32 inv = 1.0f - d;
  318. return SColor((u32)core::round32(other.getAlpha() * inv + getAlpha() * d),
  319. (u32)core::round32(other.getRed() * inv + getRed() * d),
  320. (u32)core::round32(other.getGreen() * inv + getGreen() * d),
  321. (u32)core::round32(other.getBlue() * inv + getBlue() * d));
  322. }
  323. //! Returns interpolated color. ( quadratic )
  324. /** \param c1: first color to interpolate with
  325. \param c2: second color to interpolate with
  326. \param d: value between 0.0f and 1.0f. */
  327. SColor getInterpolated_quadratic(const SColor &c1, const SColor &c2, f32 d) const
  328. {
  329. // this*(1-d)*(1-d) + 2 * c1 * (1-d) + c2 * d * d;
  330. d = core::clamp(d, 0.f, 1.f);
  331. const f32 inv = 1.f - d;
  332. const f32 mul0 = inv * inv;
  333. const f32 mul1 = 2.f * d * inv;
  334. const f32 mul2 = d * d;
  335. return SColor(
  336. core::clamp(core::floor32(
  337. getAlpha() * mul0 + c1.getAlpha() * mul1 + c2.getAlpha() * mul2),
  338. 0, 255),
  339. core::clamp(core::floor32(
  340. getRed() * mul0 + c1.getRed() * mul1 + c2.getRed() * mul2),
  341. 0, 255),
  342. core::clamp(core::floor32(
  343. getGreen() * mul0 + c1.getGreen() * mul1 + c2.getGreen() * mul2),
  344. 0, 255),
  345. core::clamp(core::floor32(
  346. getBlue() * mul0 + c1.getBlue() * mul1 + c2.getBlue() * mul2),
  347. 0, 255));
  348. }
  349. //! set the color by expecting data in the given format
  350. /** \param data: must point to valid memory containing color information in the given format
  351. \param format: tells the format in which data is available
  352. */
  353. void setData(const void *data, ECOLOR_FORMAT format)
  354. {
  355. switch (format) {
  356. case ECF_A1R5G5B5:
  357. color = A1R5G5B5toA8R8G8B8(*(u16 *)data);
  358. break;
  359. case ECF_R5G6B5:
  360. color = R5G6B5toA8R8G8B8(*(u16 *)data);
  361. break;
  362. case ECF_A8R8G8B8:
  363. color = *(u32 *)data;
  364. break;
  365. case ECF_R8G8B8: {
  366. const u8 *p = (u8 *)data;
  367. set(255, p[0], p[1], p[2]);
  368. } break;
  369. default:
  370. color = 0xffffffff;
  371. break;
  372. }
  373. }
  374. //! Write the color to data in the defined format
  375. /** \param data: target to write the color. Must contain sufficiently large memory to receive the number of bytes neede for format
  376. \param format: tells the format used to write the color into data
  377. */
  378. void getData(void *data, ECOLOR_FORMAT format) const
  379. {
  380. switch (format) {
  381. case ECF_A1R5G5B5: {
  382. u16 *dest = (u16 *)data;
  383. *dest = video::A8R8G8B8toA1R5G5B5(color);
  384. } break;
  385. case ECF_R5G6B5: {
  386. u16 *dest = (u16 *)data;
  387. *dest = video::A8R8G8B8toR5G6B5(color);
  388. } break;
  389. case ECF_R8G8B8: {
  390. u8 *dest = (u8 *)data;
  391. dest[0] = (u8)getRed();
  392. dest[1] = (u8)getGreen();
  393. dest[2] = (u8)getBlue();
  394. } break;
  395. case ECF_A8R8G8B8: {
  396. u32 *dest = (u32 *)data;
  397. *dest = color;
  398. } break;
  399. default:
  400. break;
  401. }
  402. }
  403. //! color in A8R8G8B8 Format
  404. u32 color;
  405. };
  406. //! Class representing a color with four floats.
  407. /** The color values for red, green, blue
  408. and alpha are each stored in a 32 bit floating point variable.
  409. So all four values may be between 0.0f and 1.0f.
  410. Another, faster way to define colors is using the class SColor, which
  411. stores the color values in a single 32 bit integer.
  412. */
  413. class SColorf
  414. {
  415. public:
  416. //! Default constructor for SColorf.
  417. /** Sets red, green and blue to 0.0f and alpha to 1.0f. */
  418. SColorf() :
  419. r(0.0f), g(0.0f), b(0.0f), a(1.0f) {}
  420. //! Constructs a color from up to four color values: red, green, blue, and alpha.
  421. /** \param r: Red color component. Should be a value between
  422. 0.0f meaning no red and 1.0f, meaning full red.
  423. \param g: Green color component. Should be a value between 0.0f
  424. meaning no green and 1.0f, meaning full green.
  425. \param b: Blue color component. Should be a value between 0.0f
  426. meaning no blue and 1.0f, meaning full blue.
  427. \param a: Alpha color component of the color. The alpha
  428. component defines how transparent a color should be. Has to be
  429. a value between 0.0f and 1.0f, 1.0f means not transparent
  430. (opaque), 0.0f means fully transparent. */
  431. SColorf(f32 r, f32 g, f32 b, f32 a = 1.0f) :
  432. r(r), g(g), b(b), a(a) {}
  433. //! Constructs a color from 32 bit Color.
  434. /** \param c: 32 bit color from which this SColorf class is
  435. constructed from. */
  436. SColorf(SColor c)
  437. {
  438. const f32 inv = 1.0f / 255.0f;
  439. r = c.getRed() * inv;
  440. g = c.getGreen() * inv;
  441. b = c.getBlue() * inv;
  442. a = c.getAlpha() * inv;
  443. }
  444. //! Converts this color to a SColor without floats.
  445. SColor toSColor() const
  446. {
  447. return SColor((u32)core::round32(a * 255.0f), (u32)core::round32(r * 255.0f), (u32)core::round32(g * 255.0f), (u32)core::round32(b * 255.0f));
  448. }
  449. //! Sets three color components to new values at once.
  450. /** \param rr: Red color component. Should be a value between 0.0f meaning
  451. no red (=black) and 1.0f, meaning full red.
  452. \param gg: Green color component. Should be a value between 0.0f meaning
  453. no green (=black) and 1.0f, meaning full green.
  454. \param bb: Blue color component. Should be a value between 0.0f meaning
  455. no blue (=black) and 1.0f, meaning full blue. */
  456. void set(f32 rr, f32 gg, f32 bb)
  457. {
  458. r = rr;
  459. g = gg;
  460. b = bb;
  461. }
  462. //! Sets all four color components to new values at once.
  463. /** \param aa: Alpha component. Should be a value between 0.0f meaning
  464. fully transparent and 1.0f, meaning opaque.
  465. \param rr: Red color component. Should be a value between 0.0f meaning
  466. no red and 1.0f, meaning full red.
  467. \param gg: Green color component. Should be a value between 0.0f meaning
  468. no green and 1.0f, meaning full green.
  469. \param bb: Blue color component. Should be a value between 0.0f meaning
  470. no blue and 1.0f, meaning full blue. */
  471. void set(f32 aa, f32 rr, f32 gg, f32 bb)
  472. {
  473. a = aa;
  474. r = rr;
  475. g = gg;
  476. b = bb;
  477. }
  478. //! Interpolates the color with a f32 value to another color
  479. /** \param other: Other color
  480. \param d: value between 0.0f and 1.0f
  481. \return Interpolated color. */
  482. SColorf getInterpolated(const SColorf &other, f32 d) const
  483. {
  484. d = core::clamp(d, 0.f, 1.f);
  485. const f32 inv = 1.0f - d;
  486. return SColorf(other.r * inv + r * d,
  487. other.g * inv + g * d, other.b * inv + b * d, other.a * inv + a * d);
  488. }
  489. //! Returns interpolated color. ( quadratic )
  490. /** \param c1: first color to interpolate with
  491. \param c2: second color to interpolate with
  492. \param d: value between 0.0f and 1.0f. */
  493. inline SColorf getInterpolated_quadratic(const SColorf &c1, const SColorf &c2,
  494. f32 d) const
  495. {
  496. d = core::clamp(d, 0.f, 1.f);
  497. // this*(1-d)*(1-d) + 2 * c1 * (1-d) + c2 * d * d;
  498. const f32 inv = 1.f - d;
  499. const f32 mul0 = inv * inv;
  500. const f32 mul1 = 2.f * d * inv;
  501. const f32 mul2 = d * d;
  502. return SColorf(r * mul0 + c1.r * mul1 + c2.r * mul2,
  503. g * mul0 + c1.g * mul1 + c2.g * mul2,
  504. b * mul0 + c1.b * mul1 + c2.b * mul2,
  505. a * mul0 + c1.a * mul1 + c2.a * mul2);
  506. }
  507. //! Sets a color component by index. R=0, G=1, B=2, A=3
  508. void setColorComponentValue(s32 index, f32 value)
  509. {
  510. switch (index) {
  511. case 0:
  512. r = value;
  513. break;
  514. case 1:
  515. g = value;
  516. break;
  517. case 2:
  518. b = value;
  519. break;
  520. case 3:
  521. a = value;
  522. break;
  523. }
  524. }
  525. //! Returns the alpha component of the color in the range 0.0 (transparent) to 1.0 (opaque)
  526. f32 getAlpha() const { return a; }
  527. //! Returns the red component of the color in the range 0.0 to 1.0
  528. f32 getRed() const { return r; }
  529. //! Returns the green component of the color in the range 0.0 to 1.0
  530. f32 getGreen() const { return g; }
  531. //! Returns the blue component of the color in the range 0.0 to 1.0
  532. f32 getBlue() const { return b; }
  533. //! red color component
  534. f32 r;
  535. //! green color component
  536. f32 g;
  537. //! blue component
  538. f32 b;
  539. //! alpha color component
  540. f32 a;
  541. };
  542. //! Class representing a color in HSL format
  543. /** The color values for hue, saturation, luminance
  544. are stored in 32bit floating point variables. Hue is in range [0,360],
  545. Luminance and Saturation are in percent [0,100]
  546. */
  547. class SColorHSL
  548. {
  549. public:
  550. constexpr SColorHSL(f32 h = 0.f, f32 s = 0.f, f32 l = 0.f) :
  551. Hue(h), Saturation(s), Luminance(l) {}
  552. void fromRGB(const SColorf &color);
  553. void toRGB(SColorf &color) const;
  554. f32 Hue;
  555. f32 Saturation;
  556. f32 Luminance;
  557. private:
  558. inline f32 toRGB1(f32 rm1, f32 rm2, f32 rh) const;
  559. };
  560. inline void SColorHSL::fromRGB(const SColorf &color)
  561. {
  562. const f32 maxVal = core::max_(color.getRed(), color.getGreen(), color.getBlue());
  563. const f32 minVal = (f32)core::min_(color.getRed(), color.getGreen(), color.getBlue());
  564. Luminance = (maxVal + minVal) * 50;
  565. if (core::equals(maxVal, minVal)) {
  566. Hue = 0.f;
  567. Saturation = 0.f;
  568. return;
  569. }
  570. const f32 delta = maxVal - minVal;
  571. if (Luminance <= 50) {
  572. Saturation = (delta) / (maxVal + minVal);
  573. } else {
  574. Saturation = (delta) / (2 - maxVal - minVal);
  575. }
  576. Saturation *= 100;
  577. if (core::equals(maxVal, color.getRed()))
  578. Hue = (color.getGreen() - color.getBlue()) / delta;
  579. else if (core::equals(maxVal, color.getGreen()))
  580. Hue = 2 + ((color.getBlue() - color.getRed()) / delta);
  581. else // blue is max
  582. Hue = 4 + ((color.getRed() - color.getGreen()) / delta);
  583. Hue *= 60.0f;
  584. while (Hue < 0.f)
  585. Hue += 360;
  586. }
  587. inline void SColorHSL::toRGB(SColorf &color) const
  588. {
  589. const f32 l = Luminance / 100;
  590. if (core::iszero(Saturation)) { // grey
  591. color.set(l, l, l);
  592. return;
  593. }
  594. f32 rm2;
  595. if (Luminance <= 50) {
  596. rm2 = l + l * (Saturation / 100);
  597. } else {
  598. rm2 = l + (1 - l) * (Saturation / 100);
  599. }
  600. const f32 rm1 = 2.0f * l - rm2;
  601. const f32 h = Hue / 360.0f;
  602. color.set(toRGB1(rm1, rm2, h + 1.f / 3.f),
  603. toRGB1(rm1, rm2, h),
  604. toRGB1(rm1, rm2, h - 1.f / 3.f));
  605. }
  606. // algorithm from Foley/Van-Dam
  607. inline f32 SColorHSL::toRGB1(f32 rm1, f32 rm2, f32 rh) const
  608. {
  609. if (rh < 0)
  610. rh += 1;
  611. if (rh > 1)
  612. rh -= 1;
  613. if (rh < 1.f / 6.f)
  614. rm1 = rm1 + (rm2 - rm1) * rh * 6.f;
  615. else if (rh < 0.5f)
  616. rm1 = rm2;
  617. else if (rh < 2.f / 3.f)
  618. rm1 = rm1 + (rm2 - rm1) * ((2.f / 3.f) - rh) * 6.f;
  619. return rm1;
  620. }
  621. } // end namespace video
  622. } // end namespace irr