-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQCPColorGradient.h
660 lines (617 loc) · 24.5 KB
/
QCPColorGradient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#ifndef QCPCOLORGRADIENT_H
#define QCPCOLORGRADIENT_H
#include <QMetaType>
#include <QMap>
#include <QColor>
#include <QVector>
#include <QDebug>
#include <QtMath>
class QCPRange
{
public:
double lower, upper;
QCPRange()
:lower(0),
upper(0)
{}
QCPRange(double lower, double upper)
: lower(lower),
upper(upper)
{}
bool operator==(const QCPRange& other) const { return lower == other.lower && upper == other.upper; }
bool operator!=(const QCPRange& other) const { return !(*this == other); }
QCPRange &operator+=(const double& value) { lower+=value; upper+=value; return *this; }
QCPRange &operator-=(const double& value) { lower-=value; upper-=value; return *this; }
QCPRange &operator*=(const double& value) { lower*=value; upper*=value; return *this; }
QCPRange &operator/=(const double& value) { lower/=value; upper/=value; return *this; }
friend inline const QCPRange operator+(const QCPRange&, double);
friend inline const QCPRange operator+(double, const QCPRange&);
friend inline const QCPRange operator-(const QCPRange& range, double value);
friend inline const QCPRange operator*(const QCPRange& range, double value);
friend inline const QCPRange operator*(double value, const QCPRange& range);
friend inline const QCPRange operator/(const QCPRange& range, double value);
double size() const { return upper-lower; }
double center() const { return (upper+lower)*0.5; }
void normalize() { if (lower > upper) qSwap(lower, upper); }
void expand(const QCPRange &otherRange);
void expand(double includeCoord);
QCPRange expanded(const QCPRange &otherRange) const;
QCPRange expanded(double includeCoord) const;
QCPRange bounded(double lowerBound, double upperBound) const;
QCPRange sanitizedForLogScale() const;
QCPRange sanitizedForLinScale() const;
bool contains(double value) const { return value >= lower && value <= upper; }
static bool validRange(double lower, double upper);
static bool validRange(const QCPRange &range);
static const double minRange;
static const double maxRange;
};
Q_DECLARE_TYPEINFO(QCPRange, Q_MOVABLE_TYPE);
/*! \relates QCPRange
Prints \a range in a human readable format to the qDebug output.
*/
inline QDebug operator<< (QDebug d, const QCPRange &range)
{
d.nospace() << "QCPRange(" << range.lower << ", " << range.upper << ")";
return d.space();
}
/*!
Adds \a value to both boundaries of the range.
*/
inline const QCPRange operator+(const QCPRange& range, double value)
{
QCPRange result(range);
result += value;
return result;
}
/*!
Adds \a value to both boundaries of the range.
*/
inline const QCPRange operator+(double value, const QCPRange& range)
{
QCPRange result(range);
result += value;
return result;
}
/*!
Subtracts \a value from both boundaries of the range.
*/
inline const QCPRange operator-(const QCPRange& range, double value)
{
QCPRange result(range);
result -= value;
return result;
}
/*!
Multiplies both boundaries of the range by \a value.
*/
inline const QCPRange operator*(const QCPRange& range, double value)
{
QCPRange result(range);
result *= value;
return result;
}
/*!
Multiplies both boundaries of the range by \a value.
*/
inline const QCPRange operator*(double value, const QCPRange& range)
{
QCPRange result(range);
result *= value;
return result;
}
/*!
Divides both boundaries of the range by \a value.
*/
inline const QCPRange operator/(const QCPRange& range, double value)
{
QCPRange result(range);
result /= value;
return result;
}
class QCPColorGradient
{
Q_GADGET
public:
/*!
Defines the color spaces in which color interpolation between gradient stops can be performed.
\see setColorInterpolation
*/
enum ColorInterpolation { ciRGB ///< Color channels red, green and blue are linearly interpolated
,ciHSV ///< Color channels hue, saturation and value are linearly interpolated (The hue is interpolated over the shortest angle distance)
};
Q_ENUMS(ColorInterpolation)
/*!
Defines how NaN data points shall appear in the plot.
\see setNanHandling, setNanColor
*/
enum NanHandling { nhNone ///< NaN data points are not explicitly handled and shouldn't occur in the data (this gives slight performance improvement)
,nhLowestColor ///< NaN data points appear as the lowest color defined in this QCPColorGradient
,nhHighestColor ///< NaN data points appear as the highest color defined in this QCPColorGradient
,nhTransparent ///< NaN data points appear transparent
,nhNanColor ///< NaN data points appear as the color defined with \ref setNanColor
};
Q_ENUMS(NanHandling)
/*!
Defines the available presets that can be loaded with \ref loadPreset. See the documentation
there for an image of the presets.
*/
enum GradientPreset { gpGrayscale ///< Continuous lightness from black to white (suited for non-biased data representation)
,gpHot ///< Continuous lightness from black over firey colors to white (suited for non-biased data representation)
,gpCold ///< Continuous lightness from black over icey colors to white (suited for non-biased data representation)
,gpNight ///< Continuous lightness from black over weak blueish colors to white (suited for non-biased data representation)
,gpCandy ///< Blue over pink to white
,gpGeography ///< Colors suitable to represent different elevations on geographical maps
,gpIon ///< Half hue spectrum from black over purple to blue and finally green (creates banding illusion but allows more precise magnitude estimates)
,gpThermal ///< Colors suitable for thermal imaging, ranging from dark blue over purple to orange, yellow and white
,gpPolar ///< Colors suitable to emphasize polarity around the center, with blue for negative, black in the middle and red for positive values
,gpSpectrum ///< An approximation of the visible light spectrum (creates banding illusion but allows more precise magnitude estimates)
,gpJet ///< Hue variation similar to a spectrum, often used in numerical visualization (creates banding illusion but allows more precise magnitude estimates)
,gpHues ///< Full hue cycle, with highest and lowest color red (suitable for periodic data, such as angles and phases, see \ref setPeriodic)
};
Q_ENUMS(GradientPreset)
QCPColorGradient()
: mLevelCount(350),
mColorInterpolation(ciRGB),
mNanHandling(nhNone),
mNanColor(Qt::black),
mPeriodic(false),
mColorBufferInvalidated(true)
{
mColorBuffer.fill(qRgb(0, 0, 0), mLevelCount);
}
QCPColorGradient(GradientPreset preset)
:mLevelCount(350),
mColorInterpolation(ciRGB),
mNanHandling(nhNone),
mNanColor(Qt::black),
mPeriodic(false),
mColorBufferInvalidated(true)
{
mColorBuffer.fill(qRgb(0, 0, 0), mLevelCount);
loadPreset(preset);
}
bool operator==(const QCPColorGradient &other) const
{
return ((other.mLevelCount == this->mLevelCount) &&
(other.mColorInterpolation == this->mColorInterpolation) &&
(other.mNanHandling == this ->mNanHandling) &&
(other.mNanColor == this->mNanColor) &&
(other.mPeriodic == this->mPeriodic) &&
(other.mColorStops == this->mColorStops));
}
bool operator!=(const QCPColorGradient &other) const { return !(*this == other); }
// getters:
int levelCount() const { return mLevelCount; }
QMap<double, QColor> colorStops() const { return mColorStops; }
ColorInterpolation colorInterpolation() const { return mColorInterpolation; }
NanHandling nanHandling() const { return mNanHandling; }
QColor nanColor() const { return mNanColor; }
bool periodic() const { return mPeriodic; }
// setters:
void setLevelCount(int n)
{
if (n < 2)
{
qDebug() << Q_FUNC_INFO << "n must be greater or equal 2 but was" << n;
n = 2;
}
if (n != mLevelCount)
{
mLevelCount = n;
mColorBufferInvalidated = true;
}
}
void setColorStops(const QMap<double, QColor> &colorStops)
{
mColorStops = colorStops;
mColorBufferInvalidated = true;
}
void setColorStopAt(double position, const QColor &color)
{
mColorStops.insert(position, color);
mColorBufferInvalidated = true;
}
void setColorInterpolation(ColorInterpolation interpolation)
{
if (interpolation != mColorInterpolation)
{
mColorInterpolation = interpolation;
mColorBufferInvalidated = true;
}
}
void setNanHandling(NanHandling handling)
{
mNanHandling = handling;
}
void setNanColor(const QColor &color)
{
mNanColor = color;
}
void setPeriodic(bool enabled)
{
mPeriodic = enabled;
}
// non-property methods:
void colorize(const double *data, const QCPRange &range, QRgb *scanLine, int n, int dataIndexFactor=1, bool logarithmic=false)
{
// If you change something here, make sure to also adapt color() and the other colorize() overload
if (!data)
{
qDebug() << Q_FUNC_INFO << "null pointer given as data";
return;
}
if (!scanLine)
{
qDebug() << Q_FUNC_INFO << "null pointer given as scanLine";
return;
}
if (mColorBufferInvalidated)
updateColorBuffer();
const bool skipNanCheck = mNanHandling == nhNone;
const double posToIndexFactor = !logarithmic ? (mLevelCount-1)/range.size() : (mLevelCount-1)/qLn(range.upper/range.lower);
for (int i=0; i<n; ++i)
{
const double value = data[dataIndexFactor*i];
if (skipNanCheck || !std::isnan(value))
{
int index = int((!logarithmic ? value-range.lower : qLn(value/range.lower)) * posToIndexFactor);
if (!mPeriodic)
{
index = qBound(0, index, mLevelCount-1);
} else
{
index %= mLevelCount;
if (index < 0)
index += mLevelCount;
}
scanLine[i] = mColorBuffer.at(index);
} else
{
switch(mNanHandling)
{
case nhLowestColor: scanLine[i] = mColorBuffer.first(); break;
case nhHighestColor: scanLine[i] = mColorBuffer.last(); break;
case nhTransparent: scanLine[i] = qRgba(0, 0, 0, 0); break;
case nhNanColor: scanLine[i] = mNanColor.rgba(); break;
case nhNone: break; // shouldn't happen
}
}
}
}
void colorize(const double *data, const unsigned char *alpha, const QCPRange &range, QRgb *scanLine, int n, int dataIndexFactor=1, bool logarithmic=false)
{
// If you change something here, make sure to also adapt color() and the other colorize() overload
if (!data)
{
qDebug() << Q_FUNC_INFO << "null pointer given as data";
return;
}
if (!alpha)
{
qDebug() << Q_FUNC_INFO << "null pointer given as alpha";
return;
}
if (!scanLine)
{
qDebug() << Q_FUNC_INFO << "null pointer given as scanLine";
return;
}
if (mColorBufferInvalidated)
updateColorBuffer();
const bool skipNanCheck = mNanHandling == nhNone;
const double posToIndexFactor = !logarithmic ? (mLevelCount-1)/range.size() : (mLevelCount-1)/qLn(range.upper/range.lower);
for (int i=0; i<n; ++i)
{
const double value = data[dataIndexFactor*i];
if (skipNanCheck || !std::isnan(value))
{
int index = int((!logarithmic ? value-range.lower : qLn(value/range.lower)) * posToIndexFactor);
if (!mPeriodic)
{
index = qBound(0, index, mLevelCount-1);
} else
{
index %= mLevelCount;
if (index < 0)
index += mLevelCount;
}
if (alpha[dataIndexFactor*i] == 255)
{
scanLine[i] = mColorBuffer.at(index);
} else
{
const QRgb rgb = mColorBuffer.at(index);
const float alphaF = alpha[dataIndexFactor*i]/255.0f;
scanLine[i] = qRgba(int(qRed(rgb)*alphaF), int(qGreen(rgb)*alphaF), int(qBlue(rgb)*alphaF), int(qAlpha(rgb)*alphaF)); // also multiply r,g,b with alpha, to conform to Format_ARGB32_Premultiplied
}
} else
{
switch(mNanHandling)
{
case nhLowestColor: scanLine[i] = mColorBuffer.first(); break;
case nhHighestColor: scanLine[i] = mColorBuffer.last(); break;
case nhTransparent: scanLine[i] = qRgba(0, 0, 0, 0); break;
case nhNanColor: scanLine[i] = mNanColor.rgba(); break;
case nhNone: break; // shouldn't happen
}
}
}
}
QRgb color(double position, const QCPRange &range, bool logarithmic=false)
{
// If you change something here, make sure to also adapt ::colorize()
if (mColorBufferInvalidated)
updateColorBuffer();
const bool skipNanCheck = mNanHandling == nhNone;
if (!skipNanCheck && std::isnan(position))
{
switch(mNanHandling)
{
case nhLowestColor: return mColorBuffer.first();
case nhHighestColor: return mColorBuffer.last();
case nhTransparent: return qRgba(0, 0, 0, 0);
case nhNanColor: return mNanColor.rgba();
case nhNone: return qRgba(0, 0, 0, 0); // shouldn't happen
}
}
const double posToIndexFactor = !logarithmic ? (mLevelCount-1)/range.size() : (mLevelCount-1)/qLn(range.upper/range.lower);
int index = int((!logarithmic ? position-range.lower : qLn(position/range.lower)) * posToIndexFactor);
if (!mPeriodic)
{
index = qBound(0, index, mLevelCount-1);
} else
{
index %= mLevelCount;
if (index < 0)
index += mLevelCount;
}
return mColorBuffer.at(index);
}
void loadPreset(GradientPreset preset)
{
clearColorStops();
switch (preset)
{
case gpGrayscale:
setColorInterpolation(ciRGB);
setColorStopAt(0, Qt::black);
setColorStopAt(1, Qt::white);
break;
case gpHot:
setColorInterpolation(ciRGB);
setColorStopAt(0, QColor(50, 0, 0));
setColorStopAt(0.2, QColor(180, 10, 0));
setColorStopAt(0.4, QColor(245, 50, 0));
setColorStopAt(0.6, QColor(255, 150, 10));
setColorStopAt(0.8, QColor(255, 255, 50));
setColorStopAt(1, QColor(255, 255, 255));
break;
case gpCold:
setColorInterpolation(ciRGB);
setColorStopAt(0, QColor(0, 0, 50));
setColorStopAt(0.2, QColor(0, 10, 180));
setColorStopAt(0.4, QColor(0, 50, 245));
setColorStopAt(0.6, QColor(10, 150, 255));
setColorStopAt(0.8, QColor(50, 255, 255));
setColorStopAt(1, QColor(255, 255, 255));
break;
case gpNight:
setColorInterpolation(ciHSV);
setColorStopAt(0, QColor(10, 20, 30));
setColorStopAt(1, QColor(250, 255, 250));
break;
case gpCandy:
setColorInterpolation(ciHSV);
setColorStopAt(0, QColor(0, 0, 255));
setColorStopAt(1, QColor(255, 250, 250));
break;
case gpGeography:
setColorInterpolation(ciRGB);
setColorStopAt(0, QColor(70, 170, 210));
setColorStopAt(0.20, QColor(90, 160, 180));
setColorStopAt(0.25, QColor(45, 130, 175));
setColorStopAt(0.30, QColor(100, 140, 125));
setColorStopAt(0.5, QColor(100, 140, 100));
setColorStopAt(0.6, QColor(130, 145, 120));
setColorStopAt(0.7, QColor(140, 130, 120));
setColorStopAt(0.9, QColor(180, 190, 190));
setColorStopAt(1, QColor(210, 210, 230));
break;
case gpIon:
setColorInterpolation(ciHSV);
setColorStopAt(0, QColor(50, 10, 10));
setColorStopAt(0.45, QColor(0, 0, 255));
setColorStopAt(0.8, QColor(0, 255, 255));
setColorStopAt(1, QColor(0, 255, 0));
break;
case gpThermal:
setColorInterpolation(ciRGB);
setColorStopAt(0, QColor(0, 0, 50));
setColorStopAt(0.15, QColor(20, 0, 120));
setColorStopAt(0.33, QColor(200, 30, 140));
setColorStopAt(0.6, QColor(255, 100, 0));
setColorStopAt(0.85, QColor(255, 255, 40));
setColorStopAt(1, QColor(255, 255, 255));
break;
case gpPolar:
setColorInterpolation(ciRGB);
setColorStopAt(0, QColor(50, 255, 255));
setColorStopAt(0.18, QColor(10, 70, 255));
setColorStopAt(0.28, QColor(10, 10, 190));
setColorStopAt(0.5, QColor(0, 0, 0));
setColorStopAt(0.72, QColor(190, 10, 10));
setColorStopAt(0.82, QColor(255, 70, 10));
setColorStopAt(1, QColor(255, 255, 50));
break;
case gpSpectrum:
setColorInterpolation(ciHSV);
setColorStopAt(0, QColor(50, 0, 50));
setColorStopAt(0.15, QColor(0, 0, 255));
setColorStopAt(0.35, QColor(0, 255, 255));
setColorStopAt(0.6, QColor(255, 255, 0));
setColorStopAt(0.75, QColor(255, 30, 0));
setColorStopAt(1, QColor(50, 0, 0));
break;
case gpJet:
setColorInterpolation(ciRGB);
setColorStopAt(0, QColor(0, 0, 100));
setColorStopAt(0.15, QColor(0, 50, 255));
setColorStopAt(0.35, QColor(0, 255, 255));
setColorStopAt(0.65, QColor(255, 255, 0));
setColorStopAt(0.85, QColor(255, 30, 0));
setColorStopAt(1, QColor(100, 0, 0));
break;
case gpHues:
setColorInterpolation(ciHSV);
setColorStopAt(0, QColor(255, 0, 0));
setColorStopAt(1.0/3.0, QColor(0, 0, 255));
setColorStopAt(2.0/3.0, QColor(0, 255, 0));
setColorStopAt(1, QColor(255, 0, 0));
break;
}
}
void clearColorStops()
{
mColorStops.clear();
mColorBufferInvalidated = true;
}
QCPColorGradient inverted() const
{
QCPColorGradient result(*this);
result.clearColorStops();
for (QMap<double, QColor>::const_iterator it=mColorStops.constBegin(); it!=mColorStops.constEnd(); ++it)
result.setColorStopAt(1.0-it.key(), it.value());
return result;
}
protected:
// property members:
int mLevelCount;
QMap<double, QColor> mColorStops;
ColorInterpolation mColorInterpolation;
NanHandling mNanHandling;
QColor mNanColor;
bool mPeriodic;
// non-property members:
QVector<QRgb> mColorBuffer; // have colors premultiplied with alpha (for usage with QImage::Format_ARGB32_Premultiplied)
bool mColorBufferInvalidated;
// non-virtual methods:
bool stopsUseAlpha() const
{
for (QMap<double, QColor>::const_iterator it=mColorStops.constBegin(); it!=mColorStops.constEnd(); ++it)
{
if (it.value().alpha() < 255)
return true;
}
return false;
}
void updateColorBuffer()
{
if (mColorBuffer.size() != mLevelCount)
mColorBuffer.resize(mLevelCount);
if (mColorStops.size() > 1)
{
double indexToPosFactor = 1.0/double(mLevelCount-1);
const bool useAlpha = stopsUseAlpha();
for (int i=0; i<mLevelCount; ++i)
{
double position = i*indexToPosFactor;
QMap<double, QColor>::const_iterator it = mColorStops.lowerBound(position);
if (it == mColorStops.constEnd()) // position is on or after last stop, use color of last stop
{
if (useAlpha)
{
const QColor col = std::prev(it).value();
const double alphaPremultiplier = col.alpha()/255.0; // since we use QImage::Format_ARGB32_Premultiplied
mColorBuffer[i] = qRgba(int(col.red()*alphaPremultiplier),
int(col.green()*alphaPremultiplier),
int(col.blue()*alphaPremultiplier),
col.alpha());
} else
mColorBuffer[i] = std::prev(it).value().rgba();
} else if (it == mColorStops.constBegin()) // position is on or before first stop, use color of first stop
{
if (useAlpha)
{
const QColor &col = it.value();
const double alphaPremultiplier = col.alpha()/255.0; // since we use QImage::Format_ARGB32_Premultiplied
mColorBuffer[i] = qRgba(int(col.red()*alphaPremultiplier),
int(col.green()*alphaPremultiplier),
int(col.blue()*alphaPremultiplier),
col.alpha());
} else
mColorBuffer[i] = it.value().rgba();
} else // position is in between stops (or on an intermediate stop), interpolate color
{
QMap<double, QColor>::const_iterator high = it;
QMap<double, QColor>::const_iterator low = std::prev(it);
double t = (position-low.key())/(high.key()-low.key()); // interpolation factor 0..1
switch (mColorInterpolation)
{
case ciRGB:
{
if (useAlpha)
{
const int alpha = int((1-t)*low.value().alpha() + t*high.value().alpha());
const double alphaPremultiplier = alpha/255.0; // since we use QImage::Format_ARGB32_Premultiplied
mColorBuffer[i] = qRgba(int( ((1-t)*low.value().red() + t*high.value().red())*alphaPremultiplier ),
int( ((1-t)*low.value().green() + t*high.value().green())*alphaPremultiplier ),
int( ((1-t)*low.value().blue() + t*high.value().blue())*alphaPremultiplier ),
alpha);
} else
{
mColorBuffer[i] = qRgb(int( ((1-t)*low.value().red() + t*high.value().red()) ),
int( ((1-t)*low.value().green() + t*high.value().green()) ),
int( ((1-t)*low.value().blue() + t*high.value().blue())) );
}
break;
}
case ciHSV:
{
QColor lowHsv = low.value().toHsv();
QColor highHsv = high.value().toHsv();
double hue = 0;
double hueDiff = highHsv.hueF()-lowHsv.hueF();
if (hueDiff > 0.5)
hue = lowHsv.hueF() - t*(1.0-hueDiff);
else if (hueDiff < -0.5)
hue = lowHsv.hueF() + t*(1.0+hueDiff);
else
hue = lowHsv.hueF() + t*hueDiff;
if (hue < 0) hue += 1.0;
else if (hue >= 1.0) hue -= 1.0;
if (useAlpha)
{
const QRgb rgb = QColor::fromHsvF(hue,
(1-t)*lowHsv.saturationF() + t*highHsv.saturationF(),
(1-t)*lowHsv.valueF() + t*highHsv.valueF()).rgb();
const double alpha = (1-t)*lowHsv.alphaF() + t*highHsv.alphaF();
mColorBuffer[i] = qRgba(int(qRed(rgb)*alpha), int(qGreen(rgb)*alpha), int(qBlue(rgb)*alpha), int(255*alpha));
}
else
{
mColorBuffer[i] = QColor::fromHsvF(hue,
(1-t)*lowHsv.saturationF() + t*highHsv.saturationF(),
(1-t)*lowHsv.valueF() + t*highHsv.valueF()).rgb();
}
break;
}
}
}
}
} else if (mColorStops.size() == 1)
{
const QRgb rgb = mColorStops.constBegin().value().rgb();
const double alpha = mColorStops.constBegin().value().alphaF();
mColorBuffer.fill(qRgba(int(qRed(rgb)*alpha), int(qGreen(rgb)*alpha), int(qBlue(rgb)*alpha), int(255*alpha)));
} else // mColorStops is empty, fill color buffer with black
{
mColorBuffer.fill(qRgb(0, 0, 0));
}
mColorBufferInvalidated = false;
}
};
Q_DECLARE_METATYPE(QCPColorGradient::ColorInterpolation)
Q_DECLARE_METATYPE(QCPColorGradient::NanHandling)
Q_DECLARE_METATYPE(QCPColorGradient::GradientPreset)
#endif // QCPCOLORGRADIENT_H