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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. static void blurDataTriplets (uint8* d, int num, const int delta) noexcept
  16. {
  17. uint32 last = d[0];
  18. d[0] = (uint8) ((d[0] + d[delta] + 1) / 3);
  19. d += delta;
  20. num -= 2;
  21. do
  22. {
  23. const uint32 newLast = d[0];
  24. d[0] = (uint8) ((last + d[0] + d[delta] + 1) / 3);
  25. d += delta;
  26. last = newLast;
  27. }
  28. while (--num > 0);
  29. d[0] = (uint8) ((last + d[0] + 1) / 3);
  30. }
  31. static void blurSingleChannelImage (uint8* const data, const int width, const int height,
  32. const int lineStride, const int repetitions) noexcept
  33. {
  34. jassert (width > 2 && height > 2);
  35. for (int y = 0; y < height; ++y)
  36. for (int i = repetitions; --i >= 0;)
  37. blurDataTriplets (data + lineStride * y, width, 1);
  38. for (int x = 0; x < width; ++x)
  39. for (int i = repetitions; --i >= 0;)
  40. blurDataTriplets (data + x, height, lineStride);
  41. }
  42. static void blurSingleChannelImage (Image& image, int radius)
  43. {
  44. const Image::BitmapData bm (image, Image::BitmapData::readWrite);
  45. blurSingleChannelImage (bm.data, bm.width, bm.height, bm.lineStride, 2 * radius);
  46. }
  47. //==============================================================================
  48. DropShadow::DropShadow (Colour shadowColour, const int r, Point<int> o) noexcept
  49. : colour (shadowColour), radius (r), offset (o)
  50. {
  51. jassert (radius > 0);
  52. }
  53. void DropShadow::drawForImage (Graphics& g, const Image& srcImage) const
  54. {
  55. jassert (radius > 0);
  56. if (srcImage.isValid())
  57. {
  58. Image shadowImage (srcImage.convertedToFormat (Image::SingleChannel));
  59. shadowImage.duplicateIfShared();
  60. blurSingleChannelImage (shadowImage, radius);
  61. g.setColour (colour);
  62. g.drawImageAt (shadowImage, offset.x, offset.y, true);
  63. }
  64. }
  65. void DropShadow::drawForPath (Graphics& g, const Path& path) const
  66. {
  67. jassert (radius > 0);
  68. auto area = (path.getBounds().getSmallestIntegerContainer() + offset)
  69. .expanded (radius + 1)
  70. .getIntersection (g.getClipBounds().expanded (radius + 1));
  71. if (area.getWidth() > 2 && area.getHeight() > 2)
  72. {
  73. Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true);
  74. {
  75. Graphics g2 (renderedPath);
  76. g2.setColour (Colours::white);
  77. g2.fillPath (path, AffineTransform::translation ((float) (offset.x - area.getX()),
  78. (float) (offset.y - area.getY())));
  79. }
  80. blurSingleChannelImage (renderedPath, radius);
  81. g.setColour (colour);
  82. g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
  83. }
  84. }
  85. static void drawShadowSection (Graphics& g, ColourGradient& cg, Rectangle<float> area,
  86. bool isCorner, float centreX, float centreY, float edgeX, float edgeY)
  87. {
  88. cg.point1 = area.getRelativePoint (centreX, centreY);
  89. cg.point2 = area.getRelativePoint (edgeX, edgeY);
  90. cg.isRadial = isCorner;
  91. g.setGradientFill (cg);
  92. g.fillRect (area);
  93. }
  94. void DropShadow::drawForRectangle (Graphics& g, const Rectangle<int>& targetArea) const
  95. {
  96. ColourGradient cg (colour, 0, 0, colour.withAlpha (0.0f), 0, 0, false);
  97. for (float i = 0.05f; i < 1.0f; i += 0.1f)
  98. cg.addColour (1.0 - i, colour.withMultipliedAlpha (i * i));
  99. const float radiusInset = radius / 2.0f;
  100. const float expandedRadius = radius + radiusInset;
  101. auto area = targetArea.toFloat().reduced (radiusInset) + offset.toFloat();
  102. auto r = area.expanded (expandedRadius);
  103. auto top = r.removeFromTop (expandedRadius);
  104. auto bottom = r.removeFromBottom (expandedRadius);
  105. drawShadowSection (g, cg, top.removeFromLeft (expandedRadius), true, 1.0f, 1.0f, 0, 1.0f);
  106. drawShadowSection (g, cg, top.removeFromRight (expandedRadius), true, 0, 1.0f, 1.0f, 1.0f);
  107. drawShadowSection (g, cg, top, false, 0, 1.0f, 0, 0);
  108. drawShadowSection (g, cg, bottom.removeFromLeft (expandedRadius), true, 1.0f, 0, 0, 0);
  109. drawShadowSection (g, cg, bottom.removeFromRight (expandedRadius), true, 0, 0, 1.0f, 0);
  110. drawShadowSection (g, cg, bottom, false, 0, 0, 0, 1.0f);
  111. drawShadowSection (g, cg, r.removeFromLeft (expandedRadius), false, 1.0f, 0, 0, 0);
  112. drawShadowSection (g, cg, r.removeFromRight (expandedRadius), false, 0, 0, 1.0f, 0);
  113. g.setColour (colour);
  114. g.fillRect (area);
  115. }
  116. //==============================================================================
  117. DropShadowEffect::DropShadowEffect() {}
  118. DropShadowEffect::~DropShadowEffect() {}
  119. void DropShadowEffect::setShadowProperties (const DropShadow& newShadow)
  120. {
  121. shadow = newShadow;
  122. }
  123. void DropShadowEffect::applyEffect (Image& image, Graphics& g, float scaleFactor, float alpha)
  124. {
  125. DropShadow s (shadow);
  126. s.radius = roundToInt (s.radius * scaleFactor);
  127. s.colour = s.colour.withMultipliedAlpha (alpha);
  128. s.offset.x = roundToInt (s.offset.x * scaleFactor);
  129. s.offset.y = roundToInt (s.offset.y * scaleFactor);
  130. s.drawForImage (g, image);
  131. g.setOpacity (alpha);
  132. g.drawImageAt (image, 0, 0);
  133. }
  134. } // namespace juce