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.

741 lines
26KB

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