KIconThemes

kiconcolors.cpp
1/*
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
73
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
88 : d_ptr(new KIconColorsPrivate)
89{
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
102 : d_ptr(new KIconColorsPrivate)
103{
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 d->accent = palette.accent().color();
110
111 if (!d->lastColorScheme || !d->lastPalette || palette != d->lastPalette) {
112 d->lastPalette = palette;
114 }
115
116 d->positiveText = d->lastColorScheme->foreground(KColorScheme::PositiveText).color().name();
117 d->neutralText = d->lastColorScheme->foreground(KColorScheme::NeutralText).color().name();
118 d->negativeText = d->lastColorScheme->foreground(KColorScheme::NegativeText).color().name();
119 d->activeText = d->lastColorScheme->foreground(KColorScheme::ActiveText).color().name();
120}
121
122KIconColors::~KIconColors()
123{
124}
125
126qreal luma(const QColor &color) {
127 return (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255;
128}
129
131{
132 Q_D(const KIconColors);
133
134 const QColor complement =
135 luma(d->background) > 0.5 ? Qt::white : Qt::black;
136
137 const QColor contrast =
138 luma(d->background) > 0.5 ? Qt::black : Qt::white;
139
140 QColor accentColor = d->accent;
141 // When selected, tint the accent color with a small portion of highlighted text color,
142 // because since the accent color used to be the same as the highlight color, it might cause
143 // icons, especially folders to "disappear" against the background
144 if (state == KIconLoader::SelectedState) {
145 const qreal tintRatio = 0.85;
146 const qreal r = accentColor.redF() * tintRatio + d->highlightedText.redF() * (1.0 - tintRatio);
147 const qreal g = accentColor.greenF() * tintRatio + d->highlightedText.greenF() * (1.0 - tintRatio);
148 const qreal b = accentColor.blueF() * tintRatio + d->highlightedText.blueF() * (1.0 - tintRatio);
149 accentColor.setRgbF(r, g, b, accentColor.alphaF());
150 }
151
152 return STYLESHEET_TEMPLATE()
153 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->text.name())
154 .arg(state == KIconLoader::SelectedState ? d->highlight.name() : d->background.name())
155 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->highlight.name())
156 .arg(state == KIconLoader::SelectedState ? d->highlight.name() : d->highlightedText.name())
157 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->positiveText.name())
158 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->neutralText.name())
159 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->negativeText.name())
160 .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->activeText.name())
161 .arg(complement.name())
162 .arg(contrast.name())
163 .arg(accentColor.name());
164}
165
166QColor KIconColors::highlight() const
167{
168 Q_D(const KIconColors);
169 return d->highlight;
170}
171
172QColor KIconColors::highlightedText() const
173{
174 Q_D(const KIconColors);
175 return d->highlightedText;
176}
177
178QColor KIconColors::accent() const
179{
180 Q_D(const KIconColors);
181 return d->accent;
182}
183
184QColor KIconColors::background() const
185{
186 Q_D(const KIconColors);
187 return d->background;
188}
189
190QColor KIconColors::text() const
191{
192 Q_D(const KIconColors);
193 return d->text;
194}
195
196QColor KIconColors::negativeText() const
197{
198 Q_D(const KIconColors);
199 return d->negativeText;
200}
201
202QColor KIconColors::positiveText() const
203{
204 Q_D(const KIconColors);
205 return d->positiveText;
206}
207
208QColor KIconColors::neutralText() const
209{
210 Q_D(const KIconColors);
211 return d->neutralText;
212}
213
214QColor KIconColors::activeText() const
215{
216 Q_D(const KIconColors);
217 return d->activeText;
218}
219
220void KIconColors::setText(const QColor &color)
221{
223 d->text = color;
224}
225
226void KIconColors::setBackground(const QColor &color)
227{
229 d->background = color;
230}
231
232void KIconColors::setHighlight(const QColor &color)
233{
235 d->highlight = color;
236}
237
238void KIconColors::setHighlightedText(const QColor &color)
239{
241 d->highlightedText = color;
242}
243
244void KIconColors::setAccent(const QColor &color)
245{
247 d->accent = color;
248}
249
250void KIconColors::setNegativeText(const QColor &color)
251{
253 d->negativeText = color;
254}
255
256void KIconColors::setNeutralText(const QColor &color)
257{
259 d->neutralText = color;
260}
261
262void KIconColors::setPositiveText(const QColor &color)
263{
265 d->positiveText = color;
266}
267
268void KIconColors::setActiveText(const QColor &color)
269{
271 d->activeText = color;
272}
Sepecifies which colors will be used when recoloring icons as its stylesheet.
Definition kiconcolors.h:31
KIconColors()
Will fill the colors based on the default QPalette() constructor.
QString stylesheet(KIconLoader::States state) const
States
Defines the possible states of an icon.
@ SelectedState
Icon is selected.
KGUIADDONS_EXPORT qreal luma(const QColor &)
const QColor & color() const const
float alphaF() const const
int blue() const const
float blueF() const const
int green() const const
float greenF() const const
QString name(NameFormat format) const const
int red() const const
float redF() const const
void setRgbF(float r, float g, float b, float a)
const QBrush & accent() const const
const QBrush & highlight() const const
const QBrush & highlightedText() const const
const QBrush & window() const const
const QBrush & windowText() const const
QString arg(Args &&... args) const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.