Skip to content

Instantly share code, notes, and snippets.

@sudara
Last active February 6, 2024 16:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudara/7be844196a8b811216eba0f7d37c7599 to your computer and use it in GitHub Desktop.
Save sudara/7be844196a8b811216eba0f7d37c7599 to your computer and use it in GitHub Desktop.
static void renderDropShadow (juce::Graphics& g, const juce::Path& path, juce::Colour color, const int radius = 1, const juce::Point<int> offset = { 0, 0 }, int spread = 0)
{
if (radius < 1)
return;
auto area = (path.getBounds().getSmallestIntegerContainer() + offset)
.expanded (radius + spread + 1)
.getIntersection (g.getClipBounds().expanded (radius + spread + 1));
if (area.getWidth() < 2 || area.getHeight() < 2)
return;
// spread enlarges or shrinks the path before blurring it
auto spreadPath = juce::Path (path);
if (spread != 0)
{
area.expand (spread, spread);
auto bounds = path.getBounds().expanded (spread);
spreadPath.scaleToFit (bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), true);
}
juce::Image renderedPath (juce::Image::SingleChannel, area.getWidth(), area.getHeight(), true);
juce::Graphics g2 (renderedPath);
g2.setColour (juce::Colours::white);
g2.fillPath ((spread != 0) ? spreadPath : path, juce::AffineTransform::translation ((float) (offset.x - area.getX()), (float) (offset.y - area.getY())));
applyStackBlur (renderedPath, radius);
g.setColour (color);
g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
}
juce::Path button;
button.addRoundedRectangle (getContentBounds(), 4);
renderDropShadow (g, button, juce::Colour::fromRGBA (164, 49, 255, 90), 12, { -2, -2 }, 2);
renderDropShadow (g, button, juce::Colour::fromRGBA (11, 0, 19, 200), 16, { 4, 4 }, 2);
g.setColour (juce::Colour::fromRGB (41, 11, 79));
g.fillPath (button);
@PeterEmanuel
Copy link

Hi Sudara!

I was checking my own use of GinStackBlur, which I use for blurry texts ("led-like") and I ran into an edge case where (on Mac) I kept getting an effect when lowering the radius from 3, to 2, to 1, to 0...

Which comes from the GinStackBlur source: radius = juce::jlimit (2u, 254u, radius);

So in your code above, the default radius 1 will actually become 2; just to let you know.

Cheers!
Peter Roos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment