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.

740 lines
26KB

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