The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

763 lines
27KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. #if JUCE_MSVC
  23. #pragma pack (push, 1)
  24. #endif
  25. class PixelRGB;
  26. class PixelAlpha;
  27. inline uint32 maskPixelComponents (uint32 x) noexcept
  28. {
  29. return (x >> 8) & 0x00ff00ff;
  30. }
  31. inline uint32 clampPixelComponents (uint32 x) noexcept
  32. {
  33. return (x | (0x01000100 - maskPixelComponents (x))) & 0x00ff00ff;
  34. }
  35. //==============================================================================
  36. /**
  37. Represents a 32-bit INTERNAL pixel with premultiplied alpha, and can perform compositing
  38. operations with it.
  39. This is used internally by the imaging classes.
  40. @see PixelRGB
  41. @tags{Graphics}
  42. */
  43. class JUCE_API PixelARGB
  44. {
  45. public:
  46. /** Creates a pixel without defining its colour. */
  47. PixelARGB() = default;
  48. ~PixelARGB() = default;
  49. PixelARGB (const uint8 a, const uint8 r, const uint8 g, const uint8 b) noexcept
  50. {
  51. components.b = b;
  52. components.g = g;
  53. components.r = r;
  54. components.a = a;
  55. }
  56. //==============================================================================
  57. /** Returns a uint32 which represents the pixel in a platform dependent format. */
  58. forcedinline uint32 getNativeARGB() const noexcept { return internal; }
  59. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  60. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  61. forcedinline uint32 getInARGBMaskOrder() const noexcept
  62. {
  63. #if JUCE_ANDROID
  64. return (uint32) ((components.a << 24) | (components.r << 16) | (components.g << 8) | (components.b << 0));
  65. #else
  66. return getNativeARGB();
  67. #endif
  68. }
  69. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  70. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  71. inline uint32 getInARGBMemoryOrder() const noexcept
  72. {
  73. #if JUCE_BIG_ENDIAN
  74. return getInARGBMaskOrder();
  75. #else
  76. return (uint32) ((components.b << 24) | (components.g << 16) | (components.r << 8) | components.a);
  77. #endif
  78. }
  79. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  80. operations. The exact channels which are returned is platform dependent. */
  81. forcedinline uint32 getEvenBytes() const noexcept { return 0x00ff00ff & internal; }
  82. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  83. operations. The exact channels which are returned is platform dependent. */
  84. forcedinline uint32 getOddBytes() const noexcept { return 0x00ff00ff & (internal >> 8); }
  85. //==============================================================================
  86. forcedinline uint8 getAlpha() const noexcept { return components.a; }
  87. forcedinline uint8 getRed() const noexcept { return components.r; }
  88. forcedinline uint8 getGreen() const noexcept { return components.g; }
  89. forcedinline uint8 getBlue() const noexcept { return components.b; }
  90. #if JUCE_GCC
  91. // NB these are here as a workaround because GCC refuses to bind to packed values.
  92. forcedinline uint8& getAlpha() noexcept { return comps [indexA]; }
  93. forcedinline uint8& getRed() noexcept { return comps [indexR]; }
  94. forcedinline uint8& getGreen() noexcept { return comps [indexG]; }
  95. forcedinline uint8& getBlue() noexcept { return comps [indexB]; }
  96. #else
  97. forcedinline uint8& getAlpha() noexcept { return components.a; }
  98. forcedinline uint8& getRed() noexcept { return components.r; }
  99. forcedinline uint8& getGreen() noexcept { return components.g; }
  100. forcedinline uint8& getBlue() noexcept { return components.b; }
  101. #endif
  102. //==============================================================================
  103. /** Copies another pixel colour over this one.
  104. This doesn't blend it - this colour is simply replaced by the other one.
  105. */
  106. template <class Pixel>
  107. forcedinline void set (const Pixel& src) noexcept
  108. {
  109. internal = src.getNativeARGB();
  110. }
  111. //==============================================================================
  112. /** Sets the pixel's colour from individual components. */
  113. void setARGB (const uint8 a, const uint8 r, const uint8 g, const uint8 b) noexcept
  114. {
  115. components.b = b;
  116. components.g = g;
  117. components.r = r;
  118. components.a = a;
  119. }
  120. //==============================================================================
  121. /** Blends another pixel onto this one.
  122. This takes into account the opacity of the pixel being overlaid, and blends
  123. it accordingly.
  124. */
  125. template <class Pixel>
  126. forcedinline void blend (const Pixel& src) noexcept
  127. {
  128. uint32 rb = src.getEvenBytes();
  129. uint32 ag = src.getOddBytes();
  130. const uint32 alpha = 0x100 - (ag >> 16);
  131. rb += maskPixelComponents (getEvenBytes() * alpha);
  132. ag += maskPixelComponents (getOddBytes() * alpha);
  133. internal = clampPixelComponents (rb) | (clampPixelComponents (ag) << 8);
  134. }
  135. /** Blends another pixel onto this one.
  136. This takes into account the opacity of the pixel being overlaid, and blends
  137. it accordingly.
  138. */
  139. forcedinline void blend (const PixelRGB src) noexcept;
  140. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  141. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  142. being used, so this can blend semi-transparently from a PixelRGB argument.
  143. */
  144. template <class Pixel>
  145. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  146. {
  147. uint32 rb = maskPixelComponents (extraAlpha * src.getEvenBytes());
  148. uint32 ag = maskPixelComponents (extraAlpha * src.getOddBytes());
  149. const uint32 alpha = 0x100 - (ag >> 16);
  150. rb += maskPixelComponents (getEvenBytes() * alpha);
  151. ag += maskPixelComponents (getOddBytes() * alpha);
  152. internal = clampPixelComponents (rb) | (clampPixelComponents (ag) << 8);
  153. }
  154. /** Blends another pixel with this one, creating a colour that is somewhere
  155. between the two, as specified by the amount.
  156. */
  157. template <class Pixel>
  158. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  159. {
  160. uint32 dEvenBytes = getEvenBytes();
  161. dEvenBytes += (((src.getEvenBytes() - dEvenBytes) * amount) >> 8);
  162. dEvenBytes &= 0x00ff00ff;
  163. uint32 dOddBytes = getOddBytes();
  164. dOddBytes += (((src.getOddBytes() - dOddBytes) * amount) >> 8);
  165. dOddBytes &= 0x00ff00ff;
  166. dOddBytes <<= 8;
  167. dOddBytes |= dEvenBytes;
  168. internal = dOddBytes;
  169. }
  170. //==============================================================================
  171. /** Replaces the colour's alpha value with another one. */
  172. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  173. {
  174. components.a = newAlpha;
  175. }
  176. /** Multiplies the colour's alpha value with another one. */
  177. forcedinline void multiplyAlpha (int multiplier) noexcept
  178. {
  179. // increment alpha by 1, so that if multiplier == 255 (full alpha),
  180. // this function will not change the values.
  181. ++multiplier;
  182. internal = ((((uint32) multiplier) * getOddBytes()) & 0xff00ff00)
  183. | (((((uint32) multiplier) * getEvenBytes()) >> 8) & 0x00ff00ff);
  184. }
  185. forcedinline void multiplyAlpha (const float multiplier) noexcept
  186. {
  187. multiplyAlpha ((int) (multiplier * 255.0f));
  188. }
  189. inline PixelARGB getUnpremultiplied() const noexcept { PixelARGB p (internal); p.unpremultiply(); return p; }
  190. /** Premultiplies the pixel's RGB values by its alpha. */
  191. forcedinline void premultiply() noexcept
  192. {
  193. const uint32 alpha = components.a;
  194. if (alpha < 0xff)
  195. {
  196. if (alpha == 0)
  197. {
  198. components.b = 0;
  199. components.g = 0;
  200. components.r = 0;
  201. }
  202. else
  203. {
  204. components.b = (uint8) ((components.b * alpha + 0x7f) >> 8);
  205. components.g = (uint8) ((components.g * alpha + 0x7f) >> 8);
  206. components.r = (uint8) ((components.r * alpha + 0x7f) >> 8);
  207. }
  208. }
  209. }
  210. /** Unpremultiplies the pixel's RGB values. */
  211. forcedinline void unpremultiply() noexcept
  212. {
  213. const uint32 alpha = components.a;
  214. if (alpha < 0xff)
  215. {
  216. if (alpha == 0)
  217. {
  218. components.b = 0;
  219. components.g = 0;
  220. components.r = 0;
  221. }
  222. else
  223. {
  224. components.b = (uint8) jmin ((uint32) 0xffu, (components.b * 0xffu) / alpha);
  225. components.g = (uint8) jmin ((uint32) 0xffu, (components.g * 0xffu) / alpha);
  226. components.r = (uint8) jmin ((uint32) 0xffu, (components.r * 0xffu) / alpha);
  227. }
  228. }
  229. }
  230. forcedinline void desaturate() noexcept
  231. {
  232. if (components.a < 0xff && components.a > 0)
  233. {
  234. const int newUnpremultipliedLevel = (0xff * ((int) components.r + (int) components.g + (int) components.b) / (3 * components.a));
  235. components.r = components.g = components.b
  236. = (uint8) ((newUnpremultipliedLevel * components.a + 0x7f) >> 8);
  237. }
  238. else
  239. {
  240. components.r = components.g = components.b
  241. = (uint8) (((int) components.r + (int) components.g + (int) components.b) / 3);
  242. }
  243. }
  244. //==============================================================================
  245. /** The indexes of the different components in the byte layout of this type of colour. */
  246. #if JUCE_ANDROID
  247. #if JUCE_BIG_ENDIAN
  248. enum { indexA = 0, indexR = 3, indexG = 2, indexB = 1 };
  249. #else
  250. enum { indexA = 3, indexR = 0, indexG = 1, indexB = 2 };
  251. #endif
  252. #else
  253. #if JUCE_BIG_ENDIAN
  254. enum { indexA = 0, indexR = 1, indexG = 2, indexB = 3 };
  255. #else
  256. enum { indexA = 3, indexR = 2, indexG = 1, indexB = 0 };
  257. #endif
  258. #endif
  259. private:
  260. //==============================================================================
  261. PixelARGB (uint32 internalValue) noexcept : internal (internalValue)
  262. {
  263. }
  264. //==============================================================================
  265. struct Components
  266. {
  267. #if JUCE_ANDROID
  268. #if JUCE_BIG_ENDIAN
  269. uint8 a, b, g, r;
  270. #else
  271. uint8 r, g, b, a;
  272. #endif
  273. #else
  274. #if JUCE_BIG_ENDIAN
  275. uint8 a, r, g, b;
  276. #else
  277. uint8 b, g, r, a;
  278. #endif
  279. #endif
  280. } JUCE_PACKED;
  281. union
  282. {
  283. uint32 internal;
  284. Components components;
  285. #if JUCE_GCC
  286. uint8 comps[4]; // helper struct needed because gcc does not allow references to packed union members
  287. #endif
  288. };
  289. }
  290. #ifndef DOXYGEN
  291. JUCE_PACKED
  292. #endif
  293. ;
  294. //==============================================================================
  295. /**
  296. Represents a 24-bit RGB pixel, and can perform compositing operations on it.
  297. This is used internally by the imaging classes.
  298. @see PixelARGB
  299. @tags{Graphics}
  300. */
  301. class JUCE_API PixelRGB
  302. {
  303. public:
  304. /** Creates a pixel without defining its colour. */
  305. PixelRGB() = default;
  306. ~PixelRGB() = default;
  307. //==============================================================================
  308. /** Returns a uint32 which represents the pixel in a platform dependent format which is compatible
  309. with the native format of a PixelARGB.
  310. @see PixelARGB::getNativeARGB */
  311. forcedinline uint32 getNativeARGB() const noexcept
  312. {
  313. #if JUCE_ANDROID
  314. return (uint32) ((0xffu << 24) | r | ((uint32) g << 8) | ((uint32) b << 16));
  315. #else
  316. return (uint32) ((0xffu << 24) | b | ((uint32) g << 8) | ((uint32) r << 16));
  317. #endif
  318. }
  319. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  320. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  321. forcedinline uint32 getInARGBMaskOrder() const noexcept
  322. {
  323. #if JUCE_ANDROID
  324. return (uint32) ((0xffu << 24) | b | ((uint32) g << 8) | ((uint32) r << 16));
  325. #else
  326. return getNativeARGB();
  327. #endif
  328. }
  329. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  330. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  331. inline uint32 getInARGBMemoryOrder() const noexcept
  332. {
  333. #if JUCE_BIG_ENDIAN
  334. return getInARGBMaskOrder();
  335. #else
  336. return (uint32) ((b << 24) | (g << 16) | (r << 8) | 0xff);
  337. #endif
  338. }
  339. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  340. operations. The exact channels which are returned is platform dependent but compatible with the
  341. return value of getEvenBytes of the PixelARGB class.
  342. @see PixelARGB::getEvenBytes */
  343. forcedinline uint32 getEvenBytes() const noexcept
  344. {
  345. #if JUCE_ANDROID
  346. return (uint32) (r | (b << 16));
  347. #else
  348. return (uint32) (b | (r << 16));
  349. #endif
  350. }
  351. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  352. operations. The exact channels which are returned is platform dependent but compatible with the
  353. return value of getOddBytes of the PixelARGB class.
  354. @see PixelARGB::getOddBytes */
  355. forcedinline uint32 getOddBytes() const noexcept { return (uint32)0xff0000 | g; }
  356. //==============================================================================
  357. forcedinline uint8 getAlpha() const noexcept { return 0xff; }
  358. forcedinline uint8 getRed() const noexcept { return r; }
  359. forcedinline uint8 getGreen() const noexcept { return g; }
  360. forcedinline uint8 getBlue() const noexcept { return b; }
  361. forcedinline uint8& getRed() noexcept { return r; }
  362. forcedinline uint8& getGreen() noexcept { return g; }
  363. forcedinline uint8& getBlue() noexcept { return b; }
  364. //==============================================================================
  365. /** Copies another pixel colour over this one.
  366. This doesn't blend it - this colour is simply replaced by the other one.
  367. Because PixelRGB has no alpha channel, any alpha value in the source pixel
  368. is thrown away.
  369. */
  370. template <class Pixel>
  371. forcedinline void set (const Pixel& src) noexcept
  372. {
  373. b = src.getBlue();
  374. g = src.getGreen();
  375. r = src.getRed();
  376. }
  377. /** Sets the pixel's colour from individual components. */
  378. void setARGB (const uint8, const uint8 red, const uint8 green, const uint8 blue) noexcept
  379. {
  380. r = red;
  381. g = green;
  382. b = blue;
  383. }
  384. //==============================================================================
  385. /** Blends another pixel onto this one.
  386. This takes into account the opacity of the pixel being overlaid, and blends
  387. it accordingly.
  388. */
  389. template <class Pixel>
  390. forcedinline void blend (const Pixel& src) noexcept
  391. {
  392. const uint32 alpha = (uint32) (0x100 - src.getAlpha());
  393. // getEvenBytes returns 0x00rr00bb on non-android
  394. uint32 rb = clampPixelComponents (src.getEvenBytes() + maskPixelComponents (getEvenBytes() * alpha));
  395. // getOddBytes returns 0x00aa00gg on non-android
  396. uint32 ag = clampPixelComponents (src.getOddBytes() + ((g * alpha) >> 8));
  397. g = (uint8) (ag & 0xff);
  398. #if JUCE_ANDROID
  399. b = (uint8) (rb >> 16);
  400. r = (uint8) (rb & 0xff);
  401. #else
  402. r = (uint8) (rb >> 16);
  403. b = (uint8) (rb & 0xff);
  404. #endif
  405. }
  406. forcedinline void blend (const PixelRGB src) noexcept
  407. {
  408. set (src);
  409. }
  410. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  411. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  412. being used, so this can blend semi-transparently from a PixelRGB argument.
  413. */
  414. template <class Pixel>
  415. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  416. {
  417. uint32 ag = maskPixelComponents (extraAlpha * src.getOddBytes());
  418. uint32 rb = maskPixelComponents (extraAlpha * src.getEvenBytes());
  419. const uint32 alpha = 0x100 - (ag >> 16);
  420. ag = clampPixelComponents (ag + (g * alpha >> 8));
  421. rb = clampPixelComponents (rb + maskPixelComponents (getEvenBytes() * alpha));
  422. g = (uint8) (ag & 0xff);
  423. #if JUCE_ANDROID
  424. b = (uint8) (rb >> 16);
  425. r = (uint8) (rb & 0xff);
  426. #else
  427. r = (uint8) (rb >> 16);
  428. b = (uint8) (rb & 0xff);
  429. #endif
  430. }
  431. /** Blends another pixel with this one, creating a colour that is somewhere
  432. between the two, as specified by the amount.
  433. */
  434. template <class Pixel>
  435. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  436. {
  437. uint32 dEvenBytes = getEvenBytes();
  438. dEvenBytes += (((src.getEvenBytes() - dEvenBytes) * amount) >> 8);
  439. uint32 dOddBytes = getOddBytes();
  440. dOddBytes += (((src.getOddBytes() - dOddBytes) * amount) >> 8);
  441. g = (uint8) (dOddBytes & 0xff); // dOddBytes = 0x00aa00gg
  442. #if JUCE_ANDROID
  443. r = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00bb00rr
  444. b = (uint8) (dEvenBytes >> 16);
  445. #else
  446. b = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00rr00bb
  447. r = (uint8) (dEvenBytes >> 16);
  448. #endif
  449. }
  450. //==============================================================================
  451. /** This method is included for compatibility with the PixelARGB class. */
  452. forcedinline void setAlpha (const uint8) noexcept {}
  453. /** Multiplies the colour's alpha value with another one. */
  454. forcedinline void multiplyAlpha (int) noexcept {}
  455. /** Multiplies the colour's alpha value with another one. */
  456. forcedinline void multiplyAlpha (float) noexcept {}
  457. /** Premultiplies the pixel's RGB values by its alpha. */
  458. forcedinline void premultiply() noexcept {}
  459. /** Unpremultiplies the pixel's RGB values. */
  460. forcedinline void unpremultiply() noexcept {}
  461. forcedinline void desaturate() noexcept
  462. {
  463. r = g = b = (uint8) (((int) r + (int) g + (int) b) / 3);
  464. }
  465. //==============================================================================
  466. /** The indexes of the different components in the byte layout of this type of colour. */
  467. #if JUCE_MAC
  468. enum { indexR = 0, indexG = 1, indexB = 2 };
  469. #else
  470. enum { indexR = 2, indexG = 1, indexB = 0 };
  471. #endif
  472. private:
  473. //==============================================================================
  474. PixelRGB (const uint32 internal) noexcept
  475. {
  476. #if JUCE_ANDROID
  477. b = (uint8) (internal >> 16);
  478. g = (uint8) (internal >> 8);
  479. r = (uint8) (internal);
  480. #else
  481. r = (uint8) (internal >> 16);
  482. g = (uint8) (internal >> 8);
  483. b = (uint8) (internal);
  484. #endif
  485. }
  486. //==============================================================================
  487. #if JUCE_MAC
  488. uint8 r, g, b;
  489. #else
  490. uint8 b, g, r;
  491. #endif
  492. }
  493. #ifndef DOXYGEN
  494. JUCE_PACKED
  495. #endif
  496. ;
  497. forcedinline void PixelARGB::blend (const PixelRGB src) noexcept
  498. {
  499. set (src);
  500. }
  501. //==============================================================================
  502. /**
  503. Represents an 8-bit single-channel pixel, and can perform compositing operations on it.
  504. This is used internally by the imaging classes.
  505. @see PixelARGB, PixelRGB
  506. @tags{Graphics}
  507. */
  508. class JUCE_API PixelAlpha
  509. {
  510. public:
  511. /** Creates a pixel without defining its colour. */
  512. PixelAlpha() = default;
  513. ~PixelAlpha() = default;
  514. //==============================================================================
  515. /** Returns a uint32 which represents the pixel in a platform dependent format which is compatible
  516. with the native format of a PixelARGB.
  517. @see PixelARGB::getNativeARGB */
  518. forcedinline uint32 getNativeARGB() const noexcept { return (uint32) ((a << 24) | (a << 16) | (a << 8) | a); }
  519. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  520. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  521. forcedinline uint32 getInARGBMaskOrder() const noexcept { return getNativeARGB(); }
  522. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  523. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  524. inline uint32 getInARGBMemoryOrder() const noexcept { return getNativeARGB(); }
  525. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  526. operations. The exact channels which are returned is platform dependent but compatible with the
  527. return value of getEvenBytes of the PixelARGB class.
  528. @see PixelARGB::getEvenBytes */
  529. forcedinline uint32 getEvenBytes() const noexcept { return (uint32) ((a << 16) | a); }
  530. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  531. operations. The exact channels which are returned is platform dependent but compatible with the
  532. return value of getOddBytes of the PixelARGB class.
  533. @see PixelARGB::getOddBytes */
  534. forcedinline uint32 getOddBytes() const noexcept { return (uint32) ((a << 16) | a); }
  535. //==============================================================================
  536. forcedinline uint8 getAlpha() const noexcept { return a; }
  537. forcedinline uint8& getAlpha() noexcept { return a; }
  538. forcedinline uint8 getRed() const noexcept { return 0; }
  539. forcedinline uint8 getGreen() const noexcept { return 0; }
  540. forcedinline uint8 getBlue() const noexcept { return 0; }
  541. //==============================================================================
  542. /** Copies another pixel colour over this one.
  543. This doesn't blend it - this colour is simply replaced by the other one.
  544. */
  545. template <class Pixel>
  546. forcedinline void set (const Pixel& src) noexcept
  547. {
  548. a = src.getAlpha();
  549. }
  550. /** Sets the pixel's colour from individual components. */
  551. forcedinline void setARGB (const uint8 a_, const uint8 /*r*/, const uint8 /*g*/, const uint8 /*b*/) noexcept
  552. {
  553. a = a_;
  554. }
  555. //==============================================================================
  556. /** Blends another pixel onto this one.
  557. This takes into account the opacity of the pixel being overlaid, and blends
  558. it accordingly.
  559. */
  560. template <class Pixel>
  561. forcedinline void blend (const Pixel& src) noexcept
  562. {
  563. const int srcA = src.getAlpha();
  564. a = (uint8) ((a * (0x100 - srcA) >> 8) + srcA);
  565. }
  566. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  567. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  568. being used, so this can blend semi-transparently from a PixelRGB argument.
  569. */
  570. template <class Pixel>
  571. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  572. {
  573. ++extraAlpha;
  574. const int srcAlpha = (int) ((extraAlpha * src.getAlpha()) >> 8);
  575. a = (uint8) ((a * (0x100 - srcAlpha) >> 8) + srcAlpha);
  576. }
  577. /** Blends another pixel with this one, creating a colour that is somewhere
  578. between the two, as specified by the amount.
  579. */
  580. template <class Pixel>
  581. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  582. {
  583. a += ((src.getAlpha() - a) * amount) >> 8;
  584. }
  585. //==============================================================================
  586. /** Replaces the colour's alpha value with another one. */
  587. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  588. {
  589. a = newAlpha;
  590. }
  591. /** Multiplies the colour's alpha value with another one. */
  592. forcedinline void multiplyAlpha (int multiplier) noexcept
  593. {
  594. ++multiplier;
  595. a = (uint8) ((a * multiplier) >> 8);
  596. }
  597. forcedinline void multiplyAlpha (const float multiplier) noexcept
  598. {
  599. a = (uint8) (a * multiplier);
  600. }
  601. /** Premultiplies the pixel's RGB values by its alpha. */
  602. forcedinline void premultiply() noexcept {}
  603. /** Unpremultiplies the pixel's RGB values. */
  604. forcedinline void unpremultiply() noexcept {}
  605. forcedinline void desaturate() noexcept {}
  606. //==============================================================================
  607. /** The indexes of the different components in the byte layout of this type of colour. */
  608. enum { indexA = 0 };
  609. private:
  610. //==============================================================================
  611. PixelAlpha (const uint32 internal) noexcept
  612. {
  613. a = (uint8) (internal >> 24);
  614. }
  615. //==============================================================================
  616. uint8 a;
  617. }
  618. #ifndef DOXYGEN
  619. JUCE_PACKED
  620. #endif
  621. ;
  622. #if JUCE_MSVC
  623. #pragma pack (pop)
  624. #endif
  625. } // namespace juce