aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2020-04-08 14:35:21 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2020-04-14 14:46:19 +0200
commit53aa259d57e92f48585f95b47a25ee6e16fc2aaa (patch)
treeb3e27b6ee5ca5b8fdfb4d1e084e50ebd151fc0bc /src/qml/qml/v8/qqmlbuiltinfunctions.cpp
parent27748dec471a9c9825f5a35d1936bb5d00400b5b (diff)
Implement Qt.alpha()
Introduces an easy way to get a version of a color with transparency (e.g. Qt.alpha("red", 0.5)). [ChangeLog][QML][General] Added Qt.alpha for easily modifying a color's alpha value Task-number: QTBUG-77635 Change-Id: Ic724e5fa503ca2ae5157a99eed1b5c913a39239f Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/qml/qml/v8/qqmlbuiltinfunctions.cpp')
-rw-r--r--src/qml/qml/v8/qqmlbuiltinfunctions.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
index 158f05c743..1123949cfa 100644
--- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
+++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
@@ -142,6 +142,7 @@ void Heap::QtObject::init(QQmlEngine *qmlEngine)
o->defineDefaultProperty(QStringLiteral("binding"), QV4::QtObject::method_binding);
if (qmlEngine) {
+ o->defineDefaultProperty(QStringLiteral("alpha"), QV4::QtObject::method_alpha);
o->defineDefaultProperty(QStringLiteral("lighter"), QV4::QtObject::method_lighter);
o->defineDefaultProperty(QStringLiteral("darker"), QV4::QtObject::method_darker);
o->defineDefaultProperty(QStringLiteral("tint"), QV4::QtObject::method_tint);
@@ -692,6 +693,35 @@ ReturnedValue QtObject::method_darker(const FunctionObject *b, const Value *, co
}
/*!
+ \qmlmethod color Qt::alpha(color baseColor, real value)
+
+ Returns \a baseColor with an alpha value of \a value.
+
+ \a value is a real ranging from 0 (completely transparent) to 1 (completely opaque).
+*/
+ReturnedValue QtObject::method_alpha(const FunctionObject *b, const Value *, const Value *argv,
+ int argc)
+{
+ QV4::Scope scope(b);
+ if (argc != 2)
+ THROW_GENERIC_ERROR("Qt.alpha(): Wrong number of arguments provided");
+
+ QVariant v = scope.engine->toVariant(argv[0], -1);
+ if (v.userType() == QMetaType::QString) {
+ bool ok = false;
+ v = QQmlStringConverters::colorFromString(v.toString(), &ok);
+ if (!ok)
+ return QV4::Encode::null();
+ } else if (v.userType() != QMetaType::QColor) {
+ return QV4::Encode::null();
+ }
+
+ qreal value = argv[1].toNumber();
+
+ return scope.engine->fromVariant(QQml_colorProvider()->alpha(v, value));
+}
+
+/*!
\qmlmethod color Qt::tint(color baseColor, color tintColor)
This function allows tinting one color (\a baseColor) with another (\a tintColor).
@@ -2258,4 +2288,3 @@ ReturnedValue QtObject::method_callLater(const FunctionObject *b, const Value *t
}
QT_END_NAMESPACE
-