GrantleeTheme

color.cpp
1/*
2 * SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7#include "color.h"
8#include <KTextTemplate/Exception>
9#include <KTextTemplate/MetaType>
10#include <KTextTemplate/Parser>
11#include <KTextTemplate/Variable>
12
13#include <KColorUtils>
14#include <QColor>
15
16static QColor inputToColor(const QVariant &v)
17{
19 return QColor(v.value<KTextTemplate::SafeString>().get());
20 }
21 return v.value<QColor>();
22}
23
24static QString rgbaString(const QColor &c)
25{
28}
29
30#define COLOR_PROP(name) \
31 if (property == QLatin1StringView(#name)) { \
32 return object.name(); \
33 }
35COLOR_PROP(red)
36COLOR_PROP(green)
37COLOR_PROP(blue)
38COLOR_PROP(alpha)
39if (property == QLatin1StringView("hexRgb")) {
40 return object.name();
41}
42if (property == QLatin1StringView("cssRgba")) {
43 return rgbaString(object);
44}
45return {};
47void Color::registerMetaType()
48{
50}
51
52QVariant ColorHexRgbFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
53{
54 Q_UNUSED(arg)
55 Q_UNUSED(autoescape)
56
57 const auto color = inputToColor(input);
58 return color.name();
59}
60
61QVariant ColorCssRgbaFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
62{
63 Q_UNUSED(arg)
64 Q_UNUSED(autoescape)
65
66 const auto color = inputToColor(input);
67 return rgbaString(color);
68}
69
70QVariant ColorLighterFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
71{
72 Q_UNUSED(autoescape)
73
74 const auto color = inputToColor(input);
75 const auto factor = arg.toInt();
76 return color.lighter(factor);
77}
78
79QVariant ColorDarkerFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
80{
81 Q_UNUSED(autoescape)
82
83 const auto color = inputToColor(input);
84 const auto factor = arg.toInt();
85 return color.darker(factor);
86}
87
88QVariant ColorSetAlphaFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
89{
90 Q_UNUSED(autoescape)
91
92 auto color = inputToColor(input);
93 color.setAlpha(arg.toInt());
94 return color;
95}
96
97ColorMixTag::ColorMixTag(QObject *parent)
98 : KTextTemplate::AbstractNodeFactory(parent)
99{
100}
101
102ColorMixTag::~ColorMixTag() = default;
103KTextTemplate::Node *ColorMixTag::getNode(const QString &tagContent, KTextTemplate::Parser *p) const
104{
105 Q_UNUSED(p)
106 const auto parts = smartSplit(tagContent);
107 if (parts.size() != 4 && parts.size() != 6) {
108 throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("colormix tag needs 3 or 5 arguments"));
109 }
110
111 bool ok = false;
112 const auto ratio = parts.at(3).toDouble(&ok);
113 if (!ok) {
114 throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("colormix: invalid ratio"));
115 }
116
117 QString varName;
118 if (parts.size() == 6) {
119 if (parts.at(4) != QLatin1StringView("as")) {
120 throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("colormix: syntax error"));
121 }
122 varName = parts.at(5);
123 }
124
125 return new ColorMixNode(parts.at(1), parts.at(2), ratio, varName);
126}
127
128ColorMixNode::ColorMixNode(const QString &color1Name, const QString &color2Name, double ratio, const QString &varName, QObject *parent)
129 : KTextTemplate::Node(parent)
130 , m_color1Name(color1Name)
131 , m_color2Name(color2Name)
132 , m_varName(varName)
133 , m_ratio(ratio)
134{
135}
136
137ColorMixNode::~ColorMixNode() = default;
138static QColor resolveColor(const QString &name, KTextTemplate::Context *c)
139{
140 if (name.startsWith(QLatin1Char('"')) && name.endsWith(QLatin1Char('"'))) {
141 return QColor(QStringView(name).mid(1, name.size() - 2));
142 }
143 const auto val = KTextTemplate::Variable(name).resolve(c);
144 return val.value<QColor>();
145}
146void ColorMixNode::render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *c) const
147{
148 const auto col1 = resolveColor(m_color1Name, c);
149 const auto col2 = resolveColor(m_color2Name, c);
150 const auto result = KColorUtils::mix(col1, col2, m_ratio);
151 if (!m_varName.isEmpty()) {
152 c->insert(m_varName, QVariant::fromValue(result));
153 } else {
154 (*stream) << rgbaString(result);
155 }
156}
157
158#include "moc_color.cpp"
Q_INVOKABLE QStringList smartSplit(const QString &str) const
void insert(const QString &name, const QVariant &variant)
QVariant resolve(Context *c) const
#define KTEXTTEMPLATE_END_LOOKUP
#define KTEXTTEMPLATE_BEGIN_LOOKUP(Type)
KGUIADDONS_EXPORT QColor mix(const QColor &c1, const QColor &c2, qreal bias=0.5)
int registerMetaType()
float alphaF() const const
int blue() const const
int green() const const
int red() const const
const QChar at(qsizetype position) const const
bool isEmpty() const const
QString number(double n, char format, int precision)
bool canConvert() const const
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.