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 0056 // The visual presentation of text and images of text has a contrast ratio of at least 4.5 0057 // https://www.w3.org/TR/WCAG/#contrast-minimum 0058 if (KColorUtils::contrastRatio(accent, QColor(Qt::white)) < 4.5) { 0059 // light bg 0060 c = QColor(Qt::black); 0061 } else { 0062 // dark bg 0063 c = QColor(Qt::white); 0064 } 0065 0066 if (isActive) { 0067 c.setAlphaF(1.0); 0068 } else { 0069 c.setAlphaF(0.6); 0070 } 0071 return alphaBlend(c, accent); 0072 } 0073 0074 /** 0075 * Performs the task of actually applying a color scheme to the current session, based on 0076 * color scheme file path and configuration file. 0077 * When using this function, you select the scheme to use by setting the model's selected scheme 0078 * @param colorFilePath The scheme color file path 0079 * @param configOut The config which holds the information on which scheme is currently selected, and what colors it contains 0080 */ 0081 void applyScheme(const QString &colorSchemePath, 0082 KConfig *configOut, 0083 KConfig::WriteConfigFlags writeFlags = KConfig::Normal, 0084 std::optional<QColor> accentColor = std::nullopt); 0085 0086 const qreal DefaultTintFactor = 0.15; 0087 0088 QColor tintColor(const QColor &base, const QColor &with, qreal factor = DefaultTintFactor);