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.

184 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. static inline void blurDataTriplets (uint8* d, int num, const int delta) noexcept
  19. {
  20. uint32 last = d[0];
  21. d[0] = (uint8) ((d[0] + d[delta] + 1) / 3);
  22. d += delta;
  23. num -= 2;
  24. do
  25. {
  26. const uint32 newLast = d[0];
  27. d[0] = (uint8) ((last + d[0] + d[delta] + 1) / 3);
  28. d += delta;
  29. last = newLast;
  30. }
  31. while (--num > 0);
  32. d[0] = (uint8) ((last + d[0] + 1) / 3);
  33. }
  34. static void blurSingleChannelImage (uint8* const data, const int width, const int height,
  35. const int lineStride, const int repetitions) noexcept
  36. {
  37. jassert (width > 2 && height > 2);
  38. for (int y = 0; y < height; ++y)
  39. for (int i = repetitions; --i >= 0;)
  40. blurDataTriplets (data + lineStride * y, width, 1);
  41. for (int x = 0; x < width; ++x)
  42. for (int i = repetitions; --i >= 0;)
  43. blurDataTriplets (data + x, height, lineStride);
  44. }
  45. static void blurSingleChannelImage (Image& image, int radius)
  46. {
  47. const Image::BitmapData bm (image, Image::BitmapData::readWrite);
  48. blurSingleChannelImage (bm.data, bm.width, bm.height, bm.lineStride, 2 * radius);
  49. }
  50. //==============================================================================
  51. DropShadow::DropShadow() noexcept
  52. : colour (0x90000000), radius (4)
  53. {
  54. }
  55. DropShadow::DropShadow (Colour shadowColour, const int r, Point<int> o) noexcept
  56. : colour (shadowColour), radius (r), offset (o)
  57. {
  58. jassert (radius > 0);
  59. }
  60. void DropShadow::drawForImage (Graphics& g, const Image& srcImage) const
  61. {
  62. jassert (radius > 0);
  63. if (srcImage.isValid())
  64. {
  65. Image shadowImage (srcImage.convertedToFormat (Image::SingleChannel));
  66. shadowImage.duplicateIfShared();
  67. blurSingleChannelImage (shadowImage, radius);
  68. g.setColour (colour);
  69. g.drawImageAt (shadowImage, offset.x, offset.y, true);
  70. }
  71. }
  72. void DropShadow::drawForPath (Graphics& g, const Path& path) const
  73. {
  74. jassert (radius > 0);
  75. const Rectangle<int> area ((path.getBounds().getSmallestIntegerContainer() + offset)
  76. .expanded (radius + 1)
  77. .getIntersection (g.getClipBounds().expanded (radius + 1)));
  78. if (area.getWidth() > 2 && area.getHeight() > 2)
  79. {
  80. Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true);
  81. {
  82. Graphics g2 (renderedPath);
  83. g2.setColour (Colours::white);
  84. g2.fillPath (path, AffineTransform::translation ((float) (offset.x - area.getX()),
  85. (float) (offset.y - area.getY())));
  86. }
  87. blurSingleChannelImage (renderedPath, radius);
  88. g.setColour (colour);
  89. g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
  90. }
  91. }
  92. static void drawShadowSection (Graphics& g, ColourGradient& cg, const Rectangle<int>& area,
  93. bool isCorner, float centreX, float centreY, float edgeX, float edgeY)
  94. {
  95. cg.point1 = area.getRelativePoint (centreX, centreY).toFloat();
  96. cg.point2 = area.getRelativePoint (edgeX, edgeY).toFloat();
  97. cg.isRadial = isCorner;
  98. g.setGradientFill (cg);
  99. g.fillRect (area);
  100. }
  101. void DropShadow::drawForRectangle (Graphics& g, const Rectangle<int>& targetArea) const
  102. {
  103. ColourGradient cg (colour, 0, 0, colour.withAlpha (0.0f), 0, 0, false);
  104. for (float i = 0.05f; i < 1.0f; i += 0.1f)
  105. cg.addColour (1.0 - i, colour.withMultipliedAlpha (i * i));
  106. const int radiusInset = (radius + 1) / 2;
  107. const int expandedRadius = radius + radiusInset;
  108. const Rectangle<int> area (targetArea.reduced (radiusInset) + offset);
  109. Rectangle<int> r (area.expanded (expandedRadius));
  110. Rectangle<int> top (r.removeFromTop (expandedRadius));
  111. Rectangle<int> bottom (r.removeFromBottom (expandedRadius));
  112. drawShadowSection (g, cg, top.removeFromLeft (expandedRadius), true, 1.0f, 1.0f, 0, 1.0f);
  113. drawShadowSection (g, cg, top.removeFromRight (expandedRadius), true, 0, 1.0f, 1.0f, 1.0f);
  114. drawShadowSection (g, cg, top, false, 0, 1.0f, 0, 0);
  115. drawShadowSection (g, cg, bottom.removeFromLeft (expandedRadius), true, 1.0f, 0, 0, 0);
  116. drawShadowSection (g, cg, bottom.removeFromRight (expandedRadius), true, 0, 0, 1.0f, 0);
  117. drawShadowSection (g, cg, bottom, false, 0, 0, 0, 1.0f);
  118. drawShadowSection (g, cg, r.removeFromLeft (expandedRadius), false, 1.0f, 0, 0, 0);
  119. drawShadowSection (g, cg, r.removeFromRight (expandedRadius), false, 0, 0, 1.0f, 0);
  120. g.setColour (colour);
  121. g.fillRect (area);
  122. }
  123. //==============================================================================
  124. DropShadowEffect::DropShadowEffect() {}
  125. DropShadowEffect::~DropShadowEffect() {}
  126. void DropShadowEffect::setShadowProperties (const DropShadow& newShadow)
  127. {
  128. shadow = newShadow;
  129. }
  130. void DropShadowEffect::applyEffect (Image& image, Graphics& g, float scaleFactor, float alpha)
  131. {
  132. DropShadow s (shadow);
  133. s.radius = roundToInt (s.radius * scaleFactor);
  134. s.colour = s.colour.withMultipliedAlpha (alpha);
  135. s.offset.x = roundToInt (s.offset.x * scaleFactor);
  136. s.offset.y = roundToInt (s.offset.y * scaleFactor);
  137. s.drawForImage (g, image);
  138. g.setOpacity (alpha);
  139. g.drawImageAt (image, 0, 0);
  140. }