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.

764 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 (const uint32 internalValue) noexcept
  262. : internal (internalValue)
  263. {
  264. }
  265. //==============================================================================
  266. struct Components
  267. {
  268. #if JUCE_ANDROID
  269. #if JUCE_BIG_ENDIAN
  270. uint8 a, b, g, r;
  271. #else
  272. uint8 r, g, b, a;
  273. #endif
  274. #else
  275. #if JUCE_BIG_ENDIAN
  276. uint8 a, r, g, b;
  277. #else
  278. uint8 b, g, r, a;
  279. #endif
  280. #endif
  281. } JUCE_PACKED;
  282. union
  283. {
  284. uint32 internal;
  285. Components components;
  286. #if JUCE_GCC
  287. uint8 comps[4]; // helper struct needed because gcc does not allow references to packed union members
  288. #endif
  289. };
  290. }
  291. #ifndef DOXYGEN
  292. JUCE_PACKED
  293. #endif
  294. ;
  295. //==============================================================================
  296. /**
  297. Represents a 24-bit RGB pixel, and can perform compositing operations on it.
  298. This is used internally by the imaging classes.
  299. @see PixelARGB
  300. @tags{Graphics}
  301. */
  302. class JUCE_API PixelRGB
  303. {
  304. public:
  305. /** Creates a pixel without defining its colour. */
  306. PixelRGB() = default;
  307. ~PixelRGB() = default;
  308. //==============================================================================
  309. /** Returns a uint32 which represents the pixel in a platform dependent format which is compatible
  310. with the native format of a PixelARGB.
  311. @see PixelARGB::getNativeARGB */
  312. forcedinline uint32 getNativeARGB() const noexcept
  313. {
  314. #if JUCE_ANDROID
  315. return (uint32) ((0xff << 24) | r | (g << 8) | (b << 16));
  316. #else
  317. return (uint32) ((0xff << 24) | b | (g << 8) | (r << 16));
  318. #endif
  319. }
  320. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  321. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  322. forcedinline uint32 getInARGBMaskOrder() const noexcept
  323. {
  324. #if JUCE_ANDROID
  325. return (uint32) ((0xff << 24) | (r << 16) | (g << 8) | (b << 0));
  326. #else
  327. return getNativeARGB();
  328. #endif
  329. }
  330. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  331. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  332. inline uint32 getInARGBMemoryOrder() const noexcept
  333. {
  334. #if JUCE_BIG_ENDIAN
  335. return getInARGBMaskOrder();
  336. #else
  337. return (uint32) ((b << 24) | (g << 16) | (r << 8) | 0xff);
  338. #endif
  339. }
  340. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  341. operations. The exact channels which are returned is platform dependent but compatible with the
  342. return value of getEvenBytes of the PixelARGB class.
  343. @see PixelARGB::getEvenBytes */
  344. forcedinline uint32 getEvenBytes() const noexcept
  345. {
  346. #if JUCE_ANDROID
  347. return (uint32) (r | (b << 16));
  348. #else
  349. return (uint32) (b | (r << 16));
  350. #endif
  351. }
  352. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  353. operations. The exact channels which are returned is platform dependent but compatible with the
  354. return value of getOddBytes of the PixelARGB class.
  355. @see PixelARGB::getOddBytes */
  356. forcedinline uint32 getOddBytes() const noexcept { return (uint32)0xff0000 | g; }
  357. //==============================================================================
  358. forcedinline uint8 getAlpha() const noexcept { return 0xff; }
  359. forcedinline uint8 getRed() const noexcept { return r; }
  360. forcedinline uint8 getGreen() const noexcept { return g; }
  361. forcedinline uint8 getBlue() const noexcept { return b; }
  362. forcedinline uint8& getRed() noexcept { return r; }
  363. forcedinline uint8& getGreen() noexcept { return g; }
  364. forcedinline uint8& getBlue() noexcept { return b; }
  365. //==============================================================================
  366. /** Copies another pixel colour over this one.
  367. This doesn't blend it - this colour is simply replaced by the other one.
  368. Because PixelRGB has no alpha channel, any alpha value in the source pixel
  369. is thrown away.
  370. */
  371. template <class Pixel>
  372. forcedinline void set (const Pixel& src) noexcept
  373. {
  374. b = src.getBlue();
  375. g = src.getGreen();
  376. r = src.getRed();
  377. }
  378. /** Sets the pixel's colour from individual components. */
  379. void setARGB (const uint8, const uint8 red, const uint8 green, const uint8 blue) noexcept
  380. {
  381. r = red;
  382. g = green;
  383. b = blue;
  384. }
  385. //==============================================================================
  386. /** Blends another pixel onto this one.
  387. This takes into account the opacity of the pixel being overlaid, and blends
  388. it accordingly.
  389. */
  390. template <class Pixel>
  391. forcedinline void blend (const Pixel& src) noexcept
  392. {
  393. const uint32 alpha = (uint32) (0x100 - src.getAlpha());
  394. // getEvenBytes returns 0x00rr00bb on non-android
  395. uint32 rb = clampPixelComponents (src.getEvenBytes() + maskPixelComponents (getEvenBytes() * alpha));
  396. // getOddBytes returns 0x00aa00gg on non-android
  397. uint32 ag = clampPixelComponents (src.getOddBytes() + ((g * alpha) >> 8));
  398. g = (uint8) (ag & 0xff);
  399. #if JUCE_ANDROID
  400. b = (uint8) (rb >> 16);
  401. r = (uint8) (rb & 0xff);
  402. #else
  403. r = (uint8) (rb >> 16);
  404. b = (uint8) (rb & 0xff);
  405. #endif
  406. }
  407. forcedinline void blend (const PixelRGB src) noexcept
  408. {
  409. set (src);
  410. }
  411. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  412. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  413. being used, so this can blend semi-transparently from a PixelRGB argument.
  414. */
  415. template <class Pixel>
  416. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  417. {
  418. uint32 ag = maskPixelComponents (extraAlpha * src.getOddBytes());
  419. uint32 rb = maskPixelComponents (extraAlpha * src.getEvenBytes());
  420. const uint32 alpha = 0x100 - (ag >> 16);
  421. ag = clampPixelComponents (ag + (g * alpha >> 8));
  422. rb = clampPixelComponents (rb + maskPixelComponents (getEvenBytes() * alpha));
  423. g = (uint8) (ag & 0xff);
  424. #if JUCE_ANDROID
  425. b = (uint8) (rb >> 16);
  426. r = (uint8) (rb & 0xff);
  427. #else
  428. r = (uint8) (rb >> 16);
  429. b = (uint8) (rb & 0xff);
  430. #endif
  431. }
  432. /** Blends another pixel with this one, creating a colour that is somewhere
  433. between the two, as specified by the amount.
  434. */
  435. template <class Pixel>
  436. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  437. {
  438. uint32 dEvenBytes = getEvenBytes();
  439. dEvenBytes += (((src.getEvenBytes() - dEvenBytes) * amount) >> 8);
  440. uint32 dOddBytes = getOddBytes();
  441. dOddBytes += (((src.getOddBytes() - dOddBytes) * amount) >> 8);
  442. g = (uint8) (dOddBytes & 0xff); // dOddBytes = 0x00aa00gg
  443. #if JUCE_ANDROID
  444. r = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00bb00rr
  445. b = (uint8) (dEvenBytes >> 16);
  446. #else
  447. b = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00rr00bb
  448. r = (uint8) (dEvenBytes >> 16);
  449. #endif
  450. }
  451. //==============================================================================
  452. /** This method is included for compatibility with the PixelARGB class. */
  453. forcedinline void setAlpha (const uint8) noexcept {}
  454. /** Multiplies the colour's alpha value with another one. */
  455. forcedinline void multiplyAlpha (int) noexcept {}
  456. /** Multiplies the colour's alpha value with another one. */
  457. forcedinline void multiplyAlpha (float) noexcept {}
  458. /** Premultiplies the pixel's RGB values by its alpha. */
  459. forcedinline void premultiply() noexcept {}
  460. /** Unpremultiplies the pixel's RGB values. */
  461. forcedinline void unpremultiply() noexcept {}
  462. forcedinline void desaturate() noexcept
  463. {
  464. r = g = b = (uint8) (((int) r + (int) g + (int) b) / 3);
  465. }
  466. //==============================================================================
  467. /** The indexes of the different components in the byte layout of this type of colour. */
  468. #if JUCE_MAC
  469. enum { indexR = 0, indexG = 1, indexB = 2 };
  470. #else
  471. enum { indexR = 2, indexG = 1, indexB = 0 };
  472. #endif
  473. private:
  474. //==============================================================================
  475. PixelRGB (const uint32 internal) noexcept
  476. {
  477. #if JUCE_ANDROID
  478. b = (uint8) (internal >> 16);
  479. g = (uint8) (internal >> 8);
  480. r = (uint8) (internal);
  481. #else
  482. r = (uint8) (internal >> 16);
  483. g = (uint8) (internal >> 8);
  484. b = (uint8) (internal);
  485. #endif
  486. }
  487. //==============================================================================
  488. #if JUCE_MAC
  489. uint8 r, g, b;
  490. #else
  491. uint8 b, g, r;
  492. #endif
  493. }
  494. #ifndef DOXYGEN
  495. JUCE_PACKED
  496. #endif
  497. ;
  498. forcedinline void PixelARGB::blend (const PixelRGB src) noexcept
  499. {
  500. set (src);
  501. }
  502. //==============================================================================
  503. /**
  504. Represents an 8-bit single-channel pixel, and can perform compositing operations on it.
  505. This is used internally by the imaging classes.
  506. @see PixelARGB, PixelRGB
  507. @tags{Graphics}
  508. */
  509. class JUCE_API PixelAlpha
  510. {
  511. public:
  512. /** Creates a pixel without defining its colour. */
  513. PixelAlpha() = default;
  514. ~PixelAlpha() = default;
  515. //==============================================================================
  516. /** Returns a uint32 which represents the pixel in a platform dependent format which is compatible
  517. with the native format of a PixelARGB.
  518. @see PixelARGB::getNativeARGB */
  519. forcedinline uint32 getNativeARGB() const noexcept { return (uint32) ((a << 24) | (a << 16) | (a << 8) | a); }
  520. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  521. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  522. forcedinline uint32 getInARGBMaskOrder() const noexcept { return getNativeARGB(); }
  523. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  524. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  525. inline uint32 getInARGBMemoryOrder() const noexcept { return getNativeARGB(); }
  526. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  527. operations. The exact channels which are returned is platform dependent but compatible with the
  528. return value of getEvenBytes of the PixelARGB class.
  529. @see PixelARGB::getEvenBytes */
  530. forcedinline uint32 getEvenBytes() const noexcept { return (uint32) ((a << 16) | a); }
  531. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  532. operations. The exact channels which are returned is platform dependent but compatible with the
  533. return value of getOddBytes of the PixelARGB class.
  534. @see PixelARGB::getOddBytes */
  535. forcedinline uint32 getOddBytes() const noexcept { return (uint32) ((a << 16) | a); }
  536. //==============================================================================
  537. forcedinline uint8 getAlpha() const noexcept { return a; }
  538. forcedinline uint8& getAlpha() noexcept { return a; }
  539. forcedinline uint8 getRed() const noexcept { return 0; }
  540. forcedinline uint8 getGreen() const noexcept { return 0; }
  541. forcedinline uint8 getBlue() const noexcept { return 0; }
  542. //==============================================================================
  543. /** Copies another pixel colour over this one.
  544. This doesn't blend it - this colour is simply replaced by the other one.
  545. */
  546. template <class Pixel>
  547. forcedinline void set (const Pixel& src) noexcept
  548. {
  549. a = src.getAlpha();
  550. }
  551. /** Sets the pixel's colour from individual components. */
  552. forcedinline void setARGB (const uint8 a_, const uint8 /*r*/, const uint8 /*g*/, const uint8 /*b*/) noexcept
  553. {
  554. a = a_;
  555. }
  556. //==============================================================================
  557. /** Blends another pixel onto this one.
  558. This takes into account the opacity of the pixel being overlaid, and blends
  559. it accordingly.
  560. */
  561. template <class Pixel>
  562. forcedinline void blend (const Pixel& src) noexcept
  563. {
  564. const int srcA = src.getAlpha();
  565. a = (uint8) ((a * (0x100 - srcA) >> 8) + srcA);
  566. }
  567. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  568. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  569. being used, so this can blend semi-transparently from a PixelRGB argument.
  570. */
  571. template <class Pixel>
  572. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  573. {
  574. ++extraAlpha;
  575. const int srcAlpha = (int) ((extraAlpha * src.getAlpha()) >> 8);
  576. a = (uint8) ((a * (0x100 - srcAlpha) >> 8) + srcAlpha);
  577. }
  578. /** Blends another pixel with this one, creating a colour that is somewhere
  579. between the two, as specified by the amount.
  580. */
  581. template <class Pixel>
  582. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  583. {
  584. a += ((src.getAlpha() - a) * amount) >> 8;
  585. }
  586. //==============================================================================
  587. /** Replaces the colour's alpha value with another one. */
  588. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  589. {
  590. a = newAlpha;
  591. }
  592. /** Multiplies the colour's alpha value with another one. */
  593. forcedinline void multiplyAlpha (int multiplier) noexcept
  594. {
  595. ++multiplier;
  596. a = (uint8) ((a * multiplier) >> 8);
  597. }
  598. forcedinline void multiplyAlpha (const float multiplier) noexcept
  599. {
  600. a = (uint8) (a * multiplier);
  601. }
  602. /** Premultiplies the pixel's RGB values by its alpha. */
  603. forcedinline void premultiply() noexcept {}
  604. /** Unpremultiplies the pixel's RGB values. */
  605. forcedinline void unpremultiply() noexcept {}
  606. forcedinline void desaturate() noexcept {}
  607. //==============================================================================
  608. /** The indexes of the different components in the byte layout of this type of colour. */
  609. enum { indexA = 0 };
  610. private:
  611. //==============================================================================
  612. PixelAlpha (const uint32 internal) noexcept
  613. {
  614. a = (uint8) (internal >> 24);
  615. }
  616. //==============================================================================
  617. uint8 a;
  618. }
  619. #ifndef DOXYGEN
  620. JUCE_PACKED
  621. #endif
  622. ;
  623. #if JUCE_MSVC
  624. #pragma pack (pop)
  625. #endif
  626. } // namespace juce