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 | |
13 | static 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 | " ); |
27 | /* clang-format on */ |
28 | } |
29 | |
30 | class KIconColorsPrivate : public QSharedData |
31 | { |
32 | public: |
33 | KIconColorsPrivate() |
34 | { |
35 | } |
36 | KIconColorsPrivate(const KIconColorsPrivate &other) |
37 | : QSharedData(other) |
38 | , text(other.text) |
39 | , background(other.background) |
40 | , highlight(other.highlight) |
41 | , highlightedText(other.highlightedText) |
42 | , positiveText(other.positiveText) |
43 | , neutralText(other.neutralText) |
44 | , negativeText(other.negativeText) |
45 | { |
46 | } |
47 | ~KIconColorsPrivate() |
48 | { |
49 | } |
50 | |
51 | QColor text; |
52 | QColor background; |
53 | QColor highlight; |
54 | QColor highlightedText; |
55 | QColor positiveText; |
56 | QColor neutralText; |
57 | QColor negativeText; |
58 | QColor activeText; |
59 | static std::optional<QPalette> lastPalette; |
60 | static std::optional<KColorScheme> lastColorScheme; |
61 | }; |
62 | |
63 | std::optional<QPalette> KIconColorsPrivate::lastPalette; |
64 | std::optional<KColorScheme> KIconColorsPrivate::lastColorScheme; |
65 | |
66 | KIconColors::KIconColors() |
67 | : KIconColors(QPalette()) |
68 | { |
69 | } |
70 | |
71 | KIconColors::KIconColors(const KIconColors &other) |
72 | : d_ptr(other.d_ptr) |
73 | { |
74 | } |
75 | |
76 | KIconColors KIconColors::operator=(const KIconColors &other) |
77 | { |
78 | if (d_ptr != other.d_ptr) { |
79 | d_ptr = other.d_ptr; |
80 | } |
81 | return *this; |
82 | } |
83 | |
84 | KIconColors::KIconColors(const QColor &colors) |
85 | : d_ptr(new KIconColorsPrivate) |
86 | { |
87 | Q_D(KIconColors); |
88 | d->text = colors; |
89 | d->background = colors; |
90 | d->highlight = colors; |
91 | d->highlightedText = colors; |
92 | d->positiveText = colors; |
93 | d->neutralText = colors; |
94 | d->negativeText = colors; |
95 | } |
96 | |
97 | KIconColors::KIconColors(const QPalette &palette) |
98 | : d_ptr(new KIconColorsPrivate) |
99 | { |
100 | Q_D(KIconColors); |
101 | d->text = palette.windowText().color(); |
102 | d->background = palette.window().color(); |
103 | d->highlight = palette.highlight().color(); |
104 | d->highlightedText = palette.highlightedText().color(); |
105 | |
106 | if (!d->lastColorScheme || !d->lastPalette || palette != d->lastPalette) { |
107 | d->lastPalette = palette; |
108 | d->lastColorScheme = KColorScheme(QPalette::Active, KColorScheme::Window); |
109 | } |
110 | |
111 | d->positiveText = d->lastColorScheme->foreground(KColorScheme::PositiveText).color().name(); |
112 | d->neutralText = d->lastColorScheme->foreground(KColorScheme::NeutralText).color().name(); |
113 | d->negativeText = d->lastColorScheme->foreground(KColorScheme::NegativeText).color().name(); |
114 | d->activeText = d->lastColorScheme->foreground(KColorScheme::ActiveText).color().name(); |
115 | } |
116 | |
117 | KIconColors::~KIconColors() |
118 | { |
119 | } |
120 | |
121 | qreal luma(const QColor &color) { |
122 | return (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255; |
123 | } |
124 | |
125 | QString KIconColors::stylesheet(KIconLoader::States state) const |
126 | { |
127 | Q_D(const KIconColors); |
128 | |
129 | const QColor complement = |
130 | luma(d->background) > 0.5 ? Qt::white : Qt::black; |
131 | |
132 | const QColor contrast = |
133 | luma(d->background) > 0.5 ? Qt::black : Qt::white; |
134 | |
135 | return STYLESHEET_TEMPLATE() |
136 | .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->text.name()) |
137 | .arg(state == KIconLoader::SelectedState ? d->highlight.name() : d->background.name()) |
138 | .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->highlight.name()) |
139 | .arg(state == KIconLoader::SelectedState ? d->highlight.name() : d->highlightedText.name()) |
140 | .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->positiveText.name()) |
141 | .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->neutralText.name()) |
142 | .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->negativeText.name()) |
143 | .arg(state == KIconLoader::SelectedState ? d->highlightedText.name() : d->activeText.name()) |
144 | .arg(complement.name()) |
145 | .arg(contrast.name()); |
146 | } |
147 | |
148 | QColor KIconColors::highlight() const |
149 | { |
150 | Q_D(const KIconColors); |
151 | return d->highlight; |
152 | } |
153 | |
154 | QColor KIconColors::highlightedText() const |
155 | { |
156 | Q_D(const KIconColors); |
157 | return d->highlightedText; |
158 | } |
159 | |
160 | QColor KIconColors::background() const |
161 | { |
162 | Q_D(const KIconColors); |
163 | return d->background; |
164 | } |
165 | |
166 | QColor KIconColors::text() const |
167 | { |
168 | Q_D(const KIconColors); |
169 | return d->text; |
170 | } |
171 | |
172 | QColor KIconColors::negativeText() const |
173 | { |
174 | Q_D(const KIconColors); |
175 | return d->negativeText; |
176 | } |
177 | |
178 | QColor KIconColors::positiveText() const |
179 | { |
180 | Q_D(const KIconColors); |
181 | return d->positiveText; |
182 | } |
183 | |
184 | QColor KIconColors::neutralText() const |
185 | { |
186 | Q_D(const KIconColors); |
187 | return d->neutralText; |
188 | } |
189 | |
190 | QColor KIconColors::activeText() const |
191 | { |
192 | Q_D(const KIconColors); |
193 | return d->activeText; |
194 | } |
195 | |
196 | void KIconColors::setText(const QColor &color) |
197 | { |
198 | Q_D(KIconColors); |
199 | d->text = color; |
200 | } |
201 | |
202 | void KIconColors::setBackground(const QColor &color) |
203 | { |
204 | Q_D(KIconColors); |
205 | d->background = color; |
206 | } |
207 | |
208 | void KIconColors::setHighlight(const QColor &color) |
209 | { |
210 | Q_D(KIconColors); |
211 | d->highlight = color; |
212 | } |
213 | |
214 | void KIconColors::setHighlightedText(const QColor &color) |
215 | { |
216 | Q_D(KIconColors); |
217 | d->highlightedText = color; |
218 | } |
219 | |
220 | void KIconColors::setNegativeText(const QColor &color) |
221 | { |
222 | Q_D(KIconColors); |
223 | d->negativeText = color; |
224 | } |
225 | |
226 | void KIconColors::setNeutralText(const QColor &color) |
227 | { |
228 | Q_D(KIconColors); |
229 | d->neutralText = color; |
230 | } |
231 | |
232 | void KIconColors::setPositiveText(const QColor &color) |
233 | { |
234 | Q_D(KIconColors); |
235 | d->positiveText = color; |
236 | } |
237 | |
238 | void KIconColors::setActiveText(const QColor &color) |
239 | { |
240 | Q_D(KIconColors); |
241 | d->activeText = color; |
242 | } |
243 | |