KDEAmbi/LinearColorSmoothing.cpp

37 lines
1.2 KiB
C++

#include "LinearColorSmoothing.h"
#include <QDebug>
LinearColorSmoothing::LinearColorSmoothing(double smoothingFactor)
: _smoothingFactor(qBound(0.0, smoothingFactor, 1.0))
{
}
QVector<QColor> LinearColorSmoothing::smooth(const QVector<QColor>& newColors)
{
if (newColors.isEmpty()) {
_previousColors.clear();
return newColors;
}
if (_previousColors.isEmpty() || _previousColors.size() != newColors.size()) {
_previousColors = newColors;
return newColors;
}
QVector<QColor> smoothedColors(newColors.size());
for (int i = 0; i < newColors.size(); ++i) {
const QColor& newColor = newColors.at(i);
const QColor& prevColor = _previousColors.at(i);
int r = static_cast<int>(newColor.red() * (1.0 - _smoothingFactor) + prevColor.red() * _smoothingFactor);
int g = static_cast<int>(newColor.green() * (1.0 - _smoothingFactor) + prevColor.green() * _smoothingFactor);
int b = static_cast<int>(newColor.blue() * (1.0 - _smoothingFactor) + prevColor.blue() * _smoothingFactor);
smoothedColors[i] = QColor(r, g, b);
}
_previousColors = smoothedColors;
return smoothedColors;
}