-
-
Save sudara/7be844196a8b811216eba0f7d37c7599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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