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_DropShadowEffect.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. static inline void blurDataTriplets (uint8* d, int num, const int delta) noexcept
  22. {
  23. uint32 last = d[0];
  24. d[0] = (uint8) ((d[0] + d[delta] + 1) / 3);
  25. d += delta;
  26. num -= 2;
  27. do
  28. {
  29. const uint32 newLast = d[0];
  30. d[0] = (uint8) ((last + d[0] + d[delta] + 1) / 3);
  31. d += delta;
  32. last = newLast;
  33. }
  34. while (--num > 0);
  35. d[0] = (uint8) ((last + d[0] + 1) / 3);
  36. }
  37. static void blurSingleChannelImage (uint8* const data, const int width, const int height,
  38. const int lineStride, const int repetitions) noexcept
  39. {
  40. jassert (width > 2 && height > 2);
  41. for (int y = 0; y < height; ++y)
  42. for (int i = repetitions; --i >= 0;)
  43. blurDataTriplets (data + lineStride * y, width, 1);
  44. for (int x = 0; x < width; ++x)
  45. for (int i = repetitions; --i >= 0;)
  46. blurDataTriplets (data + x, height, lineStride);
  47. }
  48. static void blurSingleChannelImage (Image& image, int radius)
  49. {
  50. const Image::BitmapData bm (image, Image::BitmapData::readWrite);
  51. blurSingleChannelImage (bm.data, bm.width, bm.height, bm.lineStride, 2 * radius);
  52. }
  53. //==============================================================================
  54. DropShadow::DropShadow() noexcept
  55. : colour (0x90000000), radius (4)
  56. {
  57. }
  58. DropShadow::DropShadow (Colour shadowColour, const int r, Point<int> o) noexcept
  59. : colour (shadowColour), radius (r), offset (o)
  60. {
  61. jassert (radius > 0);
  62. }
  63. void DropShadow::drawForImage (Graphics& g, const Image& srcImage) const
  64. {
  65. jassert (radius > 0);
  66. if (srcImage.isValid())
  67. {
  68. Image shadowImage (srcImage.convertedToFormat (Image::SingleChannel));
  69. shadowImage.duplicateIfShared();
  70. blurSingleChannelImage (shadowImage, radius);
  71. g.setColour (colour);
  72. g.drawImageAt (shadowImage, offset.x, offset.y, true);
  73. }
  74. }
  75. void DropShadow::drawForPath (Graphics& g, const Path& path) const
  76. {
  77. jassert (radius > 0);
  78. const Rectangle<int> area ((path.getBounds().getSmallestIntegerContainer() + offset)
  79. .expanded (radius + 1)
  80. .getIntersection (g.getClipBounds().expanded (radius + 1)));
  81. if (area.getWidth() > 2 && area.getHeight() > 2)
  82. {
  83. Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true);
  84. {
  85. Graphics g2 (renderedPath);
  86. g2.setColour (Colours::white);
  87. g2.fillPath (path, AffineTransform::translation ((float) (offset.x - area.getX()),
  88. (float) (offset.y - area.getY())));
  89. }
  90. blurSingleChannelImage (renderedPath, radius);
  91. g.setColour (colour);
  92. g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
  93. }
  94. }
  95. static void drawShadowSection (Graphics& g, ColourGradient& cg, Rectangle<float> area,
  96. bool isCorner, float centreX, float centreY, float edgeX, float edgeY)
  97. {
  98. cg.point1 = area.getRelativePoint (centreX, centreY);
  99. cg.point2 = area.getRelativePoint (edgeX, edgeY);
  100. cg.isRadial = isCorner;
  101. g.setGradientFill (cg);
  102. g.fillRect (area);
  103. }
  104. void DropShadow::drawForRectangle (Graphics& g, const Rectangle<int>& targetArea) const
  105. {
  106. ColourGradient cg (colour, 0, 0, colour.withAlpha (0.0f), 0, 0, false);
  107. for (float i = 0.05f; i < 1.0f; i += 0.1f)
  108. cg.addColour (1.0 - i, colour.withMultipliedAlpha (i * i));
  109. const float radiusInset = (radius + 1) / 2.0f;
  110. const float expandedRadius = radius + radiusInset;
  111. const Rectangle<float> area (targetArea.toFloat().reduced (radiusInset) + offset.toFloat());
  112. Rectangle<float> r (area.expanded (expandedRadius));
  113. Rectangle<float> top (r.removeFromTop (expandedRadius));
  114. Rectangle<float> bottom (r.removeFromBottom (expandedRadius));
  115. drawShadowSection (g, cg, top.removeFromLeft (expandedRadius), true, 1.0f, 1.0f, 0, 1.0f);
  116. drawShadowSection (g, cg, top.removeFromRight (expandedRadius), true, 0, 1.0f, 1.0f, 1.0f);
  117. drawShadowSection (g, cg, top, false, 0, 1.0f, 0, 0);
  118. drawShadowSection (g, cg, bottom.removeFromLeft (expandedRadius), true, 1.0f, 0, 0, 0);
  119. drawShadowSection (g, cg, bottom.removeFromRight (expandedRadius), true, 0, 0, 1.0f, 0);
  120. drawShadowSection (g, cg, bottom, false, 0, 0, 0, 1.0f);
  121. drawShadowSection (g, cg, r.removeFromLeft (expandedRadius), false, 1.0f, 0, 0, 0);
  122. drawShadowSection (g, cg, r.removeFromRight (expandedRadius), false, 0, 0, 1.0f, 0);
  123. g.setColour (colour);
  124. g.fillRect (area);
  125. }
  126. //==============================================================================
  127. DropShadowEffect::DropShadowEffect() {}
  128. DropShadowEffect::~DropShadowEffect() {}
  129. void DropShadowEffect::setShadowProperties (const DropShadow& newShadow)
  130. {
  131. shadow = newShadow;
  132. }
  133. void DropShadowEffect::applyEffect (Image& image, Graphics& g, float scaleFactor, float alpha)
  134. {
  135. DropShadow s (shadow);
  136. s.radius = roundToInt (s.radius * scaleFactor);
  137. s.colour = s.colour.withMultipliedAlpha (alpha);
  138. s.offset.x = roundToInt (s.offset.x * scaleFactor);
  139. s.offset.y = roundToInt (s.offset.y * scaleFactor);
  140. s.drawForImage (g, image);
  141. g.setOpacity (alpha);
  142. g.drawImageAt (image, 0, 0);
  143. }
  144. } // namespace juce