! \internal Updates the internal color buffer which will be used by \ref colorize and \ref color, to quickly convert positions to colors. This is where the interpolation between color stops is calculated. */
| 17026 | convert positions to colors. This is where the interpolation between color stops is calculated. |
| 17027 | */ |
| 17028 | void QCPColorGradient::updateColorBuffer() |
| 17029 | { |
| 17030 | if (mColorBuffer.size() != mLevelCount) |
| 17031 | mColorBuffer.resize(mLevelCount); |
| 17032 | if (mColorStops.size() > 1) |
| 17033 | { |
| 17034 | double indexToPosFactor = 1.0/double(mLevelCount-1); |
| 17035 | const bool useAlpha = stopsUseAlpha(); |
| 17036 | for (int i=0; i<mLevelCount; ++i) |
| 17037 | { |
| 17038 | double position = i*indexToPosFactor; |
| 17039 | QMap<double, QColor>::const_iterator it = const_cast<const QMap<double, QColor>*>(&mColorStops)->lowerBound(position); // force using the const lowerBound method |
| 17040 | if (it == mColorStops.constEnd()) // position is on or after last stop, use color of last stop |
| 17041 | { |
| 17042 | if (useAlpha) |
| 17043 | { |
| 17044 | const QColor col = std::prev(it).value(); |
| 17045 | const double alphaPremultiplier = col.alpha()/255.0; // since we use QImage::Format_ARGB32_Premultiplied |
| 17046 | mColorBuffer[i] = qRgba(int(col.red()*alphaPremultiplier), |
| 17047 | int(col.green()*alphaPremultiplier), |
| 17048 | int(col.blue()*alphaPremultiplier), |
| 17049 | col.alpha()); |
| 17050 | } else |
| 17051 | mColorBuffer[i] = std::prev(it).value().rgba(); |
| 17052 | } else if (it == mColorStops.constBegin()) // position is on or before first stop, use color of first stop |
| 17053 | { |
| 17054 | if (useAlpha) |
| 17055 | { |
| 17056 | const QColor &col = it.value(); |
| 17057 | const double alphaPremultiplier = col.alpha()/255.0; // since we use QImage::Format_ARGB32_Premultiplied |
| 17058 | mColorBuffer[i] = qRgba(int(col.red()*alphaPremultiplier), |
| 17059 | int(col.green()*alphaPremultiplier), |
| 17060 | int(col.blue()*alphaPremultiplier), |
| 17061 | col.alpha()); |
| 17062 | } else |
| 17063 | mColorBuffer[i] = it.value().rgba(); |
| 17064 | } else // position is in between stops (or on an intermediate stop), interpolate color |
| 17065 | { |
| 17066 | QMap<double, QColor>::const_iterator high = it; |
| 17067 | QMap<double, QColor>::const_iterator low = std::prev(it); |
| 17068 | double t = (position-low.key())/(high.key()-low.key()); // interpolation factor 0..1 |
| 17069 | switch (mColorInterpolation) |
| 17070 | { |
| 17071 | case ciRGB: |
| 17072 | { |
| 17073 | if (useAlpha) |
| 17074 | { |
| 17075 | const int alpha = int((1-t)*low.value().alpha() + t*high.value().alpha()); |
| 17076 | const double alphaPremultiplier = alpha/255.0; // since we use QImage::Format_ARGB32_Premultiplied |
| 17077 | mColorBuffer[i] = qRgba(int( ((1-t)*low.value().red() + t*high.value().red())*alphaPremultiplier ), |
| 17078 | int( ((1-t)*low.value().green() + t*high.value().green())*alphaPremultiplier ), |
| 17079 | int( ((1-t)*low.value().blue() + t*high.value().blue())*alphaPremultiplier ), |
| 17080 | alpha); |
| 17081 | } else |
| 17082 | { |
| 17083 | mColorBuffer[i] = qRgb(int( ((1-t)*low.value().red() + t*high.value().red()) ), |
| 17084 | int( ((1-t)*low.value().green() + t*high.value().green()) ), |
| 17085 | int( ((1-t)*low.value().blue() + t*high.value().blue())) ); |