SColor.h 19 KB

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