1/* vi: ts=8 sts=4 sw=4
2
3 This file is part of the KDE project, module kdecore.
4 SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org>
5 SPDX-FileCopyrightText: 2000 Antonio Larrosa <larrosa@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-only
8*/
9
10#include "kiconcolors.h"
11#include <KColorScheme>
12
13static QString STYLESHEET_TEMPLATE()
14{
15 /* clang-format off */
16 return QStringLiteral(".ColorScheme-Text { color:%1; }\
17.ColorScheme-Background{ color:%2; }\
18.ColorScheme-Highlight{ color:%3; }\
19.ColorScheme-HighlightedText{ color:%4; }\
20.ColorScheme-PositiveText{ color:%5; }\
21.ColorScheme-NeutralText{ color:%6; }\
22.ColorScheme-NegativeText{ color:%7; }\
23.ColorScheme-ActiveText{ color:%8; }\
24.ColorScheme-Complement{ color:%9; }\
25.ColorScheme-Contrast{ color:%10; }\
26.ColorScheme-Accent{ color:%11; }\
27");
28 /* clang-format on */
29}
30
31class KIconColorsPrivate : public QSharedData
32{
33public:
34 KIconColorsPrivate()
35 {
36 }
37 KIconColorsPrivate(const KIconColorsPrivate &other)
38 : QSharedData(other)
39 , text(other.text)
40 , background(other.background)
41 , highlight(other.highlight)
42 , highlightedText(other.highlightedText)
43 , accent(other.accent)
44 , positiveText(other.positiveText)
45 , neutralText(other.neutralText)
46 , negativeText(other.negativeText)
47 {
48 }
49 ~KIconColorsPrivate()
50 {
51 }
52
53 QColor text;
54 QColor background;
55 QColor highlight;
56 QColor highlightedText;
57 QColor accent;
58 QColor positiveText;
59 QColor neutralText;
60 QColor negativeText;
61 QColor activeText;
62 static std::optional<QPalette> lastPalette;
63 static std::optional<KColorScheme> lastColorScheme;
64};
65
66std::optional<QPalette> KIconColorsPrivate::lastPalette;
67std::optional<KColorScheme> KIconColorsPrivate::lastColorScheme;
68
69KIconColors::KIconColors()
70 : KIconColors(QPalette())
71{
72}
73
74KIconColors::KIconColors(const KIconColors &other)
75 : d_ptr(other.d_ptr)
76{
77}
78
79KIconColors KIconColors::operator=(const KIconColors &other)
80{
81 if (d_ptr != other.d_ptr) {
82 d_ptr = other.d_ptr;
83 }
84 return *this;
85}
86
87KIconColors::KIconColors(const QColor &colors)
88 : d_ptr(new KIconColorsPrivate)
89{
90 Q_D(KIconColors);
91 d->text = colors;
92 d->background = colors;
93 d->highlight = colors;
94 d->highlightedText = colors;
95 d->positiveText = colors;
96 d->neutralText = colors;
97 d->negativeText = colors;
98 d->accent = colors;
99}
100
101KIconColors::KIconColors(const QPalette &palette)
102 : d_ptr(new KIconColorsPrivate)
103{
104 Q_D(KIconColors);
105 d->text = palette.windowText().color();
106 d->background = palette.window().color();
107 d->highlight = palette.highlight().color();
108 d->highlightedText = palette.highlightedText().color();
109#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
110 d->accent = palette.accent().color();
111#else
112 d->accent = palette.highlight().color();
113#endif
114
115 if (!d->lastColorScheme || !d->lastPalette || palette != d->lastPalette) {
116 d->lastPalette = palette;
117 d->lastColorScheme = KColorScheme(QPalette::Active, KColorScheme::Window);
118 }
119
120 d->positiveText = d->lastColorScheme->foreground(KColorScheme::PositiveText).color().name();
121 d->neutralText = d->lastColorScheme->foreground(KColorScheme::NeutralText).color().name();
122 d->negativeText = d->lastColorScheme->foreground(KColorScheme::NegativeText).color().name();
123 d->activeText = d->lastColorScheme->foreground(KColorScheme::ActiveText).color().name();
124}
125
126KIconColors::~KIconColors()
127{
128}
129
130qreal luma(const QColor &color) {
131 return (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255;
132}
133
134QString KIconColors::stylesheet(KIconLoader::States state) const
135{
136 Q_D(const KIconColors);
137
138 const QColor complement =
139 luma(color: d->background) > 0.5 ? Qt::white : Qt::black;
140
141 const QColor contrast =
142 luma(color: d->background) > 0.5 ? Qt::black : Qt::white;
143
144 return STYLESHEET_TEMPLATE()
145 .arg(a: state == KIconLoader::SelectedState ? d->highlightedText.name() : d->text.name())
146 .arg(a: state == KIconLoader::SelectedState ? d->highlight.name() : d->background.name())
147 .arg(a: state == KIconLoader::SelectedState ? d->highlightedText.name() : d->highlight.name())
148 .arg(a: state == KIconLoader::SelectedState ? d->highlight.name() : d->highlightedText.name())
149 .arg(a: state == KIconLoader::SelectedState ? d->highlightedText.name() : d->positiveText.name())
150 .arg(a: state == KIconLoader::SelectedState ? d->highlightedText.name() : d->neutralText.name())
151 .arg(a: state == KIconLoader::SelectedState ? d->highlightedText.name() : d->negativeText.name())
152 .arg(a: state == KIconLoader::SelectedState ? d->highlightedText.name() : d->activeText.name())
153 .arg(a: complement.name())
154 .arg(a: contrast.name())
155 .arg(a: state == KIconLoader::SelectedState ? d->accent.name() : d->highlightedText.name());
156}
157
158QColor KIconColors::highlight() const
159{
160 Q_D(const KIconColors);
161 return d->highlight;
162}
163
164QColor KIconColors::highlightedText() const
165{
166 Q_D(const KIconColors);
167 return d->highlightedText;
168}
169
170QColor KIconColors::accent() const
171{
172 Q_D(const KIconColors);
173 return d->accent;
174}
175
176QColor KIconColors::background() const
177{
178 Q_D(const KIconColors);
179 return d->background;
180}
181
182QColor KIconColors::text() const
183{
184 Q_D(const KIconColors);
185 return d->text;
186}
187
188QColor KIconColors::negativeText() const
189{
190 Q_D(const KIconColors);
191 return d->negativeText;
192}
193
194QColor KIconColors::positiveText() const
195{
196 Q_D(const KIconColors);
197 return d->positiveText;
198}
199
200QColor KIconColors::neutralText() const
201{
202 Q_D(const KIconColors);
203 return d->neutralText;
204}
205
206QColor KIconColors::activeText() const
207{
208 Q_D(const KIconColors);
209 return d->activeText;
210}
211
212void KIconColors::setText(const QColor &color)
213{
214 Q_D(KIconColors);
215 d->text = color;
216}
217
218void KIconColors::setBackground(const QColor &color)
219{
220 Q_D(KIconColors);
221 d->background = color;
222}
223
224void KIconColors::setHighlight(const QColor &color)
225{
226 Q_D(KIconColors);
227 d->highlight = color;
228}
229
230void KIconColors::setHighlightedText(const QColor &color)
231{
232 Q_D(KIconColors);
233 d->highlightedText = color;
234}
235
236void KIconColors::setAccent(const QColor &color)
237{
238 Q_D(KIconColors);
239 d->accent = color;
240}
241
242void KIconColors::setNegativeText(const QColor &color)
243{
244 Q_D(KIconColors);
245 d->negativeText = color;
246}
247
248void KIconColors::setNeutralText(const QColor &color)
249{
250 Q_D(KIconColors);
251 d->neutralText = color;
252}
253
254void KIconColors::setPositiveText(const QColor &color)
255{
256 Q_D(KIconColors);
257 d->positiveText = color;
258}
259
260void KIconColors::setActiveText(const QColor &color)
261{
262 Q_D(KIconColors);
263 d->activeText = color;
264}
265

source code of kiconthemes/src/kiconcolors.cpp