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

Functions

KRITAFLAKE_EXPORT QColor colorAt (qreal position, const QGradientStops &stops)
 Calculates color at given position from given gradient stops.
 
KRITAFLAKE_EXPORT QGradient * convertGradient (const QGradient *gradient, QGradient::Type newType)
 Converts gradient type, preserving as much data as possible.
 
KRITAFLAKE_EXPORT QGradient * defaultGradient (QGradient::Type type, QGradient::Spread spread, const QGradientStops &stops)
 creates default gradient
 

Function Documentation

◆ colorAt()

QColor KoGradientHelper::colorAt ( qreal position,
const QGradientStops & stops )

Calculates color at given position from given gradient stops.

Definition at line 93 of file KoGradientHelper.cpp.

94{
95 if (! stops.count())
96 return QColor();
97
98 if (stops.count() == 1)
99 return stops.first().second;
100
101 QGradientStop prevStop(-1.0, QColor());
102 QGradientStop nextStop(2.0, QColor());
103 // find framing gradient stops
104 Q_FOREACH (const QGradientStop & stop, stops) {
105 if (stop.first > prevStop.first && stop.first < position)
106 prevStop = stop;
107 if (stop.first < nextStop.first && stop.first > position)
108 nextStop = stop;
109 }
110
111 QColor theColor;
112
113 if (prevStop.first < 0.0) {
114 // new stop is before the first stop
115 theColor = nextStop.second;
116 } else if (nextStop.first > 1.0) {
117 // new stop is after the last stop
118 theColor = prevStop.second;
119 } else {
120 // linear interpolate colors between framing stops
121 QColor prevColor = prevStop.second, nextColor = nextStop.second;
122 qreal colorScale = (position - prevStop.first) / (nextStop.first - prevStop.first);
123 theColor.setRedF(prevColor.redF() + colorScale *(nextColor.redF() - prevColor.redF()));
124 theColor.setGreenF(prevColor.greenF() + colorScale *(nextColor.greenF() - prevColor.greenF()));
125 theColor.setBlueF(prevColor.blueF() + colorScale *(nextColor.blueF() - prevColor.blueF()));
126 theColor.setAlphaF(prevColor.alphaF() + colorScale *(nextColor.alphaF() - prevColor.alphaF()));
127 }
128 return theColor;
129}

◆ convertGradient()

QGradient * KoGradientHelper::convertGradient ( const QGradient * gradient,
QGradient::Type newType )

Converts gradient type, preserving as much data as possible.

Definition at line 35 of file KoGradientHelper.cpp.

36{
37 QPointF start, stop;
38 // try to preserve gradient positions
39 switch (gradient->type()) {
40 case QGradient::LinearGradient: {
41 const QLinearGradient *g = static_cast<const QLinearGradient*>(gradient);
42 start = g->start();
43 stop = g->finalStop();
44 break;
45 }
46 case QGradient::RadialGradient: {
47 const QRadialGradient *g = static_cast<const QRadialGradient*>(gradient);
48 start = g->center();
49 stop = QPointF(g->radius(), 0.0);
50 break;
51 }
52 case QGradient::ConicalGradient: {
53 const QConicalGradient *g = static_cast<const QConicalGradient*>(gradient);
54 start = g->center();
55 qreal radAngle = g->angle() * M_PI / 180.0;
56 stop = QPointF(0.5 * cos(radAngle), 0.5 * sin(radAngle));
57 break;
58 }
59 default:
60 start = QPointF(0.0, 0.0);
61 stop = QPointF(0.5, 0.5);
62 }
63
64 QGradient *newGradient = 0;
65 switch (newType) {
66 case QGradient::LinearGradient:
67 newGradient = new QLinearGradient(start, stop);
68 break;
69 case QGradient::RadialGradient: {
70 QPointF diff(stop - start);
71 qreal radius = sqrt(diff.x()*diff.x() + diff.y()*diff.y());
72 newGradient = new QRadialGradient(start, radius, start);
73 break;
74 }
75 case QGradient::ConicalGradient: {
76 QPointF diff(stop - start);
77 qreal angle = atan2(diff.y(), diff.x());
78 if (angle < 0.0)
79 angle += 2 * M_PI;
80 newGradient = new QConicalGradient(start, angle * 180/M_PI);
81 break;
82 }
83 default:
84 return 0;
85 }
86 newGradient->setCoordinateMode(QGradient::ObjectBoundingMode);
87 newGradient->setSpread(gradient->spread());
88 newGradient->setStops(gradient->stops());
89
90 return newGradient;
91}
#define M_PI
Definition kis_global.h:111
KRITAIMAGE_EXPORT qreal atan2(qreal y, qreal x)
atan2 replacement

References M_PI.

◆ defaultGradient()

QGradient * KoGradientHelper::defaultGradient ( QGradient::Type type,
QGradient::Spread spread,
const QGradientStops & stops )

creates default gradient

Definition at line 12 of file KoGradientHelper.cpp.

13{
14 QGradient *gradient = 0;
15 switch (type) {
16 case QGradient::LinearGradient:
17 gradient = new QLinearGradient(QPointF(0.0, 0.5), QPointF(1, 0.5));
18 break;
19 case QGradient::RadialGradient:
20 gradient = new QRadialGradient(QPointF(0.5, 0.5), sqrt(0.5));
21 break;
22 case QGradient::ConicalGradient:
23 gradient = new QConicalGradient(QPointF(0.5, 0.5), 0.0);
24 break;
25 default:
26 return 0;
27 }
28 gradient->setCoordinateMode(QGradient::ObjectBoundingMode);
29 gradient->setSpread(spread);
30 gradient->setStops(stops);
31
32 return gradient;
33}