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.

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