LMMS
Loading...
Searching...
No Matches
juce_DropShadowEffect.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29static void blurDataTriplets (uint8* d, int num, const int delta) noexcept
30{
31 uint32 last = d[0];
32 d[0] = (uint8) ((d[0] + d[delta] + 1) / 3);
33 d += delta;
34
35 num -= 2;
36
37 do
38 {
39 const uint32 newLast = d[0];
40 d[0] = (uint8) ((last + d[0] + d[delta] + 1) / 3);
41 d += delta;
42 last = newLast;
43 }
44 while (--num > 0);
45
46 d[0] = (uint8) ((last + d[0] + 1) / 3);
47}
48
49static void blurSingleChannelImage (uint8* const data, const int width, const int height,
50 const int lineStride, const int repetitions) noexcept
51{
52 jassert (width > 2 && height > 2);
53
54 for (int y = 0; y < height; ++y)
55 for (int i = repetitions; --i >= 0;)
56 blurDataTriplets (data + lineStride * y, width, 1);
57
58 for (int x = 0; x < width; ++x)
59 for (int i = repetitions; --i >= 0;)
60 blurDataTriplets (data + x, height, lineStride);
61}
62
63static void blurSingleChannelImage (Image& image, int radius)
64{
66 blurSingleChannelImage (bm.data, bm.width, bm.height, bm.lineStride, 2 * radius);
67}
68
69//==============================================================================
71 : colour (shadowColour), radius (r), offset (o)
72{
73 jassert (radius > 0);
74}
75
76void DropShadow::drawForImage (Graphics& g, const Image& srcImage) const
77{
78 jassert (radius > 0);
79
80 if (srcImage.isValid())
81 {
82 Image shadowImage (srcImage.convertedToFormat (Image::SingleChannel));
83 shadowImage.duplicateIfShared();
84
85 blurSingleChannelImage (shadowImage, radius);
86
87 g.setColour (colour);
88 g.drawImageAt (shadowImage, offset.x, offset.y, true);
89 }
90}
91
92void DropShadow::drawForPath (Graphics& g, const Path& path) const
93{
94 jassert (radius > 0);
95
96 auto area = (path.getBounds().getSmallestIntegerContainer() + offset)
97 .expanded (radius + 1)
98 .getIntersection (g.getClipBounds().expanded (radius + 1));
99
100 if (area.getWidth() > 2 && area.getHeight() > 2)
101 {
102 Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true);
103
104 {
105 Graphics g2 (renderedPath);
107 g2.fillPath (path, AffineTransform::translation ((float) (offset.x - area.getX()),
108 (float) (offset.y - area.getY())));
109 }
110
111 blurSingleChannelImage (renderedPath, radius);
112
113 g.setColour (colour);
114 g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
115 }
116}
117
119 bool isCorner, float centreX, float centreY, float edgeX, float edgeY)
120{
121 cg.point1 = area.getRelativePoint (centreX, centreY);
122 cg.point2 = area.getRelativePoint (edgeX, edgeY);
123 cg.isRadial = isCorner;
124
125 g.setGradientFill (cg);
126 g.fillRect (area);
127}
128
130{
131 ColourGradient cg (colour, 0, 0, colour.withAlpha (0.0f), 0, 0, false);
132
133 for (float i = 0.05f; i < 1.0f; i += 0.1f)
134 cg.addColour (1.0 - i, colour.withMultipliedAlpha (i * i));
135
136 const float radiusInset = (float) radius / 2.0f;
137 const float expandedRadius = (float) radius + radiusInset;
138
139 auto area = targetArea.toFloat().reduced (radiusInset) + offset.toFloat();
140
141 auto r = area.expanded (expandedRadius);
142 auto top = r.removeFromTop (expandedRadius);
143 auto bottom = r.removeFromBottom (expandedRadius);
144
145 drawShadowSection (g, cg, top.removeFromLeft (expandedRadius), true, 1.0f, 1.0f, 0, 1.0f);
146 drawShadowSection (g, cg, top.removeFromRight (expandedRadius), true, 0, 1.0f, 1.0f, 1.0f);
147 drawShadowSection (g, cg, top, false, 0, 1.0f, 0, 0);
148
149 drawShadowSection (g, cg, bottom.removeFromLeft (expandedRadius), true, 1.0f, 0, 0, 0);
150 drawShadowSection (g, cg, bottom.removeFromRight (expandedRadius), true, 0, 0, 1.0f, 0);
151 drawShadowSection (g, cg, bottom, false, 0, 0, 0, 1.0f);
152
153 drawShadowSection (g, cg, r.removeFromLeft (expandedRadius), false, 1.0f, 0, 0, 0);
154 drawShadowSection (g, cg, r.removeFromRight (expandedRadius), false, 0, 0, 1.0f, 0);
155
156 g.setColour (colour);
157 g.fillRect (area);
158}
159
160//==============================================================================
163
165{
166 shadow = newShadow;
167}
168
169void DropShadowEffect::applyEffect (Image& image, Graphics& g, float scaleFactor, float alpha)
170{
172 s.radius = roundToInt ((float) s.radius * scaleFactor);
173 s.colour = s.colour.withMultipliedAlpha (alpha);
174 s.offset.x = roundToInt ((float) s.offset.x * scaleFactor);
175 s.offset.y = roundToInt ((float) s.offset.y * scaleFactor);
176
177 s.drawForImage (g, image);
178
179 g.setOpacity (alpha);
180 g.drawImageAt (image, 0, 0);
181}
182
183} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
static AffineTransform translation(float deltaX, float deltaY) noexcept
Definition juce_AffineTransform.cpp:81
Definition juce_ColourGradient.h:38
Point< float > point2
Definition juce_ColourGradient.h:195
bool isRadial
Definition juce_ColourGradient.h:202
Point< float > point1
Definition juce_ColourGradient.h:195
int addColour(double proportionAlongGradient, Colour colour)
Definition juce_ColourGradient.cpp:113
Definition juce_Colour.h:38
DropShadowEffect()
Definition juce_DropShadowEffect.cpp:161
void setShadowProperties(const DropShadow &newShadow)
Definition juce_DropShadowEffect.cpp:164
void applyEffect(Image &sourceImage, Graphics &destContext, float scaleFactor, float alpha) override
Definition juce_DropShadowEffect.cpp:169
DropShadow shadow
Definition juce_DropShadowEffect.h:108
~DropShadowEffect() override
Definition juce_DropShadowEffect.cpp:162
Definition juce_GraphicsContext.h:45
void fillPath(const Path &path) const
Definition juce_GraphicsContext.cpp:561
void setColour(Colour newColour)
Definition juce_GraphicsContext.cpp:278
Definition juce_Image.h:310
int height
Definition juce_Image.h:356
@ readWrite
Definition juce_Image.h:316
int lineStride
Definition juce_Image.h:354
int width
Definition juce_Image.h:356
uint8 * data
Definition juce_Image.h:351
Definition juce_Image.h:58
Image convertedToFormat(PixelFormat newFormat) const
Definition juce_Image.cpp:317
void duplicateIfShared()
Definition juce_Image.cpp:288
bool isValid() const noexcept
Definition juce_Image.h:147
@ SingleChannel
Definition juce_Image.h:68
Definition juce_Path.h:65
Rectangle< float > getBounds() const noexcept
Definition juce_Path.cpp:200
Definition juce_Point.h:42
Definition juce_Rectangle.h:67
Rectangle< float > toFloat() const noexcept
Definition juce_Rectangle.h:873
Rectangle reduced(ValueType deltaX, ValueType deltaY) const noexcept
Definition juce_Rectangle.h:485
Point< ValueType > getRelativePoint(FloatType relativeX, FloatType relativeY) const noexcept
Definition juce_Rectangle.h:581
int y
Definition inflate.c:1588
unsigned d
Definition inflate.c:940
int g
Definition inflate.c:1573
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned x[BMAX+1]
Definition inflate.c:1586
static int int height
Definition pugl.h:1594
static int width
Definition pugl.h:1593
JSAMPIMAGE data
Definition jpeglib.h:945
#define jassert(expression)
const Colour white
Definition juce_Colours.h:180
Definition carla_juce.cpp:31
static void blurSingleChannelImage(uint8 *const data, const int width, const int height, const int lineStride, const int repetitions) noexcept
Definition juce_DropShadowEffect.cpp:49
unsigned int uint32
Definition juce_MathsFunctions.h:45
static void drawShadowSection(Graphics &g, ColourGradient &cg, Rectangle< float > area, bool isCorner, float centreX, float centreY, float edgeX, float edgeY)
Definition juce_DropShadowEffect.cpp:118
static void blurDataTriplets(uint8 *d, int num, const int delta) noexcept
Definition juce_DropShadowEffect.cpp:29
unsigned char uint8
Definition juce_MathsFunctions.h:37
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
@ image
Definition juce_AccessibilityRole.h:42
Definition juce_DropShadowEffect.h:36
Point< int > offset
Definition juce_DropShadowEffect.h:64
void drawForPath(Graphics &g, const Path &path) const
Definition juce_DropShadowEffect.cpp:92
void drawForImage(Graphics &g, const Image &srcImage) const
Definition juce_DropShadowEffect.cpp:76
int radius
Definition juce_DropShadowEffect.h:61
void drawForRectangle(Graphics &g, const Rectangle< int > &area) const
Definition juce_DropShadowEffect.cpp:129
DropShadow()=default
Colour colour
Definition juce_DropShadowEffect.h:58
int r
Definition crypt.c:458