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.1KB

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