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.

juce_PixelFormats.h 25KB

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