Krita Source Code Documentation
Loading...
Searching...
No Matches
KisGradientConversion Namespace Reference

Namespace containing functions to convert to/from different types of gradients. More...

Functions

KoAbstractGradientSP toAbstractGradient (const QGradient *gradient)
 Convert the stops of a QGradient to a krita abstract gradient.
 
KoAbstractGradientSP toAbstractGradient (const QGradientStops &gradient)
 Convert a QGradientStop list to a krita abstract gradient.
 
KoAbstractGradientSP toAbstractGradient (KoSegmentGradientSP gradient)
 Create a clone of a KoSegmentGradientSP and return it casted to a krita abstract gradient.
 
KoAbstractGradientSP toAbstractGradient (KoStopGradientSP gradient)
 Create a clone of a KoStopGradientSP and return it casted to a krita abstract gradient.
 
KoGradientStop toKoGradientStop (const KoColor &color, KoGradientSegmentEndpointType type, qreal offset, KoCanvasResourcesInterfaceSP canvasResourcesInterface)
 
QGradient * toQGradient (KoAbstractGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoAbstractGradientSP to a QGradient.
 
QGradient * toQGradient (KoSegmentGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoSegmentGradientSP to a QGradient.
 
QGradient * toQGradient (KoStopGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoStopGradientSP to a QGradient.
 
QGradientStop toQGradientStop (const KoColor &color, KoGradientSegmentEndpointType type, qreal offset, KoCanvasResourcesInterfaceSP canvasResourcesInterface)
 
QGradientStop toQGradientStop (const KoColor &color, KoGradientStopType type, qreal position, KoCanvasResourcesInterfaceSP canvasResourcesInterface)
 
QGradientStops toQGradientStops (KoAbstractGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoAbstractGradientSP to a QGradientStop list.
 
QGradientStops toQGradientStops (KoSegmentGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoSegmentGradientSP to a QGradientStop list.
 
QGradientStops toQGradientStops (KoStopGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoStopGradientSP to a QGradientStop list.
 
KoSegmentGradientSP toSegmentGradient (const QGradient *gradient)
 Convert a QGradient to a krita segment gradient.
 
KoSegmentGradientSP toSegmentGradient (const QGradientStops &gradient)
 Convert a QGradientStop list to a krita segment gradient.
 
KoSegmentGradientSP toSegmentGradient (KoAbstractGradientSP gradient)
 Convert a krita abstract gradient to a krita segment gradient.
 
KoSegmentGradientSP toSegmentGradient (KoStopGradientSP gradient)
 Convert a krita stop gradient to a krita segment gradient.
 
KoStopGradientSP toStopGradient (const QGradient *gradient)
 Convert the stops of a QGradient to a KoStopGradientSP.
 
KoStopGradientSP toStopGradient (const QGradientStops &gradient)
 Convert a QGradientStop list to a KoStopGradientSP.
 
KoStopGradientSP toStopGradient (KoAbstractGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoAbstractGradientSP to a KoStopGradientSP.
 
KoStopGradientSP toStopGradient (KoSegmentGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface=nullptr)
 Convert a KoSegmentGradientSP to a KoStopGradientSP.
 

Detailed Description

Namespace containing functions to convert to/from different types of gradients.

Function Documentation

◆ toAbstractGradient() [1/4]

KRITAPIGMENT_EXPORT KoAbstractGradientSP KisGradientConversion::toAbstractGradient ( const QGradient * gradient)

Convert the stops of a QGradient to a krita abstract gradient.

This function makes use of toStopGradient(const QGradientStops &) to create a KoStopGradientSP that is casted to a KoAbstractGradientSP

Parameters
gradientA QGradient with the stops of the gradient
Returns
KoAbstractGradientSP containing stops with the positions and colors of the gradient

Definition at line 203 of file KisGradientConversion.cpp.

204 {
205 if (!gradient) {
206 return nullptr;
207 }
208 return toStopGradient(gradient).dynamicCast<KoAbstractGradient>();
209 }

References toStopGradient().

◆ toAbstractGradient() [2/4]

KRITAPIGMENT_EXPORT KoAbstractGradientSP KisGradientConversion::toAbstractGradient ( const QGradientStops & gradient)

Convert a QGradientStop list to a krita abstract gradient.

This function makes use of toStopGradient(const QGradientStops &) to create a KoStopGradientSP that is casted to a KoAbstractGradientSP

Parameters
gradientA QGradientStop list with the stops of the gradient
Returns
KoAbstractGradientSP containing stops with the positions and colors of the gradient

Definition at line 198 of file KisGradientConversion.cpp.

199 {
200 return toStopGradient(gradient).dynamicCast<KoAbstractGradient>();
201 }
KoStopGradientSP toStopGradient(const QGradientStops &gradient)
Convert a QGradientStop list to a KoStopGradientSP.

References toStopGradient().

◆ toAbstractGradient() [3/4]

KRITAPIGMENT_EXPORT KoAbstractGradientSP KisGradientConversion::toAbstractGradient ( KoSegmentGradientSP gradient)

Create a clone of a KoSegmentGradientSP and return it casted to a krita abstract gradient.

Parameters
gradientA KoSegmentGradientSP gradient to convert from
Returns
A clone of the input gradient casted to KoAbstractGradientSP

Definition at line 219 of file KisGradientConversion.cpp.

220 {
221 if (!gradient) {
222 return nullptr;
223 }
224 return gradient->clone().dynamicCast<KoAbstractGradient>();
225 }

◆ toAbstractGradient() [4/4]

KRITAPIGMENT_EXPORT KoAbstractGradientSP KisGradientConversion::toAbstractGradient ( KoStopGradientSP gradient)

Create a clone of a KoStopGradientSP and return it casted to a krita abstract gradient.

Parameters
gradientA KoStopGradientSP gradient to convert from
Returns
A clone of the input gradient casted to KoAbstractGradientSP

Definition at line 211 of file KisGradientConversion.cpp.

212 {
213 if (!gradient) {
214 return nullptr;
215 }
216 return gradient->clone().dynamicCast<KoAbstractGradient>();
217 }

◆ toKoGradientStop()

KoGradientStop KisGradientConversion::toKoGradientStop ( const KoColor & color,
KoGradientSegmentEndpointType type,
qreal offset,
KoCanvasResourcesInterfaceSP canvasResourcesInterface )

Definition at line 272 of file KisGradientConversion.cpp.

274 {
275 KoGradientStop stop;
276
277 stop.position = offset;
278
279 if (type == FOREGROUND_ENDPOINT) {
280 stop.type = FOREGROUNDSTOP;
281 stop.color = color;
282 } else if (type == FOREGROUND_TRANSPARENT_ENDPOINT) {
283 stop.type = COLORSTOP;
284 if (canvasResourcesInterface) {
285 stop.color = canvasResourcesInterface->resource(KoCanvasResource::ForegroundColor).value<KoColor>();
286 } else {
287 stop.color = color;
288 }
289 stop.color.setOpacity(static_cast<quint8>(0));
290 } else if (type == BACKGROUND_ENDPOINT) {
291 stop.type = BACKGROUNDSTOP;
292 stop.color = color;
293 } else if (type == BACKGROUND_TRANSPARENT_ENDPOINT) {
294 stop.type = COLORSTOP;
295 if (canvasResourcesInterface) {
296 stop.color = canvasResourcesInterface->resource(KoCanvasResource::BackgroundColor).value<KoColor>();
297 } else {
298 stop.color = color;
299 }
300 stop.color.setOpacity(static_cast<quint8>(0));
301 } else {
302 stop.type = COLORSTOP;
303 stop.color = color;
304 }
305
306 return stop;
307 }
@ BACKGROUND_TRANSPARENT_ENDPOINT
@ FOREGROUND_ENDPOINT
@ BACKGROUND_ENDPOINT
@ FOREGROUND_TRANSPARENT_ENDPOINT
@ FOREGROUNDSTOP
@ BACKGROUNDSTOP
@ COLORSTOP
void setOpacity(quint8 alpha)
Definition KoColor.cpp:333
@ BackgroundColor
The active background color selected for this canvas.
@ ForegroundColor
The active foreground color selected for this canvas.
KoGradientStopType type

References BACKGROUND_ENDPOINT, BACKGROUND_TRANSPARENT_ENDPOINT, KoCanvasResource::BackgroundColor, BACKGROUNDSTOP, KoGradientStop::color, COLORSTOP, FOREGROUND_ENDPOINT, FOREGROUND_TRANSPARENT_ENDPOINT, KoCanvasResource::ForegroundColor, FOREGROUNDSTOP, KoGradientStop::position, KoColor::setOpacity(), and KoGradientStop::type.

◆ toQGradient() [1/3]

KRITAPIGMENT_EXPORT QGradient * KisGradientConversion::toQGradient ( KoAbstractGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoAbstractGradientSP to a QGradient.

This function makes use of toQGradient(KoStopGradientSP, KoCanvasResourcesInterfaceSP) and toQGradient(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP)

Parameters
gradientA KoAbstractGradientSP to convert from
canvasResourcesInterfaceKoCanvasResourcesInterfaceSP used if some of the gradient stops should take the foreground/background color
Returns
QGradient containing stops with the positions and colors of the gradient

Definition at line 165 of file KisGradientConversion.cpp.

166 {
167 if (!gradient) {
168 return nullptr;
169 }
170 if (gradient.dynamicCast<KoStopGradient>()) {
171 return toQGradient(gradient.dynamicCast<KoStopGradient>(), canvasResourcesInterface);
172 } else if (gradient.dynamicCast<KoSegmentGradient>()) {
173 return toQGradient(gradient.dynamicCast<KoSegmentGradient>(), canvasResourcesInterface);
174 }
175 return nullptr;
176 }
QGradient * toQGradient(KoAbstractGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface)
Convert a KoAbstractGradientSP to a QGradient.

References toQGradient().

◆ toQGradient() [2/3]

KRITAPIGMENT_EXPORT QGradient * KisGradientConversion::toQGradient ( KoSegmentGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoSegmentGradientSP to a QGradient.

Creates a QLinearGradient and sets its stops using toQGradientStops(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP)

Parameters
gradientA KoSegmentGradientSP to convert from
canvasResourcesInterfaceKoCanvasResourcesInterfaceSP used if some of the gradient stops should take the foreground/background color
Returns
QGradient containing stops with the positions and colors of the gradient

Definition at line 188 of file KisGradientConversion.cpp.

189 {
190 if (!gradient) {
191 return nullptr;
192 }
193 QGradient *qGradient = new QLinearGradient;
194 qGradient->setStops(toQGradientStops(gradient, canvasResourcesInterface));
195 return qGradient;
196 }

References toQGradientStops().

◆ toQGradient() [3/3]

KRITAPIGMENT_EXPORT QGradient * KisGradientConversion::toQGradient ( KoStopGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoStopGradientSP to a QGradient.

This function doesn't use the stop gradient's type and start/stop positions to create different types of QGradient. This just creates a QLinearGradient and sets its stops using toQGradientStops(KoStopGradientSP, KoCanvasResourcesInterfaceSP). To get the correct type of QGradient use KoStopGradient::toQGradient()

Parameters
gradientA KoStopGradientSP to convert from
canvasResourcesInterfaceKoCanvasResourcesInterfaceSP used if some of the gradient stops should take the foreground/background color
Returns
QGradient containing stops with the positions and colors of the gradient

Definition at line 178 of file KisGradientConversion.cpp.

179 {
180 if (!gradient) {
181 return nullptr;
182 }
183 QGradient *qGradient = new QLinearGradient;
184 qGradient->setStops(toQGradientStops(gradient, canvasResourcesInterface));
185 return qGradient;
186 }

References toQGradientStops().

◆ toQGradientStop() [1/2]

QGradientStop KisGradientConversion::toQGradientStop ( const KoColor & color,
KoGradientSegmentEndpointType type,
qreal offset,
KoCanvasResourcesInterfaceSP canvasResourcesInterface )

Definition at line 82 of file KisGradientConversion.cpp.

84 {
85 QGradientStop stop;
86 stop.first = offset;
87
88 if (type == FOREGROUND_ENDPOINT) {
89 if (canvasResourcesInterface) {
90 canvasResourcesInterface->resource(KoCanvasResource::ForegroundColor).value<KoColor>().toQColor(&stop.second);
91 return stop;
92 }
93 } else if (type == FOREGROUND_TRANSPARENT_ENDPOINT) {
94 if (canvasResourcesInterface) {
95 canvasResourcesInterface->resource(KoCanvasResource::ForegroundColor).value<KoColor>().toQColor(&stop.second);
96 stop.second.setAlpha(0);
97 return stop;
98 }
99 } else if (type == BACKGROUND_ENDPOINT) {
100 if (canvasResourcesInterface) {
101 canvasResourcesInterface->resource(KoCanvasResource::BackgroundColor).value<KoColor>().toQColor(&stop.second);
102 return stop;
103 }
104 } else if (type == BACKGROUND_TRANSPARENT_ENDPOINT) {
105 if (canvasResourcesInterface) {
106 canvasResourcesInterface->resource(KoCanvasResource::BackgroundColor).value<KoColor>().toQColor(&stop.second);
107 stop.second.setAlpha(0);
108 return stop;
109 }
110 }
111
112 color.toQColor(&stop.second);
113 return stop;
114 }
void toQColor(QColor *c) const
a convenience method for the above.
Definition KoColor.cpp:198

References BACKGROUND_ENDPOINT, BACKGROUND_TRANSPARENT_ENDPOINT, KoCanvasResource::BackgroundColor, FOREGROUND_ENDPOINT, FOREGROUND_TRANSPARENT_ENDPOINT, KoCanvasResource::ForegroundColor, and KoColor::toQColor().

◆ toQGradientStop() [2/2]

QGradientStop KisGradientConversion::toQGradientStop ( const KoColor & color,
KoGradientStopType type,
qreal position,
KoCanvasResourcesInterfaceSP canvasResourcesInterface )

Definition at line 32 of file KisGradientConversion.cpp.

34 {
35 QGradientStop stop;
36 stop.first = position;
37
38 if (type == FOREGROUNDSTOP) {
39 if (canvasResourcesInterface) {
40 canvasResourcesInterface->resource(KoCanvasResource::ForegroundColor).value<KoColor>().toQColor(&stop.second);
41 return stop;
42 }
43 } else if (type == BACKGROUNDSTOP) {
44 if (canvasResourcesInterface) {
45 canvasResourcesInterface->resource(KoCanvasResource::BackgroundColor).value<KoColor>().toQColor(&stop.second);
46 return stop;
47 }
48 }
49
50 color.toQColor(&stop.second);
51 return stop;
52 }

References KoCanvasResource::BackgroundColor, BACKGROUNDSTOP, KoCanvasResource::ForegroundColor, FOREGROUNDSTOP, and KoColor::toQColor().

◆ toQGradientStops() [1/3]

KRITAPIGMENT_EXPORT QGradientStops KisGradientConversion::toQGradientStops ( KoAbstractGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoAbstractGradientSP to a QGradientStop list.

This function makes use of toQGradientStops(KoStopGradientSP, KoCanvasResourcesInterfaceSP) and toQGradientStops(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP)

Parameters
gradientA KoAbstractGradientSP to convert from
canvasResourcesInterfaceKoCanvasResourcesInterfaceSP used if some of the gradient stops should take the foreground/background color
Returns
QGradientStop list containing the positions and colors of the gradient

Definition at line 18 of file KisGradientConversion.cpp.

20 {
21 if (!gradient) {
22 return QGradientStops();
23 }
24 if (gradient.dynamicCast<KoStopGradient>()) {
25 return toQGradientStops(gradient.dynamicCast<KoStopGradient>(), canvasResourcesInterface);
26 } else if (gradient.dynamicCast<KoSegmentGradient>()) {
27 return toQGradientStops(gradient.dynamicCast<KoSegmentGradient>(), canvasResourcesInterface);
28 }
29 return QGradientStops();
30 }
QGradientStops toQGradientStops(KoAbstractGradientSP gradient, KoCanvasResourcesInterfaceSP canvasResourcesInterface)
Convert a KoAbstractGradientSP to a QGradientStop list.

References toQGradientStops().

◆ toQGradientStops() [2/3]

KRITAPIGMENT_EXPORT QGradientStops KisGradientConversion::toQGradientStops ( KoSegmentGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoSegmentGradientSP to a QGradientStop list.

For each segment in the segment gradient two QGradientStop are created, one for the start point and another one for the end point.

To convert end points that have FOREGROUND_ENDPOINT, BACKGROUND_ENDPOINT, FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT type, the canvasResourcesInterface is used. If the canvasResourcesInterface is null, the color field of the end point is used.

The opacity of the QGradientStop color is set to 0 if the end point type is FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT.

If two QGradientStop turn out to be in the same position and have the same color, they are collapsed and only one stop is added to the list (This prevents having duplicated stops due to the end point of a segment being equal to the start point of the next segment).

The middle point, interpolation and color interpolation of the segment are ignored, so some information may be lost

Parameters
gradientA KoSegmentGradientSP to convert from
canvasResourcesInterfaceKoCanvasResourcesInterfaceSP used if some of the gradient stops should take the foreground/background color
Returns
QGradientStop list containing the positions and colors of the gradient

Definition at line 116 of file KisGradientConversion.cpp.

118 {
119 QGradientStops stops;
120
121 if (!gradient) {
122 return stops;
123 }
124
125 QGradientStop lastStop;
126 lastStop.first = -1.0;
127
128 for (KoGradientSegment *segment : gradient->segments()) {
129 QGradientStop stop;
130
131 stop = toQGradientStop(
132 segment->startColor(), segment->startType(), segment->startOffset(),
133 canvasResourcesInterface
134 );
135 if (qFuzzyCompare(stop.first, lastStop.first)) {
136 if (stop.second != lastStop.second) {
137 stop.first = stop.first + 0.000001;
138 stops << stop;
139 lastStop = stop;
140 }
141 } else {
142 stops << stop;
143 lastStop = stop;
144 }
145
146 stop = toQGradientStop(
147 segment->endColor(), segment->endType(), segment->endOffset(),
148 canvasResourcesInterface
149 );
150 if (qFuzzyCompare(stop.first, lastStop.first)) {
151 if (stop.second != lastStop.second) {
152 stop.first = stop.first + 0.000001;
153 stops << stop;
154 lastStop = stop;
155 }
156 } else {
157 stops << stop;
158 lastStop = stop;
159 }
160 }
161
162 return stops;
163 }
Write API docs here.
static bool qFuzzyCompare(half p1, half p2)
QGradientStop toQGradientStop(const KoColor &color, KoGradientStopType type, qreal position, KoCanvasResourcesInterfaceSP canvasResourcesInterface)

References qFuzzyCompare(), and toQGradientStop().

◆ toQGradientStops() [3/3]

KRITAPIGMENT_EXPORT QGradientStops KisGradientConversion::toQGradientStops ( KoStopGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoStopGradientSP to a QGradientStop list.

For each stop in the stop gradient a QGradientStop is created.

To convert stops that have FOREGROUNDSTOP or BACKGROUNDSTOP type, the canvasResourcesInterface is used. If the canvasResourcesInterface is null, the color field of the stop is used

Parameters
gradientA KoStopGradientSP to convert from
canvasResourcesInterfaceKoCanvasResourcesInterfaceSP used if some of the gradient stops should take the foreground/background color
Returns
QGradientStop list containing the positions and colors of the gradient

Definition at line 54 of file KisGradientConversion.cpp.

56 {
57 QGradientStops stops;
58
59 if (!gradient) {
60 return stops;
61 }
62
63 qreal lastStopPosition = -1.0;
64 for (const KoGradientStop &stopGradientStop : gradient->stops()) {
65 if (qFuzzyCompare(stopGradientStop.position, lastStopPosition)) {
66 stops << toQGradientStop(
67 stopGradientStop.color, stopGradientStop.type, stopGradientStop.position + 0.000001,
68 canvasResourcesInterface
69 );
70 lastStopPosition = stopGradientStop.position + 0.000001;
71 } else {
72 stops << toQGradientStop(
73 stopGradientStop.color, stopGradientStop.type, stopGradientStop.position,
74 canvasResourcesInterface
75 );
76 lastStopPosition = stopGradientStop.position;
77 }
78 }
79 return stops;
80 }

References qFuzzyCompare(), and toQGradientStop().

◆ toSegmentGradient() [1/4]

KRITAPIGMENT_EXPORT KoSegmentGradientSP KisGradientConversion::toSegmentGradient ( const QGradient * gradient)

Convert a QGradient to a krita segment gradient.

Creates a segment gradient from the stops of the QGradient using toSegmentGradient(const QGradientStops &)

Parameters
gradientA QGradient with the stops of the gradient
Returns
KoSegmentGradientSP containing segments with the positions and colors of the gradient

Definition at line 381 of file KisGradientConversion.cpp.

382 {
383 if (!gradient || gradient->type() == QGradient::NoGradient) {
384 return nullptr;
385 }
386 KoSegmentGradientSP segmentGradient = toSegmentGradient(gradient->stops());
387 segmentGradient->setType(gradient->type());
388 segmentGradient->setSpread(gradient->spread());
389 return segmentGradient;
390 }

References toSegmentGradient().

◆ toSegmentGradient() [2/4]

KRITAPIGMENT_EXPORT KoSegmentGradientSP KisGradientConversion::toSegmentGradient ( const QGradientStops & gradient)

Convert a QGradientStop list to a krita segment gradient.

If two stops have the same position a new segment between them is not created

Parameters
gradientA QGradientStop list with the stops of the gradient
Returns
KoSegmentGradientSP containing segments with the positions and colors of the gradient

Definition at line 360 of file KisGradientConversion.cpp.

361 {
362 KoSegmentGradientSP segmentGradient(new KoSegmentGradient);
363 const QGradientStops &stops = gradient;
364
365 for (int i = 0; i < stops.size() - 1; ++i) {
366 if (qFuzzyCompare(stops[i].first, stops[i + 1].first)) {
367 continue;
368 }
369 segmentGradient->createSegment(
371 stops[i].first, stops[i + 1].first, (stops[i].first + stops[i + 1].first) / 2,
372 stops[i].second, stops[i + 1].second
373 );
374 }
375
376 segmentGradient->setValid(true);
377
378 return segmentGradient;
379 }
@ INTERP_LINEAR
@ COLOR_INTERP_RGB

References COLOR_INTERP_RGB, INTERP_LINEAR, and qFuzzyCompare().

◆ toSegmentGradient() [3/4]

KRITAPIGMENT_EXPORT KoSegmentGradientSP KisGradientConversion::toSegmentGradient ( KoAbstractGradientSP gradient)

Convert a krita abstract gradient to a krita segment gradient.

If the underlying gradient is a stop gradient, toSegmentGradient(KoStopGradientSP) is used; otherwise, if it is a segment gradient, a clone is returned

Parameters
gradientA KoAbstractGradientSP to convert from
Returns
KoSegmentGradientSP containing segments with the positions and colors of the gradient

Definition at line 392 of file KisGradientConversion.cpp.

393 {
394 if (!gradient) {
395 return nullptr;
396 }
397 if (gradient.dynamicCast<KoSegmentGradient>()) {
398 return gradient->clone().dynamicCast<KoSegmentGradient>();
399 } else if (gradient.dynamicCast<KoStopGradient>()) {
400 return toSegmentGradient(gradient.dynamicCast<KoStopGradient>());
401 }
402 return nullptr;
403 }
KoSegmentGradientSP toSegmentGradient(const QGradientStops &gradient)
Convert a QGradientStop list to a krita segment gradient.

References toSegmentGradient().

◆ toSegmentGradient() [4/4]

KRITAPIGMENT_EXPORT KoSegmentGradientSP KisGradientConversion::toSegmentGradient ( KoStopGradientSP gradient)

Convert a krita stop gradient to a krita segment gradient.

If two stops have the same position a new segment is not created

Parameters
gradientA KoStopGradientSP to convert from
Returns
KoSegmentGradientSP containing segments with the positions and colors of the gradient

Definition at line 405 of file KisGradientConversion.cpp.

406 {
407 if (!gradient) {
408 return nullptr;
409 }
410
411 KoSegmentGradientSP segmentGradient(new KoSegmentGradient);
412 QList<KoGradientStop> stops = gradient->stops();
413
414 for (int i = 0; i < stops.size() - 1; ++i) {
415 if (qFuzzyCompare(stops[i].position, stops[i + 1].position)) {
416 continue;
417 }
418
419 KoGradientSegmentEndpointType startType, endType;
420 const KoGradientStopType startStopType = stops[i].type;
421 const KoGradientStopType endStopType = stops[i + 1].type;
422
423 if (startStopType == FOREGROUNDSTOP) {
424 startType = FOREGROUND_ENDPOINT;
425 } else if (startStopType == BACKGROUNDSTOP) {
426 startType = BACKGROUND_ENDPOINT;
427 } else {
428 startType = COLOR_ENDPOINT;
429 }
430
431 if (endStopType == FOREGROUNDSTOP) {
432 endType = FOREGROUND_ENDPOINT;
433 } else if (endStopType == BACKGROUNDSTOP) {
434 endType = BACKGROUND_ENDPOINT;
435 } else {
436 endType = COLOR_ENDPOINT;
437 }
438
439 segmentGradient->createSegment(
441 stops[i].position, stops[i + 1].position, (stops[i].position + stops[i + 1].position) / 2,
442 stops[i].color.toQColor(), stops[i + 1].color.toQColor(),
443 startType, endType
444 );
445 }
446
447 segmentGradient->setType(gradient->type());
448 segmentGradient->setSpread(gradient->spread());
449
450 segmentGradient->setName(gradient->name());
451 segmentGradient->setFilename(gradient->filename());
452 segmentGradient->setValid(true);
453
454 return segmentGradient;
455 }
KoGradientSegmentEndpointType
@ COLOR_ENDPOINT
KoGradientStopType

References BACKGROUND_ENDPOINT, BACKGROUNDSTOP, COLOR_ENDPOINT, COLOR_INTERP_RGB, FOREGROUND_ENDPOINT, FOREGROUNDSTOP, INTERP_LINEAR, and qFuzzyCompare().

◆ toStopGradient() [1/4]

KRITAPIGMENT_EXPORT KoStopGradientSP KisGradientConversion::toStopGradient ( const QGradient * gradient)

Convert the stops of a QGradient to a KoStopGradientSP.

Parameters
gradientA QGradient with the stops of the gradient
Returns
KoStopGradientSP containing stops with the positions and colors of the gradient

Definition at line 247 of file KisGradientConversion.cpp.

248 {
249 if (!gradient || gradient->type() == QGradient::NoGradient) {
250 return nullptr;
251 }
252 KoStopGradientSP stopGradient = toStopGradient(gradient->stops());
253 stopGradient->setType(gradient->type());
254 stopGradient->setSpread(gradient->spread());
255 return stopGradient;
256 }

References toStopGradient().

◆ toStopGradient() [2/4]

KRITAPIGMENT_EXPORT KoStopGradientSP KisGradientConversion::toStopGradient ( const QGradientStops & gradient)

Convert a QGradientStop list to a KoStopGradientSP.

Parameters
gradientA QGradientStop list with the stops of the gradient
Returns
KoStopGradientSP containing stops with the positions and colors of the gradient

Definition at line 227 of file KisGradientConversion.cpp.

228 {
229 KoStopGradientSP stopGradient(new KoStopGradient);
231
232 for (const QGradientStop &qGradientStop : gradient) {
233 KoGradientStop stop;
234 stop.type = COLORSTOP;
235 stop.position = qGradientStop.first;
236 stop.color = KoColor(qGradientStop.second, stopGradient->colorSpace());
237 stops << stop;
238 }
239
240 stopGradient->setStops(stops);
241 stopGradient->setType(QGradient::LinearGradient);
242 stopGradient->setValid(true);
243
244 return stopGradient;
245 }

References KoGradientStop::color, COLORSTOP, KoGradientStop::position, and KoGradientStop::type.

◆ toStopGradient() [3/4]

KRITAPIGMENT_EXPORT KoStopGradientSP KisGradientConversion::toStopGradient ( KoAbstractGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoAbstractGradientSP to a KoStopGradientSP.

If the underlying gradient is a segment gradient, toStopGradient(KoSegmentGradientSP, KoCanvasResourcesInterfaceSP) is used; otherwise, if it is a stop gradient, a clone is returned

Parameters
gradientA KoAbstractGradientSP to convert from
Returns
KoStopGradientSP containing stops with the positions and colors of the gradient

Definition at line 258 of file KisGradientConversion.cpp.

260 {
261 if (!gradient) {
262 return nullptr;
263 }
264 if (gradient.dynamicCast<KoStopGradient>()) {
265 return gradient->clone().dynamicCast<KoStopGradient>();
266 } else if (gradient.dynamicCast<KoSegmentGradient>()) {
267 return toStopGradient(gradient.dynamicCast<KoSegmentGradient>(), canvasResourcesInterface);
268 }
269 return nullptr;
270 }

References toStopGradient().

◆ toStopGradient() [4/4]

KRITAPIGMENT_EXPORT KoStopGradientSP KisGradientConversion::toStopGradient ( KoSegmentGradientSP gradient,
KoCanvasResourcesInterfaceSP canvasResourcesInterface = nullptr )

Convert a KoSegmentGradientSP to a KoStopGradientSP.

For each segment in the segment gradient two stops are created, one for the start point and another one for the end point.

To convert end points that have FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT type, the stop type is set to the COLORSTOP and the canvasResourcesInterface is used to set the stop color if it is not null. If the canvasResourcesInterface is null, the color field of the end point is used.

The opacity of the stop color is set to 0 if the end point type is FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT.

If two stops turn out to be in the same position and have the same type and color, they are collapsed and only one stop is added to the list (This prevents having duplicated stops due to the end point of a segment being equal to the start point of the next segment).

The middle point, interpolation and color interpolation of the segment are ignored, so some information may be lost

Parameters
gradientA KoSegmentGradientSP gradient to convert from
canvasResourcesInterfaceKoCanvasResourcesInterfaceSP used if some of the gradient end points have FOREGROUND_TRANSPARENT_ENDPOINT or BACKGROUND_TRANSPARENT_ENDPOINT type
Returns
KoStopGradientSP containing stops with the positions and colors of the gradient

Definition at line 309 of file KisGradientConversion.cpp.

310 {
311 if (!gradient) {
312 return nullptr;
313 }
314
315 KoStopGradientSP stopGradient(new KoStopGradient);
317
318 KoGradientStop lastStop;
319 lastStop.position = -1.0;
320
321 for (KoGradientSegment *segment : gradient->segments()) {
322 KoGradientStop stop;
323
324 stop = toKoGradientStop(
325 segment->startColor(), segment->startType(), segment->startOffset(),
326 canvasResourcesInterface
327 );
328 stop.color.convertTo(stopGradient->colorSpace());
329 if (!qFuzzyCompare(stop.position, lastStop.position) || stop.type != lastStop.type || stop.color != lastStop.color) {
330 stops << stop;
331 lastStop.type = stop.type;
332 lastStop.color = stop.color;
333 lastStop.position = stop.position;
334 }
335
336 stop = toKoGradientStop(
337 segment->endColor(), segment->endType(), segment->endOffset(),
338 canvasResourcesInterface
339 );
340 stop.color.convertTo(stopGradient->colorSpace());
341 if (!qFuzzyCompare(stop.position, lastStop.position) || stop.type != lastStop.type || stop.color != lastStop.color) {
342 stops << stop;
343 lastStop.type = stop.type;
344 lastStop.color = stop.color;
345 lastStop.position = stop.position;
346 }
347 }
348
349 stopGradient->setType(gradient->type());
350 stopGradient->setSpread(gradient->spread());
351 stopGradient->setStops(stops);
352
353 stopGradient->setName(gradient->name());
354 stopGradient->setFilename(gradient->filename());
355 stopGradient->setValid(true);
356
357 return stopGradient;
358 }
void convertTo(const KoColorSpace *cs, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags)
Definition KoColor.cpp:136
KoGradientStop toKoGradientStop(const KoColor &color, KoGradientSegmentEndpointType type, qreal offset, KoCanvasResourcesInterfaceSP canvasResourcesInterface)

References KoGradientStop::color, KoColor::convertTo(), KoGradientStop::position, qFuzzyCompare(), toKoGradientStop(), and KoGradientStop::type.