Warning, file /plasma/plasma-workspace/kcms/colors/colorsapplicator.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2021 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003     SPDX-FileCopyrightText: 2021 Benjamin Port <benjamin.port@enioka.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #pragma once
0009 
0010 #include <optional>
0011 
0012 #include <QColor>
0013 #include <QString>
0014 
0015 #include <KColorUtils>
0016 #include <KConfig>
0017 
0018 inline QColor alphaBlend(const QColor &foreground, const QColor &background)
0019 {
0020     const auto foregroundAlpha = foreground.alphaF();
0021     const auto inverseForegroundAlpha = 1.0 - foregroundAlpha;
0022     const auto backgroundAlpha = background.alphaF();
0023 
0024     if (foregroundAlpha == 0.0) {
0025         return background;
0026     }
0027 
0028     if (backgroundAlpha == 1.0) {
0029         return QColor::fromRgb((foregroundAlpha * foreground.red()) + (inverseForegroundAlpha * background.red()),
0030                                (foregroundAlpha * foreground.green()) + (inverseForegroundAlpha * background.green()),
0031                                (foregroundAlpha * foreground.blue()) + (inverseForegroundAlpha * background.blue()),
0032                                0xff);
0033     } else {
0034         const auto inverseBackgroundAlpha = (backgroundAlpha * inverseForegroundAlpha);
0035         const auto finalAlpha = foregroundAlpha + inverseBackgroundAlpha;
0036         Q_ASSERT(finalAlpha != 0.0);
0037 
0038         return QColor::fromRgb((foregroundAlpha * foreground.red()) + (inverseBackgroundAlpha * background.red()),
0039                                (foregroundAlpha * foreground.green()) + (inverseBackgroundAlpha * background.green()),
0040                                (foregroundAlpha * foreground.blue()) + (inverseBackgroundAlpha * background.blue()),
0041                                finalAlpha);
0042     }
0043 }
0044 
0045 inline QColor accentBackground(const QColor &accent, const QColor &background)
0046 {
0047     auto c = accent;
0048     c.setAlphaF(0.7);
0049     return alphaBlend(c, background);
0050 }
0051 
0052 inline QColor accentForeground(const QColor &accent, const bool &isActive)
0053 {
0054     auto c = QColor(Qt::white);
0055     // light bg
0056     if (KColorUtils::luma(accent) > 0.5) {
0057         c = QColor(Qt::black);
0058     } else {
0059         // dark bg
0060         c = QColor(Qt::white);
0061     }
0062 
0063     if (isActive) {
0064         c.setAlphaF(1.0);
0065     } else {
0066         c.setAlphaF(0.6);
0067     }
0068     return alphaBlend(c, accent);
0069 }
0070 
0071 /**
0072  * Performs the task of actually applying a color scheme to the current session, based on
0073  * color scheme file path and configuration file.
0074  * When using this function, you select the scheme to use by setting the model's selected scheme
0075  * @param colorFilePath The scheme color file path
0076  * @param configOut The config which holds the information on which scheme is currently selected, and what colors it contains
0077  */
0078 void applyScheme(const QString &colorSchemePath,
0079                  KConfig *configOut,
0080                  KConfig::WriteConfigFlags writeFlags = KConfig::Normal,
0081                  std::optional<QColor> accentColor = std::nullopt);
0082 
0083 const qreal DefaultTintFactor = 0.15;
0084 
0085 QColor tintColor(const QColor &base, const QColor &with, qreal factor = DefaultTintFactor);