SColor.h 21 KB

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