SColor.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. //! 32 bit format using 10 bits for R, G, B and 2 for alpha.
  56. ECF_A2R10G10B10,
  57. /** Depth and stencil formats. */
  58. //! 16 bit format using 16 bits for depth.
  59. ECF_D16,
  60. //! 32 bit(?) format using 24 bits for depth.
  61. ECF_D24,
  62. //! 32 bit format using 32 bits for depth.
  63. ECF_D32,
  64. //! 32 bit format using 24 bits for depth and 8 bits for stencil.
  65. ECF_D24S8,
  66. //! Unknown color format:
  67. ECF_UNKNOWN
  68. };
  69. //! Names for ECOLOR_FORMAT types
  70. const c8 *const ColorFormatNames[] = {
  71. "A1R5G5B5",
  72. "R5G6B5",
  73. "R8G8B8",
  74. "A8R8G8B8",
  75. "R16F",
  76. "G16R16F",
  77. "A16B16G16R16F",
  78. "R32F",
  79. "G32R32F",
  80. "A32B32G32R32F",
  81. "R8",
  82. "R8G8",
  83. "R16",
  84. "R16G16",
  85. "A2R10G10B10",
  86. "D16",
  87. "D24",
  88. "D32",
  89. "D24S8",
  90. "UNKNOWN",
  91. 0,
  92. };
  93. static_assert(sizeof(ColorFormatNames) / sizeof(ColorFormatNames[0])
  94. == ECF_UNKNOWN + 2, "name table size mismatch");
  95. //! Creates a 16 bit A1R5G5B5 color
  96. inline u16 RGBA16(u32 r, u32 g, u32 b, u32 a = 0xFF)
  97. {
  98. return (u16)((a & 0x80) << 8 |
  99. (r & 0xF8) << 7 |
  100. (g & 0xF8) << 2 |
  101. (b & 0xF8) >> 3);
  102. }
  103. //! Creates a 16 bit A1R5G5B5 color
  104. inline u16 RGB16(u32 r, u32 g, u32 b)
  105. {
  106. return RGBA16(r, g, b);
  107. }
  108. //! Creates a 16bit A1R5G5B5 color, based on 16bit input values
  109. inline u16 RGB16from16(u16 r, u16 g, u16 b)
  110. {
  111. return (0x8000 |
  112. (r & 0x1F) << 10 |
  113. (g & 0x1F) << 5 |
  114. (b & 0x1F));
  115. }
  116. //! Converts a 32bit (X8R8G8B8) color to a 16bit A1R5G5B5 color
  117. inline u16 X8R8G8B8toA1R5G5B5(u32 color)
  118. {
  119. return (u16)(0x8000 |
  120. (color & 0x00F80000) >> 9 |
  121. (color & 0x0000F800) >> 6 |
  122. (color & 0x000000F8) >> 3);
  123. }
  124. //! Converts a 32bit (A8R8G8B8) color to a 16bit A1R5G5B5 color
  125. inline u16 A8R8G8B8toA1R5G5B5(u32 color)
  126. {
  127. return (u16)((color & 0x80000000) >> 16 |
  128. (color & 0x00F80000) >> 9 |
  129. (color & 0x0000F800) >> 6 |
  130. (color & 0x000000F8) >> 3);
  131. }
  132. //! Converts a 32bit (A8R8G8B8) color to a 16bit R5G6B5 color
  133. inline u16 A8R8G8B8toR5G6B5(u32 color)
  134. {
  135. return (u16)((color & 0x00F80000) >> 8 |
  136. (color & 0x0000FC00) >> 5 |
  137. (color & 0x000000F8) >> 3);
  138. }
  139. //! Convert A8R8G8B8 Color from A1R5G5B5 color
  140. /** build a nicer 32bit Color by extending dest lower bits with source high bits. */
  141. inline u32 A1R5G5B5toA8R8G8B8(u16 color)
  142. {
  143. return (((-((s32)color & 0x00008000) >> (s32)31) & 0xFF000000) |
  144. ((color & 0x00007C00) << 9) | ((color & 0x00007000) << 4) |
  145. ((color & 0x000003E0) << 6) | ((color & 0x00000380) << 1) |
  146. ((color & 0x0000001F) << 3) | ((color & 0x0000001C) >> 2));
  147. }
  148. //! Returns A8R8G8B8 Color from R5G6B5 color
  149. inline u32 R5G6B5toA8R8G8B8(u16 color)
  150. {
  151. return 0xFF000000 |
  152. ((color & 0xF800) << 8) |
  153. ((color & 0x07E0) << 5) |
  154. ((color & 0x001F) << 3);
  155. }
  156. //! Returns A1R5G5B5 Color from R5G6B5 color
  157. inline u16 R5G6B5toA1R5G5B5(u16 color)
  158. {
  159. return 0x8000 | (((color & 0xFFC0) >> 1) | (color & 0x1F));
  160. }
  161. //! Returns R5G6B5 Color from A1R5G5B5 color
  162. inline u16 A1R5G5B5toR5G6B5(u16 color)
  163. {
  164. return (((color & 0x7FE0) << 1) | (color & 0x1F));
  165. }
  166. //! Returns the alpha component from A1R5G5B5 color
  167. /** In Irrlicht, alpha refers to opacity.
  168. \return The alpha value of the color. 0 is transparent, 1 is opaque. */
  169. inline u32 getAlpha(u16 color)
  170. {
  171. return ((color >> 15) & 0x1);
  172. }
  173. //! Returns the red component from A1R5G5B5 color.
  174. /** Shift left by 3 to get 8 bit value. */
  175. inline u32 getRed(u16 color)
  176. {
  177. return ((color >> 10) & 0x1F);
  178. }
  179. //! Returns the green component from A1R5G5B5 color
  180. /** Shift left by 3 to get 8 bit value. */
  181. inline u32 getGreen(u16 color)
  182. {
  183. return ((color >> 5) & 0x1F);
  184. }
  185. //! Returns the blue component from A1R5G5B5 color
  186. /** Shift left by 3 to get 8 bit value. */
  187. inline u32 getBlue(u16 color)
  188. {
  189. return (color & 0x1F);
  190. }
  191. //! Class representing a 32 bit ARGB color.
  192. /** The color values for alpha, red, green, and blue are
  193. stored in a single u32. So all four values may be between 0 and 255.
  194. Alpha in Irrlicht is opacity, so 0 is fully transparent, 255 is fully opaque (solid).
  195. This class is used by most parts of the Irrlicht Engine
  196. to specify a color. Another way is using the class SColorf, which
  197. stores the color values in 4 floats.
  198. This class must consist of only one u32 and must not use virtual functions.
  199. */
  200. class SColor
  201. {
  202. public:
  203. //! Constructor of the Color. Does nothing.
  204. /** The color value is not initialized to save time. */
  205. SColor() {}
  206. //! Constructs the color from 4 values representing the alpha, red, green and blue component.
  207. /** Must be values between 0 and 255. */
  208. constexpr SColor(u32 a, u32 r, u32 g, u32 b) :
  209. color(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)) {}
  210. //! Constructs the color from a 32 bit value. Could be another color.
  211. constexpr SColor(u32 clr) :
  212. color(clr) {}
  213. //! Returns the alpha component of the color.
  214. /** The alpha component defines how opaque a color is.
  215. \return The alpha value of the color. 0 is fully transparent, 255 is fully opaque. */
  216. u32 getAlpha() const { return color >> 24; }
  217. //! Returns the red component of the color.
  218. /** \return Value between 0 and 255, specifying how red the color is.
  219. 0 means no red, 255 means full red. */
  220. u32 getRed() const { return (color >> 16) & 0xff; }
  221. //! Returns the green component of the color.
  222. /** \return Value between 0 and 255, specifying how green the color is.
  223. 0 means no green, 255 means full green. */
  224. u32 getGreen() const { return (color >> 8) & 0xff; }
  225. //! Returns the blue component of the color.
  226. /** \return Value between 0 and 255, specifying how blue the color is.
  227. 0 means no blue, 255 means full blue. */
  228. u32 getBlue() const { return color & 0xff; }
  229. //! Get an approximate brightness value of the color in the range [0,255]
  230. f32 getBrightness() const
  231. {
  232. return 0.3f * getRed() + 0.59f * getGreen() + 0.11f * getBlue();
  233. }
  234. //! Sets the alpha component of the Color.
  235. /** The alpha component defines how transparent a color should be.
  236. \param a The alpha value of the color. 0 is fully transparent, 255 is fully opaque. */
  237. void setAlpha(u32 a) { color = ((a & 0xff) << 24) | (color & 0x00ffffff); }
  238. //! Sets the red component of the Color.
  239. /** \param r: Has to be a value between 0 and 255.
  240. 0 means no red, 255 means full red. */
  241. void setRed(u32 r) { color = ((r & 0xff) << 16) | (color & 0xff00ffff); }
  242. //! Sets the green component of the Color.
  243. /** \param g: Has to be a value between 0 and 255.
  244. 0 means no green, 255 means full green. */
  245. void setGreen(u32 g) { color = ((g & 0xff) << 8) | (color & 0xffff00ff); }
  246. //! Sets the blue component of the Color.
  247. /** \param b: Has to be a value between 0 and 255.
  248. 0 means no blue, 255 means full blue. */
  249. void setBlue(u32 b) { color = (b & 0xff) | (color & 0xffffff00); }
  250. //! Calculates a 16 bit A1R5G5B5 value of this color.
  251. /** \return 16 bit A1R5G5B5 value of this color. */
  252. u16 toA1R5G5B5() const { return A8R8G8B8toA1R5G5B5(color); }
  253. //! Converts color to OpenGL color format
  254. /** From ARGB to RGBA in 4 byte components for endian aware
  255. passing to OpenGL
  256. \param dest: address where the 4x8 bit OpenGL color is stored. */
  257. void toOpenGLColor(u8 *dest) const
  258. {
  259. *dest = (u8)getRed();
  260. *++dest = (u8)getGreen();
  261. *++dest = (u8)getBlue();
  262. *++dest = (u8)getAlpha();
  263. }
  264. //! Sets all four components of the color at once.
  265. /** Constructs the color from 4 values representing the alpha,
  266. red, green and blue components of the color. Must be values
  267. between 0 and 255.
  268. \param a: Alpha component of the color. The alpha component
  269. defines how transparent a color should be. Has to be a value
  270. between 0 and 255. 255 means not transparent (opaque), 0 means
  271. fully transparent.
  272. \param r: Sets the red component of the Color. Has to be a
  273. value between 0 and 255. 0 means no red, 255 means full red.
  274. \param g: Sets the green component of the Color. Has to be a
  275. value between 0 and 255. 0 means no green, 255 means full
  276. green.
  277. \param b: Sets the blue component of the Color. Has to be a
  278. value between 0 and 255. 0 means no blue, 255 means full blue. */
  279. void set(u32 a, u32 r, u32 g, u32 b)
  280. {
  281. color = (((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff));
  282. }
  283. void set(u32 col) { color = col; }
  284. //! Compares the color to another color.
  285. /** \return True if the colors are the same, and false if not. */
  286. bool operator==(const SColor &other) const { return other.color == color; }
  287. //! Compares the color to another color.
  288. /** \return True if the colors are different, and false if they are the same. */
  289. bool operator!=(const SColor &other) const { return other.color != color; }
  290. //! comparison operator
  291. /** \return True if this color is smaller than the other one */
  292. bool operator<(const SColor &other) const { return (color < other.color); }
  293. //! Adds two colors in a gamma-incorrect way
  294. /** \param other Color to add to this color
  295. \return Sum of the two non-linear colors, clamped to 0..255 values */
  296. SColor operator+(const SColor &other) const
  297. {
  298. return SColor(core::min_(getAlpha() + other.getAlpha(), 255u),
  299. core::min_(getRed() + other.getRed(), 255u),
  300. core::min_(getGreen() + other.getGreen(), 255u),
  301. core::min_(getBlue() + other.getBlue(), 255u));
  302. }
  303. //! Interpolates the color with a f32 value to another color
  304. /** Note that the interpolation is neither physically nor perceptually
  305. linear since it happens directly in the sRGB color space.
  306. \param other: Other color
  307. \param d: value between 0.0f and 1.0f. d=0 returns other, d=1 returns this, values between interpolate.
  308. \return Interpolated color. */
  309. SColor getInterpolated(const SColor &other, f32 d) const
  310. {
  311. d = core::clamp(d, 0.f, 1.f);
  312. const f32 inv = 1.0f - d;
  313. return SColor((u32)core::round32(other.getAlpha() * inv + getAlpha() * d),
  314. (u32)core::round32(other.getRed() * inv + getRed() * d),
  315. (u32)core::round32(other.getGreen() * inv + getGreen() * d),
  316. (u32)core::round32(other.getBlue() * inv + getBlue() * d));
  317. }
  318. //! set the color by expecting data in the given format
  319. /** \param data: must point to valid memory containing color information in the given format
  320. \param format: tells the format in which data is available
  321. */
  322. void setData(const void *data, ECOLOR_FORMAT format)
  323. {
  324. switch (format) {
  325. case ECF_A1R5G5B5:
  326. color = A1R5G5B5toA8R8G8B8(*(u16 *)data);
  327. break;
  328. case ECF_R5G6B5:
  329. color = R5G6B5toA8R8G8B8(*(u16 *)data);
  330. break;
  331. case ECF_A8R8G8B8:
  332. color = *(u32 *)data;
  333. break;
  334. case ECF_R8G8B8: {
  335. const u8 *p = (u8 *)data;
  336. set(255, p[0], p[1], p[2]);
  337. } break;
  338. default:
  339. color = 0xffffffff;
  340. break;
  341. }
  342. }
  343. //! Write the color to data in the defined format
  344. /** \param data: target to write the color. Must contain sufficiently large memory to receive the number of bytes neede for format
  345. \param format: tells the format used to write the color into data
  346. */
  347. void getData(void *data, ECOLOR_FORMAT format) const
  348. {
  349. switch (format) {
  350. case ECF_A1R5G5B5: {
  351. u16 *dest = (u16 *)data;
  352. *dest = video::A8R8G8B8toA1R5G5B5(color);
  353. } break;
  354. case ECF_R5G6B5: {
  355. u16 *dest = (u16 *)data;
  356. *dest = video::A8R8G8B8toR5G6B5(color);
  357. } break;
  358. case ECF_R8G8B8: {
  359. u8 *dest = (u8 *)data;
  360. dest[0] = (u8)getRed();
  361. dest[1] = (u8)getGreen();
  362. dest[2] = (u8)getBlue();
  363. } break;
  364. case ECF_A8R8G8B8: {
  365. u32 *dest = (u32 *)data;
  366. *dest = color;
  367. } break;
  368. default:
  369. break;
  370. }
  371. }
  372. //! color in A8R8G8B8 Format
  373. u32 color;
  374. };
  375. //! Class representing a color with four floats.
  376. /** The color values for red, green, blue
  377. and alpha are each stored in a 32 bit floating point variable.
  378. So all four values may be between 0.0f and 1.0f.
  379. Another, faster way to define colors is using the class SColor, which
  380. stores the color values in a single 32 bit integer.
  381. */
  382. class SColorf
  383. {
  384. public:
  385. //! Default constructor for SColorf.
  386. /** Sets red, green and blue to 0.0f and alpha to 1.0f. */
  387. SColorf() :
  388. r(0.0f), g(0.0f), b(0.0f), a(1.0f) {}
  389. //! Constructs a color from up to four color values: red, green, blue, and alpha.
  390. /** \param r: Red color component. Should be a value between
  391. 0.0f meaning no red and 1.0f, meaning full red.
  392. \param g: Green color component. Should be a value between 0.0f
  393. meaning no green and 1.0f, meaning full green.
  394. \param b: Blue color component. Should be a value between 0.0f
  395. meaning no blue and 1.0f, meaning full blue.
  396. \param a: Alpha color component of the color. The alpha
  397. component defines how transparent a color should be. Has to be
  398. a value between 0.0f and 1.0f, 1.0f means not transparent
  399. (opaque), 0.0f means fully transparent. */
  400. SColorf(f32 r, f32 g, f32 b, f32 a = 1.0f) :
  401. r(r), g(g), b(b), a(a) {}
  402. //! Constructs a color from 32 bit Color without gamma correction
  403. /** \param c: 32 bit color from which this SColorf class is
  404. constructed from. */
  405. SColorf(SColor c)
  406. {
  407. const f32 inv = 1.0f / 255.0f;
  408. r = c.getRed() * inv;
  409. g = c.getGreen() * inv;
  410. b = c.getBlue() * inv;
  411. a = c.getAlpha() * inv;
  412. }
  413. //! Converts this color to a SColor without gamma correction
  414. SColor toSColor() const
  415. {
  416. 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));
  417. }
  418. //! Sets three color components to new values at once.
  419. /** \param rr: Red color component. Should be a value between 0.0f meaning
  420. no red (=black) and 1.0f, meaning full red.
  421. \param gg: Green color component. Should be a value between 0.0f meaning
  422. no green (=black) and 1.0f, meaning full green.
  423. \param bb: Blue color component. Should be a value between 0.0f meaning
  424. no blue (=black) and 1.0f, meaning full blue. */
  425. void set(f32 rr, f32 gg, f32 bb)
  426. {
  427. r = rr;
  428. g = gg;
  429. b = bb;
  430. }
  431. //! Sets all four color components to new values at once.
  432. /** \param aa: Alpha component. Should be a value between 0.0f meaning
  433. fully transparent and 1.0f, meaning opaque.
  434. \param rr: Red color component. Should be a value between 0.0f meaning
  435. no red and 1.0f, meaning full red.
  436. \param gg: Green color component. Should be a value between 0.0f meaning
  437. no green and 1.0f, meaning full green.
  438. \param bb: Blue color component. Should be a value between 0.0f meaning
  439. no blue and 1.0f, meaning full blue. */
  440. void set(f32 aa, f32 rr, f32 gg, f32 bb)
  441. {
  442. a = aa;
  443. r = rr;
  444. g = gg;
  445. b = bb;
  446. }
  447. //! Interpolates the color with a f32 value to another color
  448. /** Note that the interpolation is neither physically nor perceptually
  449. linear if it happens directly in the sRGB color space.
  450. \param other: Other color
  451. \param d: value between 0.0f and 1.0f
  452. \return Interpolated color. */
  453. SColorf getInterpolated(const SColorf &other, f32 d) const
  454. {
  455. d = core::clamp(d, 0.f, 1.f);
  456. const f32 inv = 1.0f - d;
  457. return SColorf(other.r * inv + r * d,
  458. other.g * inv + g * d, other.b * inv + b * d, other.a * inv + a * d);
  459. }
  460. //! Returns the alpha component of the color in the range 0.0 (transparent) to 1.0 (opaque)
  461. f32 getAlpha() const { return a; }
  462. //! Returns the red component of the color in the range 0.0 to 1.0
  463. f32 getRed() const { return r; }
  464. //! Returns the green component of the color in the range 0.0 to 1.0
  465. f32 getGreen() const { return g; }
  466. //! Returns the blue component of the color in the range 0.0 to 1.0
  467. f32 getBlue() const { return b; }
  468. //! red color component
  469. f32 r;
  470. //! green color component
  471. f32 g;
  472. //! blue component
  473. f32 b;
  474. //! alpha color component
  475. f32 a;
  476. };
  477. //! Class representing a color in HSL format
  478. /** The color values for hue, saturation, luminance
  479. are stored in 32bit floating point variables. Hue is in range [0,360],
  480. Luminance and Saturation are in percent [0,100]
  481. */
  482. class SColorHSL
  483. {
  484. public:
  485. constexpr SColorHSL(f32 h = 0.f, f32 s = 0.f, f32 l = 0.f) :
  486. Hue(h), Saturation(s), Luminance(l) {}
  487. void fromRGB(const SColorf &color);
  488. void toRGB(SColorf &color) const;
  489. f32 Hue;
  490. f32 Saturation;
  491. f32 Luminance;
  492. private:
  493. inline f32 toRGB1(f32 rm1, f32 rm2, f32 rh) const;
  494. };
  495. inline void SColorHSL::fromRGB(const SColorf &color)
  496. {
  497. const f32 maxVal = core::max_(color.getRed(), color.getGreen(), color.getBlue());
  498. const f32 minVal = (f32)core::min_(color.getRed(), color.getGreen(), color.getBlue());
  499. Luminance = (maxVal + minVal) * 50;
  500. if (core::equals(maxVal, minVal)) {
  501. Hue = 0.f;
  502. Saturation = 0.f;
  503. return;
  504. }
  505. const f32 delta = maxVal - minVal;
  506. if (Luminance <= 50) {
  507. Saturation = (delta) / (maxVal + minVal);
  508. } else {
  509. Saturation = (delta) / (2 - maxVal - minVal);
  510. }
  511. Saturation *= 100;
  512. if (core::equals(maxVal, color.getRed()))
  513. Hue = (color.getGreen() - color.getBlue()) / delta;
  514. else if (core::equals(maxVal, color.getGreen()))
  515. Hue = 2 + ((color.getBlue() - color.getRed()) / delta);
  516. else // blue is max
  517. Hue = 4 + ((color.getRed() - color.getGreen()) / delta);
  518. Hue *= 60.0f;
  519. while (Hue < 0.f)
  520. Hue += 360;
  521. }
  522. inline void SColorHSL::toRGB(SColorf &color) const
  523. {
  524. const f32 l = Luminance / 100;
  525. if (core::iszero(Saturation)) { // grey
  526. color.set(l, l, l);
  527. return;
  528. }
  529. f32 rm2;
  530. if (Luminance <= 50) {
  531. rm2 = l + l * (Saturation / 100);
  532. } else {
  533. rm2 = l + (1 - l) * (Saturation / 100);
  534. }
  535. const f32 rm1 = 2.0f * l - rm2;
  536. const f32 h = Hue / 360.0f;
  537. color.set(toRGB1(rm1, rm2, h + 1.f / 3.f),
  538. toRGB1(rm1, rm2, h),
  539. toRGB1(rm1, rm2, h - 1.f / 3.f));
  540. }
  541. // algorithm from Foley/Van-Dam
  542. inline f32 SColorHSL::toRGB1(f32 rm1, f32 rm2, f32 rh) const
  543. {
  544. if (rh < 0)
  545. rh += 1;
  546. if (rh > 1)
  547. rh -= 1;
  548. if (rh < 1.f / 6.f)
  549. rm1 = rm1 + (rm2 - rm1) * rh * 6.f;
  550. else if (rh < 0.5f)
  551. rm1 = rm2;
  552. else if (rh < 2.f / 3.f)
  553. rm1 = rm1 + (rm2 - rm1) * ((2.f / 3.f) - rh) * 6.f;
  554. return rm1;
  555. }
  556. } // end namespace video
  557. } // end namespace irr