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.

185 lines
6.1KB

  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 (Colour shadowColour, const int r, Point<int> o) noexcept
  55. : colour (shadowColour), radius (r), offset (o)
  56. {
  57. jassert (radius > 0);
  58. }
  59. void DropShadow::drawForImage (Graphics& g, const Image& srcImage) const
  60. {
  61. jassert (radius > 0);
  62. if (srcImage.isValid())
  63. {
  64. Image shadowImage (srcImage.convertedToFormat (Image::SingleChannel));
  65. shadowImage.duplicateIfShared();
  66. blurSingleChannelImage (shadowImage, radius);
  67. g.setColour (colour);
  68. g.drawImageAt (shadowImage, offset.x, offset.y, true);
  69. }
  70. }
  71. void DropShadow::drawForPath (Graphics& g, const Path& path) const
  72. {
  73. jassert (radius > 0);
  74. auto area = (path.getBounds().getSmallestIntegerContainer() + offset)
  75. .expanded (radius + 1)
  76. .getIntersection (g.getClipBounds().expanded (radius + 1));
  77. if (area.getWidth() > 2 && area.getHeight() > 2)
  78. {
  79. Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true);
  80. {
  81. Graphics g2 (renderedPath);
  82. g2.setColour (Colours::white);
  83. g2.fillPath (path, AffineTransform::translation ((float) (offset.x - area.getX()),
  84. (float) (offset.y - area.getY())));
  85. }
  86. blurSingleChannelImage (renderedPath, radius);
  87. g.setColour (colour);
  88. g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
  89. }
  90. }
  91. static void drawShadowSection (Graphics& g, ColourGradient& cg, Rectangle<float> area,
  92. bool isCorner, float centreX, float centreY, float edgeX, float edgeY)
  93. {
  94. cg.point1 = area.getRelativePoint (centreX, centreY);
  95. cg.point2 = area.getRelativePoint (edgeX, edgeY);
  96. cg.isRadial = isCorner;
  97. g.setGradientFill (cg);
  98. g.fillRect (area);
  99. }
  100. void DropShadow::drawForRectangle (Graphics& g, const Rectangle<int>& targetArea) const
  101. {
  102. ColourGradient cg (colour, 0, 0, colour.withAlpha (0.0f), 0, 0, false);
  103. for (float i = 0.05f; i < 1.0f; i += 0.1f)
  104. cg.addColour (1.0 - i, colour.withMultipliedAlpha (i * i));
  105. const float radiusInset = radius / 2.0f;
  106. const float expandedRadius = radius + radiusInset;
  107. auto area = targetArea.toFloat().reduced (radiusInset) + offset.toFloat();
  108. auto r = area.expanded (expandedRadius);
  109. auto top = r.removeFromTop (expandedRadius);
  110. auto bottom = r.removeFromBottom (expandedRadius);
  111. drawShadowSection (g, cg, top.removeFromLeft (expandedRadius), true, 1.0f, 1.0f, 0, 1.0f);
  112. drawShadowSection (g, cg, top.removeFromRight (expandedRadius), true, 0, 1.0f, 1.0f, 1.0f);
  113. drawShadowSection (g, cg, top, false, 0, 1.0f, 0, 0);
  114. drawShadowSection (g, cg, bottom.removeFromLeft (expandedRadius), true, 1.0f, 0, 0, 0);
  115. drawShadowSection (g, cg, bottom.removeFromRight (expandedRadius), true, 0, 0, 1.0f, 0);
  116. drawShadowSection (g, cg, bottom, false, 0, 0, 0, 1.0f);
  117. drawShadowSection (g, cg, r.removeFromLeft (expandedRadius), false, 1.0f, 0, 0, 0);
  118. drawShadowSection (g, cg, r.removeFromRight (expandedRadius), false, 0, 0, 1.0f, 0);
  119. g.setColour (colour);
  120. g.fillRect (area);
  121. }
  122. //==============================================================================
  123. DropShadowEffect::DropShadowEffect() {}
  124. DropShadowEffect::~DropShadowEffect() {}
  125. void DropShadowEffect::setShadowProperties (const DropShadow& newShadow)
  126. {
  127. shadow = newShadow;
  128. }
  129. void DropShadowEffect::applyEffect (Image& image, Graphics& g, float scaleFactor, float alpha)
  130. {
  131. DropShadow s (shadow);
  132. s.radius = roundToInt (s.radius * scaleFactor);
  133. s.colour = s.colour.withMultipliedAlpha (alpha);
  134. s.offset.x = roundToInt (s.offset.x * scaleFactor);
  135. s.offset.y = roundToInt (s.offset.y * scaleFactor);
  136. s.drawForImage (g, image);
  137. g.setOpacity (alpha);
  138. g.drawImageAt (image, 0, 0);
  139. }
  140. } // namespace juce