GrantleeTheme

color.cpp
1 /*
2  * Copyright (C) 2019 Volker Krause <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "color.h"
20 
21 #include <grantlee/exception.h>
22 #include <grantlee/metatype.h>
23 #include <grantlee/parser.h>
24 #include <grantlee/variable.h>
25 
26 #include <KColorUtils>
27 #include <QColor>
28 #include <QDebug>
29 
30 static QColor inputToColor(const QVariant &v)
31 {
32  if (v.canConvert<Grantlee::SafeString>()) {
33  return QColor(v.value<Grantlee::SafeString>().get());
34  }
35  return v.value<QColor>();
36 }
37 
38 static QString rgbaString(const QColor &c)
39 {
40  return QLatin1String("rgba(")
41  + QString::number(c.red()) + QLatin1String(", ")
42  + QString::number(c.green()) + QLatin1String(", ")
43  + QString::number(c.blue()) + QLatin1String(", ")
44  + QString::number(c.alphaF()) + QLatin1Char(')');
45 }
46 
47 #define COLOR_PROP(name) if (property == QLatin1String(#name)) { return object.name(); }
48 
49 GRANTLEE_BEGIN_LOOKUP(QColor)
50 COLOR_PROP(red)
51 COLOR_PROP(green)
52 COLOR_PROP(blue)
53 COLOR_PROP(alpha)
54 if (property == QLatin1String("hexRgb")) {
55  return object.name();
56 }
57 if (property == QLatin1String("cssRgba")) {
58  return rgbaString(object);
59 }
60 return {};
61 GRANTLEE_END_LOOKUP
62 
63 void Color::registerMetaType()
64 {
65  Grantlee::registerMetaType<QColor>();
66 }
67 
68 QVariant ColorHexRgbFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
69 {
70  Q_UNUSED(arg)
71  Q_UNUSED(autoescape)
72 
73  const auto color = inputToColor(input);
74  return color.name();
75 }
76 
77 QVariant ColorCssRgbaFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
78 {
79  Q_UNUSED(arg)
80  Q_UNUSED(autoescape)
81 
82  const auto color = inputToColor(input);
83  return rgbaString(color);
84 }
85 
86 QVariant ColorLighterFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
87 {
88  Q_UNUSED(autoescape)
89 
90  const auto color = inputToColor(input);
91  const auto factor = arg.toInt();
92  return color.lighter(factor);
93 }
94 
95 QVariant ColorDarkerFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
96 {
97  Q_UNUSED(autoescape)
98 
99  const auto color = inputToColor(input);
100  const auto factor = arg.toInt();
101  return color.darker(factor);
102 }
103 
104 QVariant ColorSetAlphaFilter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const
105 {
106  Q_UNUSED(autoescape)
107 
108  auto color = inputToColor(input);
109  color.setAlpha(arg.toInt());
110  return color;
111 }
112 
113 ColorMixTag::ColorMixTag(QObject *parent)
114  : Grantlee::AbstractNodeFactory(parent)
115 {
116 }
117 
118 ColorMixTag::~ColorMixTag() = default;
119 
120 Grantlee::Node *ColorMixTag::getNode(const QString &tagContent, Grantlee::Parser *p) const
121 {
122  Q_UNUSED(p);
123  const auto parts = smartSplit(tagContent);
124  if (parts.size() != 4 && parts.size() != 6) {
125  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("colormix tag needs 3 or 5 arguments"));
126  }
127 
128  bool ok = false;
129  const auto ratio = parts.at(3).toDouble(&ok);
130  if (!ok) {
131  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("colormix: invalid ratio"));
132  }
133 
134  QString varName;
135  if (parts.size() == 6) {
136  if (parts.at(4) != QLatin1String("as")) {
137  throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("colormix: syntax error"));
138  }
139  varName = parts.at(5);
140  }
141 
142  return new ColorMixNode(parts.at(1), parts.at(2), ratio, varName);
143 }
144 
145 ColorMixNode::ColorMixNode(const QString &color1Name, const QString &color2Name, double ratio, const QString &varName, QObject *parent)
146  : Grantlee::Node(parent)
147  , m_color1Name(color1Name)
148  , m_color2Name(color2Name)
149  , m_varName(varName)
150  , m_ratio(ratio)
151 {
152 }
153 
154 ColorMixNode::~ColorMixNode() = default;
155 
156 static QColor resolveColor(const QString &name, Grantlee::Context *c)
157 {
158  if (name.startsWith(QLatin1Char('"')) && name.endsWith(QLatin1Char('"'))) {
159  return QColor(name.midRef(1, name.size() - 2));
160  }
161 
162  const auto val = Grantlee::Variable(name).resolve(c);
163  return val.value<QColor>();
164 }
165 
166 void ColorMixNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) const
167 {
168  const auto col1 = resolveColor(m_color1Name, c);
169  const auto col2 = resolveColor(m_color2Name, c);
170  const auto result = KColorUtils::mix(col1, col2, m_ratio);
171  if (!m_varName.isEmpty()) {
172  c->insert(m_varName, QVariant::fromValue(result));
173  } else {
174  (*stream) << rgbaString(result);
175  }
176 }
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-2020 The KDE developers.
Generated on Tue Jun 2 2020 23:12:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.