diff --git a/src/colorutils.h b/src/colorutils.h --- a/src/colorutils.h +++ b/src/colorutils.h @@ -51,7 +51,7 @@ * @since 5.69 * @since org.kde.kirigami 2.12 */ - Q_INVOKABLE ColorUtils::Brightness brightnessForColor(QColor color); + Q_INVOKABLE ColorUtils::Brightness brightnessForColor(const QColor &color); /** * Returns the result of overlaying the foreground color on the background @@ -73,7 +73,7 @@ * @since 5.69 * @since org.kde.kirigami 2.12 */ - Q_INVOKABLE QColor alphaBlend(QColor foreground, QColor background); + Q_INVOKABLE QColor alphaBlend(const QColor &foreground, const QColor &background); /** * Returns a linearly interpolated color between color one and color two. @@ -98,7 +98,7 @@ * @since 5.69 * @since org.kde.kirigami 2.12 */ - Q_INVOKABLE QColor linearInterpolation(QColor one, QColor two, double balance); + Q_INVOKABLE QColor linearInterpolation(const QColor &one, const QColor &two, double balance); /** * Increases or decreases the properties of `color` by fixed amounts. @@ -127,7 +127,7 @@ * @since 5.69 * @since org.kde.kirigami 2.12 */ - Q_INVOKABLE QColor adjustColor(QColor color, QJSValue adjustments); + Q_INVOKABLE QColor adjustColor(const QColor &color, const QJSValue &adjustments); /** * Smoothly scales colors. @@ -158,7 +158,7 @@ * @since 5.69 * @since org.kde.kirigami 2.12 */ - Q_INVOKABLE QColor scaleColor(QColor color, QJSValue adjustments); + Q_INVOKABLE QColor scaleColor(const QColor &color, const QJSValue &adjustments); /** * Tint a color using a separate alpha value. diff --git a/src/colorutils.cpp b/src/colorutils.cpp --- a/src/colorutils.cpp +++ b/src/colorutils.cpp @@ -14,20 +14,22 @@ ColorUtils::ColorUtils(QObject *parent) : QObject(parent) {} -ColorUtils::Brightness ColorUtils::brightnessForColor(QColor color) { - auto luma = [](QColor color) { - return (0.299*color.red() + 0.587*color.green() + 0.114*color.blue())/255; +ColorUtils::Brightness ColorUtils::brightnessForColor(const QColor &color) { + auto luma = [](const QColor &color) { + return (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255; }; return luma(color) > 0.5 ? ColorUtils::Brightness::Light : ColorUtils::Brightness::Dark; } -QColor ColorUtils::alphaBlend(QColor foreground, QColor background) { +QColor ColorUtils::alphaBlend(const QColor &foreground, const QColor &background) { const auto foregroundAlpha = foreground.alpha(); const auto inverseForegroundAlpha = 0xff - foregroundAlpha; const auto backgroundAlpha = background.alpha(); - if (foregroundAlpha == 0x00) return background; + if (foregroundAlpha == 0x00) { + return background; + } if (backgroundAlpha == 0xff) { return QColor::fromRgb( @@ -49,17 +51,21 @@ } } -QColor ColorUtils::linearInterpolation(QColor one, QColor two, double balance) { +QColor ColorUtils::linearInterpolation(const QColor &one, const QColor &two, double balance) { - auto scaleAlpha = [](QColor color, double factor) { - return QColor::fromRgb(color.red(), color.green(), color.blue(), color.alpha()*factor); + auto scaleAlpha = [](const QColor &color, double factor) { + return QColor::fromRgb(color.red(), color.green(), color.blue(), color.alpha() * factor); }; auto linearlyInterpolateDouble = [](double one, double two, double factor) { return one + (two - one) * factor; }; - if (one == Qt::transparent) return scaleAlpha(two, balance); - if (two == Qt::transparent) return scaleAlpha(one, 1 - balance); + if (one == Qt::transparent) { + return scaleAlpha(two, balance); + } + if (two == Qt::transparent) { + return scaleAlpha(one, 1 - balance); + } return QColor::fromHsv( std::fmod(linearlyInterpolateDouble(one.hue(), two.hue(), balance), 360.0), @@ -83,11 +89,11 @@ double alpha = 0.0; }; -ParsedAdjustments parseAdjustments(QJSValue value) +ParsedAdjustments parseAdjustments(const QJSValue &value) { ParsedAdjustments parsed; - auto checkProperty = [](QJSValue value, QString property) { + auto checkProperty = [](const QJSValue &value, const QString &property) { if (value.hasProperty(property)) { auto val = value.property(property); if (val.isNumber()) { @@ -97,7 +103,7 @@ return QVariant(); }; - std::map map = { + std::vector> items { { QStringLiteral("red"), parsed.red }, { QStringLiteral("green"), parsed.green }, { QStringLiteral("blue"), parsed.blue }, @@ -110,9 +116,11 @@ { QStringLiteral("alpha"), parsed.alpha } }; - for (std::pair item : map) { + for (const auto &item : items) { auto val = checkProperty(value, item.first); - if (val != QVariant()) item.second = val.toDouble(); + if (val.isValid()) { + item.second = val.toDouble(); + } } if ((parsed.red || parsed.green || parsed.blue) && (parsed.hue || parsed.saturation || parsed.value)) { @@ -122,18 +130,31 @@ return parsed; } -QColor ColorUtils::adjustColor(QColor color, QJSValue adjustments) +QColor ColorUtils::adjustColor(const QColor &color, const QJSValue &adjustments) { auto adjusts = parseAdjustments(adjustments); - if (qBound(-360.0, adjusts.hue, 360.0) != adjusts.hue) qCritical() << "Hue is out of bounds"; - - if (qBound(-255.0, adjusts.red, 255.0) != adjusts.red) qCritical() << "Red is out of bounds"; - if (qBound(-255.0, adjusts.green, 255.0) != adjusts.green) qCritical() << "Green is out of bounds"; - if (qBound(-255.0, adjusts.blue, 255.0) != adjusts.blue) qCritical() << "Green is out of bounds"; - if (qBound(-255.0, adjusts.saturation, 255.0) != adjusts.saturation) qCritical() << "Saturation is out of bounds"; - if (qBound(-255.0, adjusts.value, 255.0) != adjusts.value) qCritical() << "Value is out of bounds"; - if (qBound(-255.0, adjusts.alpha, 255.0) != adjusts.alpha) qCritical() << "Alpha is out of bounds"; + if (qBound(-360.0, adjusts.hue, 360.0) != adjusts.hue) { + qCritical() << "Hue is out of bounds"; + } + if (qBound(-255.0, adjusts.red, 255.0) != adjusts.red) { + qCritical() << "Red is out of bounds"; + } + if (qBound(-255.0, adjusts.green, 255.0) != adjusts.green) { + qCritical() << "Green is out of bounds"; + } + if (qBound(-255.0, adjusts.blue, 255.0) != adjusts.blue) { + qCritical() << "Green is out of bounds"; + } + if (qBound(-255.0, adjusts.saturation, 255.0) != adjusts.saturation) { + qCritical() << "Saturation is out of bounds"; + } + if (qBound(-255.0, adjusts.value, 255.0) != adjusts.value) { + qCritical() << "Value is out of bounds"; + } + if (qBound(-255.0, adjusts.alpha, 255.0) != adjusts.alpha) { + qCritical() << "Alpha is out of bounds"; + } auto copy = color; @@ -147,32 +168,46 @@ copy.setBlue(copy.blue() + adjusts.blue); } else if (adjusts.hue || adjusts.saturation || adjusts.value) { copy.setHsl( - std::fmod(copy.hue()+adjusts.hue, 360.0), - copy.saturation()+adjusts.saturation, - copy.value()+adjusts.value, + std::fmod(copy.hue() + adjusts.hue, 360.0), + copy.saturation() + adjusts.saturation, + copy.value() + adjusts.value, copy.alpha() ); } return copy; } -QColor ColorUtils::scaleColor(QColor color, QJSValue adjustments) +QColor ColorUtils::scaleColor(const QColor& color, const QJSValue &adjustments) { auto adjusts = parseAdjustments(adjustments); auto copy = color; - if (qBound(-100.0, adjusts.red, 100.00) != adjusts.red) qCritical() << "Red is out of bounds"; - if (qBound(-100.0, adjusts.green, 100.00) != adjusts.green) qCritical() << "Green is out of bounds"; - if (qBound(-100.0, adjusts.blue, 100.00) != adjusts.blue) qCritical() << "Blue is out of bounds"; - if (qBound(-100.0, adjusts.saturation, 100.00) != adjusts.saturation) qCritical() << "Saturation is out of bounds"; - if (qBound(-100.0, adjusts.value, 100.00) != adjusts.value) qCritical() << "Value is out of bounds"; - if (qBound(-100.0, adjusts.alpha, 100.00) != adjusts.alpha) qCritical() << "Alpha is out of bounds"; + if (qBound(-100.0, adjusts.red, 100.00) != adjusts.red) { + qCritical() << "Red is out of bounds"; + } + if (qBound(-100.0, adjusts.green, 100.00) != adjusts.green) { + qCritical() << "Green is out of bounds"; + } + if (qBound(-100.0, adjusts.blue, 100.00) != adjusts.blue) { + qCritical() << "Blue is out of bounds"; + } + if (qBound(-100.0, adjusts.saturation, 100.00) != adjusts.saturation) { + qCritical() << "Saturation is out of bounds"; + } + if (qBound(-100.0, adjusts.value, 100.00) != adjusts.value) { + qCritical() << "Value is out of bounds"; + } + if (qBound(-100.0, adjusts.alpha, 100.00) != adjusts.alpha) { + qCritical() << "Alpha is out of bounds"; + } - if (adjusts.hue != 0) qCritical() << "Hue cannot be scaled"; + if (adjusts.hue != 0) { + qCritical() << "Hue cannot be scaled"; + } auto shiftToAverage = [](double current, double factor) { - auto scale = qBound(-100.0, factor, 100.0)/100; + auto scale = qBound(-100.0, factor, 100.0) / 100; return current + (scale > 0 ? 255 - current : current) * scale; };