forked from quickshell/quickshell
fix: additional pr requests
This commit is contained in:
parent
b7eb562abc
commit
a84a6726cb
2 changed files with 10 additions and 12 deletions
|
@ -17,18 +17,14 @@ ColorQuantizerOperation::ColorQuantizerOperation(QUrl* source, qreal depth, qrea
|
||||||
, maxDepth(depth)
|
, maxDepth(depth)
|
||||||
, rescaleSize(rescaleSize) {
|
, rescaleSize(rescaleSize) {
|
||||||
setAutoDelete(false);
|
setAutoDelete(false);
|
||||||
colors = QList<QColor>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColorQuantizerOperation::quantizeImage(const QAtomicInteger<bool>& shouldCancel) {
|
void ColorQuantizerOperation::quantizeImage(const QAtomicInteger<bool>& shouldCancel) {
|
||||||
if (shouldCancel.loadAcquire()) return;
|
if (shouldCancel.loadAcquire() || source->isEmpty()) return;
|
||||||
|
|
||||||
colors.clear();
|
colors.clear();
|
||||||
|
|
||||||
if (source->isEmpty()) return;
|
|
||||||
|
|
||||||
auto image = QImage(source->toLocalFile());
|
auto image = QImage(source->toLocalFile());
|
||||||
|
|
||||||
if ((image.width() > rescaleSize || image.height() > rescaleSize) && rescaleSize > 0) {
|
if ((image.width() > rescaleSize || image.height() > rescaleSize) && rescaleSize > 0) {
|
||||||
image = image.scaled(
|
image = image.scaled(
|
||||||
static_cast<int>(rescaleSize),
|
static_cast<int>(rescaleSize),
|
||||||
|
@ -59,7 +55,7 @@ void ColorQuantizerOperation::quantizeImage(const QAtomicInteger<bool>& shouldCa
|
||||||
|
|
||||||
auto endTime = QDateTime::currentDateTime();
|
auto endTime = QDateTime::currentDateTime();
|
||||||
auto milliseconds = startTime.msecsTo(endTime);
|
auto milliseconds = startTime.msecsTo(endTime);
|
||||||
qDebug() << "Color Quantization took: " << milliseconds << "ms";
|
qCDebug(logColorQuantizer) << "Color Quantization took: " << milliseconds << "ms";
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QColor> ColorQuantizerOperation::quantization(
|
QList<QColor> ColorQuantizerOperation::quantization(
|
||||||
|
@ -95,8 +91,8 @@ QList<QColor> ColorQuantizerOperation::quantization(
|
||||||
|
|
||||||
auto dominantChannel = findBiggestColorRange(rgbValues);
|
auto dominantChannel = findBiggestColorRange(rgbValues);
|
||||||
std::ranges::sort(rgbValues, [dominantChannel](const auto& a, const auto& b) {
|
std::ranges::sort(rgbValues, [dominantChannel](const auto& a, const auto& b) {
|
||||||
if (dominantChannel == "r") return a.red() < b.red();
|
if (dominantChannel == 'r') return a.red() < b.red();
|
||||||
else if (dominantChannel == "g") return a.green() < b.green();
|
else if (dominantChannel == 'g') return a.green() < b.green();
|
||||||
return a.blue() < b.blue();
|
return a.blue() < b.blue();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -112,7 +108,7 @@ QList<QColor> ColorQuantizerOperation::quantization(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QChar ColorQuantizerOperation::findBiggestColorRange(const QList<QColor>& rgbValues) {
|
char ColorQuantizerOperation::findBiggestColorRange(const QList<QColor>& rgbValues) {
|
||||||
if (rgbValues.isEmpty()) return 'r';
|
if (rgbValues.isEmpty()) return 'r';
|
||||||
|
|
||||||
auto rMin = 255;
|
auto rMin = 255;
|
||||||
|
@ -203,8 +199,8 @@ void ColorQuantizer::setRescaleSize(int rescaleSize) {
|
||||||
|
|
||||||
void ColorQuantizer::operationFinished(const QList<QColor>& result) {
|
void ColorQuantizer::operationFinished(const QList<QColor>& result) {
|
||||||
bColors = result;
|
bColors = result;
|
||||||
emit this->colorsChanged();
|
|
||||||
this->liveOperation = nullptr;
|
this->liveOperation = nullptr;
|
||||||
|
emit this->colorsChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColorQuantizer::quantizeAsync() {
|
void ColorQuantizer::quantizeAsync() {
|
||||||
|
@ -212,12 +208,14 @@ void ColorQuantizer::quantizeAsync() {
|
||||||
|
|
||||||
qCDebug(logColorQuantizer) << "Starting color quantization asynchronously";
|
qCDebug(logColorQuantizer) << "Starting color quantization asynchronously";
|
||||||
this->liveOperation = new ColorQuantizerOperation(&mSource, mDepth, mRescaleSize);
|
this->liveOperation = new ColorQuantizerOperation(&mSource, mDepth, mRescaleSize);
|
||||||
|
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
this->liveOperation,
|
this->liveOperation,
|
||||||
&ColorQuantizerOperation::done,
|
&ColorQuantizerOperation::done,
|
||||||
this,
|
this,
|
||||||
&ColorQuantizer::operationFinished
|
&ColorQuantizer::operationFinished
|
||||||
);
|
);
|
||||||
|
|
||||||
QThreadPool::globalInstance()->start(this->liveOperation);
|
QThreadPool::globalInstance()->start(this->liveOperation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ private slots:
|
||||||
void finished();
|
void finished();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QChar findBiggestColorRange(const QList<QColor>& rgbValues);
|
static char findBiggestColorRange(const QList<QColor>& rgbValues);
|
||||||
|
|
||||||
void quantizeImage(const QAtomicInteger<bool>& shouldCancel = false);
|
void quantizeImage(const QAtomicInteger<bool>& shouldCancel = false);
|
||||||
QList<QColor> quantization(
|
QList<QColor> quantization(
|
||||||
|
@ -67,7 +67,7 @@ class ColorQuantizer
|
||||||
/// Access the colors resulting from the color quantization performed.
|
/// Access the colors resulting from the color quantization performed.
|
||||||
/// > [!NOTE] The amount of colors returned from the quantization is determined by
|
/// > [!NOTE] The amount of colors returned from the quantization is determined by
|
||||||
/// > the property depth, specifically 2ⁿ where n is the depth.
|
/// > the property depth, specifically 2ⁿ where n is the depth.
|
||||||
Q_PROPERTY(QList<QColor> colors READ default BINDABLE bindableColors);
|
Q_PROPERTY(QList<QColor> colors READ default NOTIFY colorsChanged BINDABLE bindableColors);
|
||||||
|
|
||||||
/// Path to the image you'd like to run the color quantization on.
|
/// Path to the image you'd like to run the color quantization on.
|
||||||
Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged);
|
Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue