SColor.h 23 KB

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