LMMS
Loading...
Searching...
No Matches
juce_ColourGradient.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
30{
31 #if JUCE_DEBUG
32 point1.setX (987654.0f);
33 #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED jassert (point1.x != 987654.0f);
34 #else
35 #define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED
36 #endif
37}
38
40 : point1 (other.point1), point2 (other.point2), isRadial (other.isRadial), colours (other.colours)
41{}
42
44 : point1 (other.point1), point2 (other.point2), isRadial (other.isRadial),
45 colours (std::move (other.colours))
46{}
47
48ColourGradient& ColourGradient::operator= (const ColourGradient& other)
49{
50 point1 = other.point1;
51 point2 = other.point2;
52 isRadial = other.isRadial;
53 colours = other.colours;
54 return *this;
55}
56
57ColourGradient& ColourGradient::operator= (ColourGradient&& other) noexcept
58{
59 point1 = other.point1;
60 point2 = other.point2;
61 isRadial = other.isRadial;
62 colours = std::move (other.colours);
63 return *this;
64}
65
66ColourGradient::ColourGradient (Colour colour1, float x1, float y1,
67 Colour colour2, float x2, float y2, bool radial)
68 : ColourGradient (colour1, Point<float> (x1, y1),
69 colour2, Point<float> (x2, y2), radial)
70{
71}
72
74 Colour colour2, Point<float> p2, bool radial)
75 : point1 (p1),
76 point2 (p2),
77 isRadial (radial)
78{
79 colours.add (ColourPoint { 0.0, colour1 },
80 ColourPoint { 1.0, colour2 });
81}
82
84
86{
87 return { c1, 0, y1, c2, 0, y2, false };
88}
89
91{
92 return { c1, x1, 0, c2, x2, 0, false };
93}
94
95bool ColourGradient::operator== (const ColourGradient& other) const noexcept
96{
97 return point1 == other.point1 && point2 == other.point2
98 && isRadial == other.isRadial
99 && colours == other.colours;
100}
101
102bool ColourGradient::operator!= (const ColourGradient& other) const noexcept
103{
104 return ! operator== (other);
105}
106
107//==============================================================================
109{
110 colours.clear();
111}
112
113int ColourGradient::addColour (const double proportionAlongGradient, Colour colour)
114{
115 // must be within the two end-points
116 jassert (proportionAlongGradient >= 0 && proportionAlongGradient <= 1.0);
117
118 if (proportionAlongGradient <= 0)
119 {
120 colours.set (0, { 0.0, colour });
121 return 0;
122 }
123
124 auto pos = jmin (1.0, proportionAlongGradient);
125
126 int i;
127 for (i = 0; i < colours.size(); ++i)
128 if (colours.getReference(i).position > pos)
129 break;
130
131 colours.insert (i, { pos, colour });
132 return i;
133}
134
136{
137 jassert (index > 0 && index < colours.size() - 1);
138 colours.remove (index);
139}
140
141void ColourGradient::multiplyOpacity (const float multiplier) noexcept
142{
143 for (auto& c : colours)
144 c.colour = c.colour.withMultipliedAlpha (multiplier);
145}
146
147//==============================================================================
149{
150 return colours.size();
151}
152
153double ColourGradient::getColourPosition (int index) const noexcept
154{
155 if (isPositiveAndBelow (index, colours.size()))
156 return colours.getReference (index).position;
157
158 return 0;
159 }
160
161Colour ColourGradient::getColour (int index) const noexcept
162{
163 if (isPositiveAndBelow (index, colours.size()))
164 return colours.getReference (index).colour;
165
166 return {};
167}
168
169void ColourGradient::setColour (int index, Colour newColour) noexcept
170{
171 if (isPositiveAndBelow (index, colours.size()))
172 colours.getReference (index).colour = newColour;
173}
174
175Colour ColourGradient::getColourAtPosition (double position) const noexcept
176{
177 jassert (colours.getReference(0).position == 0.0); // the first colour specified has to go at position 0
178
179 if (position <= 0 || colours.size() <= 1)
180 return colours.getReference(0).colour;
181
182 int i = colours.size() - 1;
183 while (position < colours.getReference(i).position)
184 --i;
185
186 auto& p1 = colours.getReference (i);
187
188 if (i >= colours.size() - 1)
189 return p1.colour;
190
191 auto& p2 = colours.getReference (i + 1);
192
193 return p1.colour.interpolatedWith (p2.colour, (float) ((position - p1.position) / (p2.position - p1.position)));
194}
195
196//==============================================================================
197void ColourGradient::createLookupTable (PixelARGB* const lookupTable, const int numEntries) const noexcept
198{
199 JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
200 jassert (colours.size() >= 2);
201 jassert (numEntries > 0);
202 jassert (colours.getReference(0).position == 0.0); // The first colour specified has to go at position 0
203
204 auto pix1 = colours.getReference (0).colour.getPixelARGB();
205 int index = 0;
206
207 for (int j = 1; j < colours.size(); ++j)
208 {
209 auto& p = colours.getReference (j);
210 auto numToDo = roundToInt (p.position * (numEntries - 1)) - index;
211 auto pix2 = p.colour.getPixelARGB();
212
213 for (int i = 0; i < numToDo; ++i)
214 {
215 jassert (index >= 0 && index < numEntries);
216
217 lookupTable[index] = pix1;
218 lookupTable[index].tween (pix2, (uint32) ((i << 8) / numToDo));
219 ++index;
220 }
221
222 pix1 = pix2;
223 }
224
225 while (index < numEntries)
226 lookupTable [index++] = pix1;
227}
228
230{
231 JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED // Trying to use this object without setting its coordinates?
232 jassert (colours.size() >= 2);
233
234 auto numEntries = jlimit (1, jmax (1, (colours.size() - 1) << 8),
235 3 * (int) point1.transformedBy (transform)
236 .getDistanceFrom (point2.transformedBy (transform)));
237 lookupTable.malloc (numEntries);
238 createLookupTable (lookupTable, numEntries);
239 return numEntries;
240}
241
243{
244 for (auto& c : colours)
245 if (! c.colour.isOpaque())
246 return false;
247
248 return true;
249}
250
252{
253 for (auto& c : colours)
254 if (! c.colour.isTransparent())
255 return false;
256
257 return true;
258}
259
260bool ColourGradient::ColourPoint::operator== (ColourPoint other) const noexcept
261{
262 return position == other.position && colour == other.colour;
263}
264
265bool ColourGradient::ColourPoint::operator!= (ColourPoint other) const noexcept
266{
267 return position != other.position || colour != other.colour;
268}
269
270} // namespace juce
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_AffineTransform.h:43
static ColourGradient horizontal(Colour colour1, float x1, Colour colour2, float x2)
Definition juce_ColourGradient.cpp:90
ColourGradient() noexcept
Definition juce_ColourGradient.cpp:29
int createLookupTable(const AffineTransform &transform, HeapBlock< PixelARGB > &resultLookupTable) const
Definition juce_ColourGradient.cpp:229
Colour getColour(int index) const noexcept
Definition juce_ColourGradient.cpp:161
bool isOpaque() const noexcept
Definition juce_ColourGradient.cpp:242
Point< float > point2
Definition juce_ColourGradient.h:195
static ColourGradient vertical(Colour colour1, float y1, Colour colour2, float y2)
Definition juce_ColourGradient.cpp:85
Colour getColourAtPosition(double position) const noexcept
Definition juce_ColourGradient.cpp:175
void removeColour(int index)
Definition juce_ColourGradient.cpp:135
bool isRadial
Definition juce_ColourGradient.h:202
int getNumColours() const noexcept
Definition juce_ColourGradient.cpp:148
Array< ColourPoint > colours
Definition juce_ColourGradient.h:219
bool isInvisible() const noexcept
Definition juce_ColourGradient.cpp:251
void multiplyOpacity(float multiplier) noexcept
Definition juce_ColourGradient.cpp:141
void setColour(int index, Colour newColour) noexcept
Definition juce_ColourGradient.cpp:169
void clearColours()
Definition juce_ColourGradient.cpp:108
~ColourGradient()
Definition juce_ColourGradient.cpp:83
Point< float > point1
Definition juce_ColourGradient.h:195
int addColour(double proportionAlongGradient, Colour colour)
Definition juce_ColourGradient.cpp:113
double getColourPosition(int index) const noexcept
Definition juce_ColourGradient.cpp:153
Definition juce_Colour.h:38
Definition juce_HeapBlock.h:87
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Definition juce_HeapBlock.h:252
Definition juce_PixelFormats.h:59
Definition juce_Point.h:42
register unsigned j
Definition inflate.c:1576
register unsigned i
Definition inflate.c:1575
static void c2(register WDL_FFT_COMPLEX *a)
Definition fft.c:270
#define JUCE_COLOURGRADIENT_CHECK_COORDS_INITIALISED
#define jassert(expression)
Definition carla_juce.cpp:31
unsigned int uint32
Definition juce_MathsFunctions.h:45
constexpr Type jmax(Type a, Type b)
Definition juce_MathsFunctions.h:94
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Definition juce_MathsFunctions.h:279
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
#define false
Definition ordinals.h:83
Definition juce_ColourGradient.h:211
Colour colour
Definition juce_ColourGradient.h:216
double position
Definition juce_ColourGradient.h:215
uch * p
Definition crypt.c:594
return c
Definition crypt.c:175
#define const
Definition zconf.h:137