File indexing completed on 2024-09-08 12:18:03
0001 /* vi: ts=8 sts=4 sw=4 0002 0003 This file is part of the KDE project, module kdecore. 0004 SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org> 0005 SPDX-FileCopyrightText: 2000 Antonio Larrosa <larrosa@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-only 0008 */ 0009 0010 #include "kiconcolors.h" 0011 #include <KColorScheme> 0012 0013 static QString STYLESHEET_TEMPLATE() 0014 { 0015 /* clang-format off */ 0016 return QStringLiteral(".ColorScheme-Text { color:%1; }\ 0017 .ColorScheme-Background{ color:%2; }\ 0018 .ColorScheme-Highlight{ color:%3; }\ 0019 .ColorScheme-HighlightedText{ color:%4; }\ 0020 .ColorScheme-PositiveText{ color:%5; }\ 0021 .ColorScheme-NeutralText{ color:%6; }\ 0022 .ColorScheme-NegativeText{ color:%7; }\ 0023 .ColorScheme-ActiveText{ color:%8; }\ 0024 .ColorScheme-Complement{ color:%9; }\ 0025 .ColorScheme-Contrast{ color:%10; }\ 0026 "); 0027 /* clang-format on */ 0028 } 0029 0030 class KIconColorsPrivate : public QSharedData 0031 { 0032 public: 0033 KIconColorsPrivate() 0034 { 0035 } 0036 KIconColorsPrivate(const KIconColorsPrivate &other) 0037 : QSharedData(other) 0038 , text(other.text) 0039 , background(other.background) 0040 , highlight(other.highlight) 0041 , highlightedText(other.highlightedText) 0042 , positiveText(other.positiveText) 0043 , neutralText(other.neutralText) 0044 , negativeText(other.negativeText) 0045 { 0046 } 0047 ~KIconColorsPrivate() 0048 { 0049 } 0050 0051 QColor text; 0052 QColor background; 0053 QColor highlight; 0054 QColor highlightedText; 0055 QColor positiveText; 0056 QColor neutralText; 0057 QColor negativeText; 0058 QColor activeText; 0059 static std::optional<QPalette> lastPalette; 0060 static std::optional<KColorScheme> lastColorScheme; 0061 }; 0062 0063 std::optional<QPalette> KIconColorsPrivate::lastPalette; 0064 std::optional<KColorScheme> KIconColorsPrivate::lastColorScheme; 0065 0066 KIconColors::KIconColors() 0067 : KIconColors(QPalette()) 0068 { 0069 } 0070 0071 KIconColors::KIconColors(const KIconColors &other) 0072 : d_ptr(other.d_ptr) 0073 { 0074 } 0075 0076 KIconColors KIconColors::operator=(const KIconColors &other) 0077 { 0078 if (d_ptr != other.d_ptr) { 0079 d_ptr = other.d_ptr; 0080 } 0081 return *this; 0082 } 0083 0084 KIconColors::KIconColors(const QColor &colors) 0085 : d_ptr(new KIconColorsPrivate) 0086 { 0087 Q_D(KIconColors); 0088 d->text = colors; 0089 d->background = colors; 0090 d->highlight = colors; 0091 d->highlightedText = colors; 0092 d->positiveText = colors; 0093 d->neutralText = colors; 0094 d->negativeText = colors; 0095 } 0096 0097 KIconColors::KIconColors(const QPalette &palette) 0098 : d_ptr(new KIconColorsPrivate) 0099 { 0100 Q_D(KIconColors); 0101 d->text = palette.windowText().color(); 0102 d->background = palette.window().color(); 0103 d->highlight = palette.highlight().color(); 0104 d->highlightedText = palette.highlightedText().color(); 0105 0106 if (!d->lastColorScheme || !d->lastPalette || palette != d->lastPalette) { 0107 d->lastPalette = palette; 0108 d->lastColorScheme = KColorScheme(QPalette::Active, KColorScheme::Window); 0109 } 0110 0111 d->positiveText = d->lastColorScheme->foreground(KColorScheme::PositiveText).color().name(); 0112 d->neutralText = d->lastColorScheme->foreground(KColorScheme::NeutralText).color().name(); 0113 d->negativeText = d->lastColorScheme->foreground(KColorScheme::NegativeText).color().name(); 0114 d->activeText = d->lastColorScheme->foreground(KColorScheme::ActiveText).color().name(); 0115 } 0116 0117 KIconColors::~KIconColors() 0118 { 0119 } 0120 0121 qreal luma(const QColor &color) { 0122 return (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255; 0123 } 0124 0125 QString KIconColors::stylesheet(KIconLoader::States state) const 0126 { 0127 Q_D(const KIconColors); 0128 0129 const QColor complement = 0130 luma(d->background) > 0.5 ? Qt::white : Qt::black; 0131 0132 const QColor contrast = 0133 luma(d->background) > 0.5 ? Qt::black : Qt::white; 0134 0135 return STYLESHEET_TEMPLATE() 0136 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->text.name()) 0137 .arg(state == KIconLoader::SelectedState ? d->highlight.name() : d->background.name()) 0138 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->highlight.name()) 0139 .arg(state == KIconLoader::SelectedState ? d->highlight.name() : d->highlightedText.name()) 0140 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->positiveText.name()) 0141 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->neutralText.name()) 0142 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->negativeText.name()) 0143 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->activeText.name()) 0144 .arg(complement.name()) 0145 .arg(contrast.name()); 0146 } 0147 0148 QColor KIconColors::highlight() const 0149 { 0150 Q_D(const KIconColors); 0151 return d->highlight; 0152 } 0153 0154 QColor KIconColors::highlightedText() const 0155 { 0156 Q_D(const KIconColors); 0157 return d->highlightedText; 0158 } 0159 0160 QColor KIconColors::background() const 0161 { 0162 Q_D(const KIconColors); 0163 return d->background; 0164 } 0165 0166 QColor KIconColors::text() const 0167 { 0168 Q_D(const KIconColors); 0169 return d->text; 0170 } 0171 0172 QColor KIconColors::negativeText() const 0173 { 0174 Q_D(const KIconColors); 0175 return d->negativeText; 0176 } 0177 0178 QColor KIconColors::positiveText() const 0179 { 0180 Q_D(const KIconColors); 0181 return d->positiveText; 0182 } 0183 0184 QColor KIconColors::neutralText() const 0185 { 0186 Q_D(const KIconColors); 0187 return d->neutralText; 0188 } 0189 0190 QColor KIconColors::activeText() const 0191 { 0192 Q_D(const KIconColors); 0193 return d->activeText; 0194 } 0195 0196 void KIconColors::setText(const QColor &color) 0197 { 0198 Q_D(KIconColors); 0199 d->text = color; 0200 } 0201 0202 void KIconColors::setBackground(const QColor &color) 0203 { 0204 Q_D(KIconColors); 0205 d->background = color; 0206 } 0207 0208 void KIconColors::setHighlight(const QColor &color) 0209 { 0210 Q_D(KIconColors); 0211 d->highlight = color; 0212 } 0213 0214 void KIconColors::setHighlightedText(const QColor &color) 0215 { 0216 Q_D(KIconColors); 0217 d->highlightedText = color; 0218 } 0219 0220 void KIconColors::setNegativeText(const QColor &color) 0221 { 0222 Q_D(KIconColors); 0223 d->negativeText = color; 0224 } 0225 0226 void KIconColors::setNeutralText(const QColor &color) 0227 { 0228 Q_D(KIconColors); 0229 d->neutralText = color; 0230 } 0231 0232 void KIconColors::setPositiveText(const QColor &color) 0233 { 0234 Q_D(KIconColors); 0235 d->positiveText = color; 0236 } 0237 0238 void KIconColors::setActiveText(const QColor &color) 0239 { 0240 Q_D(KIconColors); 0241 d->activeText = color; 0242 }