Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSegmentGradient.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2000 Matthias Elter <elter@kde.org>
3 SPDX-FileCopyrightText: 2004 Boudewijn Rempt <boud@valdyas.org>
4 SPDX-FileCopyrightText: 2004 Adrian Page <adrian@pagenet.plus.com>
5 SPDX-FileCopyrightText: 2004, 2007 Sven Langkamp <sven.langkamp@gmail.com>
6 SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
7
8 SPDX-License-Identifier: LGPL-2.1-or-later
9 */
10
11#ifndef KOSEGMENTGRADIENT_H
12#define KOSEGMENTGRADIENT_H
13
14#include <QList>
15#include <QColor>
16
17#include <KoResource.h>
19#include "KoColor.h"
20
21#include <kritapigment_export.h>
22
23
24enum {
30};
31
32enum {
36};
37
38//For saving to .ggr to match GIMP format, we also have Foreground (transparent) and Background (transparent) modes, currently unused...
46
49 offset(_off), color(_color), type(_type)
50 {
51
52 }
53
54 qreal offset;
57
58
59};
60
62class KRITAPIGMENT_EXPORT KoGradientSegment
63{
64public:
65 KoGradientSegment(int interpolationType, int colorInterpolationType, KoGradientSegmentEndpoint start, KoGradientSegmentEndpoint end, qreal middleOffset);
66
67 // startOffset <= t <= endOffset
68 void colorAt(KoColor&, qreal t) const;
69
70 const KoColor& startColor() const;
71 const KoColor& endColor() const;
72 KoGradientSegmentEndpointType startType() const;
73 KoGradientSegmentEndpointType endType() const;
74
75 void setStartColor(const KoColor& color) {
76 m_start.color = color;
77 if (m_start.type == FOREGROUND_TRANSPARENT_ENDPOINT || m_start.type == BACKGROUND_TRANSPARENT_ENDPOINT) {
78 m_start.color.setOpacity(quint8(0));
79 } else if (m_start.type == FOREGROUND_ENDPOINT || m_start.type == BACKGROUND_ENDPOINT) {
80 m_start.color.setOpacity(quint8(255));
81 }
82 }
83 void setEndColor(const KoColor& color) {
84 m_end.color = color;
85 if (m_end.type == FOREGROUND_TRANSPARENT_ENDPOINT || m_end.type == BACKGROUND_TRANSPARENT_ENDPOINT) {
86 m_end.color.setOpacity(quint8(0));
87 } else if (m_end.type == FOREGROUND_ENDPOINT || m_end.type == BACKGROUND_ENDPOINT) {
88 m_end.color.setOpacity(quint8(255));
89 }
90 }
91
92 void setStartType(KoGradientSegmentEndpointType type);
93 void setEndType(KoGradientSegmentEndpointType type);
94
95 qreal startOffset() const;
96 qreal middleOffset() const;
97 qreal endOffset() const;
98
99 void setStartOffset(qreal t);
100 void setMiddleOffset(qreal t);
101 void setEndOffset(qreal t);
102
103 void setVariableColors(const KoColor& foreground, const KoColor& background);
104 bool hasVariableColors();
105
106 qreal length() {
107 return m_length;
108 }
109
110 int interpolation() const;
111 int colorInterpolation() const;
112
113 void setInterpolation(int interpolationType);
114 void setColorInterpolation(int colorInterpolationType);
115
116 void mirrorSegment();
117
118 bool isValid() const;
119
120protected:
121
123 {
124 public:
127
128 virtual void colorAt(KoColor& dst, qreal t, const KoColor& start, const KoColor& end) const = 0;
129 virtual int type() const = 0;
130 };
131
133 {
134 public:
136
137 void colorAt(KoColor& dst, qreal t, const KoColor& start, const KoColor& end) const override;
138 int type() const override {
139 return COLOR_INTERP_RGB;
140 }
141
142 private:
144
147 };
148
150 {
151 public:
153
154 void colorAt(KoColor& dst, qreal t, const KoColor& start, const KoColor& end) const override;
155 int type() const override {
156 return COLOR_INTERP_HSV_CW;
157 }
158 private:
160
163 };
164
166 {
167 public:
169
170 void colorAt(KoColor& dst, qreal t, const KoColor& start, const KoColor& end) const override;
171 int type() const override {
173 }
174 private:
176
179 };
180
182 {
183 public:
186
187 virtual qreal valueAt(qreal t, qreal middle) const = 0;
188 virtual int type() const = 0;
189 };
190
192 {
193 public:
195
196 qreal valueAt(qreal t, qreal middle) const override;
197 int type() const override {
198 return INTERP_LINEAR;
199 }
200
201 // This does the actual calculation and is made
202 // static as an optimization for the other
203 // strategies that need this for their own calculation.
204 static qreal calcValueAt(qreal t, qreal middle);
205
206 private:
208
210 };
211
213 {
214 public:
216
217 qreal valueAt(qreal t, qreal middle) const override;
218 int type() const override {
219 return INTERP_CURVED;
220 }
221 private:
223
226 };
227
229 {
230 public:
232
233 qreal valueAt(qreal t, qreal middle) const override;
234 int type() const override {
236 }
237 private:
239
241 };
242
244 {
245 public:
247
248 qreal valueAt(qreal t, qreal middle) const override;
249 int type() const override {
251 }
252 private:
254
256 };
257
259 {
260 public:
262
263 qreal valueAt(qreal t, qreal middle) const override;
264 int type() const override {
265 return INTERP_SINE;
266 }
267 private:
269
271 };
272private:
275
277 qreal m_length;
279
281 bool m_hasVariableColors = false;
282
283};
284
288class KRITAPIGMENT_EXPORT KoSegmentGradient : public KoAbstractGradient
289{
290
291public:
292 explicit KoSegmentGradient(const QString &file = QString());
293 ~KoSegmentGradient() override;
296 KoResourceSP clone() const override;
297
298 bool loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP resourcesInterface) override;
299 bool saveToDevice(QIODevice* dev) const override;
300
301 QPair<QString, QString> resourceType() const override {
302 return QPair<QString, QString>(ResourceType::Gradients, ResourceSubType::SegmentedGradients);
303 }
304
306 void colorAt(KoColor& dst, qreal t) const override;
307
308 QList<int> requiredCanvasResources() const override;
309 void bakeVariableColors(KoCanvasResourcesInterfaceSP canvasResourcesInterface) override;
310 void updateVariableColors(KoCanvasResourcesInterfaceSP canvasResourcesInterface) override;
311
317 KoGradientSegment *segmentAt(qreal t) const;
318
320 QGradient* toQGradient() const override;
321
323 QString defaultFileExtension() const override;
324
329 void toXML(QDomDocument& doc, QDomElement& gradientElt) const;
335 static KoSegmentGradient fromXML(const QDomElement& elt);
336
352 void createSegment(int interpolation, int colorInterpolation, double startOffset, double endOffset, double middleOffset,
353 const QColor & leftColor, const QColor & rightColor,
355
356 void createSegment(int interpolation, int colorInterpolation, double startOffset, double endOffset, double middleOffset,
357 const KoColor & leftColor, const KoColor & rightColor,
359
367 const QList<double> getHandlePositions() const;
368
374 const QList<double> getMiddleHandlePositions() const;
375
390 void moveSegmentStartOffset(KoGradientSegment* segment, double t);
391
406 void moveSegmentEndOffset(KoGradientSegment* segment, double t);
407
419 void moveSegmentMiddleOffset(KoGradientSegment* segment, double t);
420
426 void splitSegment(KoGradientSegment* segment);
427
433 void duplicateSegment(KoGradientSegment* segment);
434
440 void mirrorSegment(KoGradientSegment* segment);
441
449 KoGradientSegment* removeSegment(KoGradientSegment* segment);
450
461 KoGradientSegment* collapseSegment(KoGradientSegment* segment);
462
468 bool removeSegmentPossible() const;
469
470 const QList<KoGradientSegment *>& segments() const;
471 void setSegments(const QList<KoGradientSegment*> &segments);
472
473protected:
474
475 inline void pushSegment(KoGradientSegment* segment) {
476 m_segments.push_back(segment);
477 }
478
480
481private:
482 bool init();
483};
484
486
487#endif // KOSEGMENTGRADIENT_H
488
QSharedPointer< KoSegmentGradient > KoSegmentGradientSP
KoGradientSegmentEndpointType
@ COLOR_ENDPOINT
@ BACKGROUND_TRANSPARENT_ENDPOINT
@ FOREGROUND_ENDPOINT
@ BACKGROUND_ENDPOINT
@ FOREGROUND_TRANSPARENT_ENDPOINT
@ INTERP_SPHERE_DECREASING
@ INTERP_LINEAR
@ INTERP_CURVED
@ INTERP_SINE
@ INTERP_SPHERE_INCREASING
@ COLOR_INTERP_RGB
@ COLOR_INTERP_HSV_CCW
@ COLOR_INTERP_HSV_CW
PythonPluginManager * instance
void setOpacity(quint8 alpha)
Definition KoColor.cpp:333
virtual void colorAt(KoColor &dst, qreal t, const KoColor &start, const KoColor &end) const =0
static CurvedInterpolationStrategy * m_instance
static HSVCCWColorInterpolationStrategy * m_instance
static HSVCWColorInterpolationStrategy * m_instance
virtual qreal valueAt(qreal t, qreal middle) const =0
static LinearInterpolationStrategy * m_instance
static RGBColorInterpolationStrategy * m_instance
static SineInterpolationStrategy * m_instance
static SphereDecreasingInterpolationStrategy * m_instance
static SphereIncreasingInterpolationStrategy * m_instance
Write API docs here.
ColorInterpolationStrategy * m_colorInterpolator
InterpolationStrategy * m_interpolator
void setStartColor(const KoColor &color)
KoGradientSegmentEndpoint m_end
void setEndColor(const KoColor &color)
QPair< QString, QString > resourceType() const override
QList< KoGradientSegment * > m_segments
KoSegmentGradient & operator=(const KoSegmentGradient &rhs)=delete
void pushSegment(KoGradientSegment *segment)
bool loadFromDevice(QIODevice *)
const QString SegmentedGradients
const QString Gradients
KoGradientSegmentEndpoint(qreal _off, KoColor _color, KoGradientSegmentEndpointType _type)
KoGradientSegmentEndpointType type