Audio plugin host https://kx.studio/carla
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.

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