GrantleeTheme

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

KDE's Doxygen guidelines are available online.