diff options
author | Sean Harmer <sean.harmer@kdab.com> | 2015-02-15 10:51:51 +0000 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2015-02-18 15:56:27 +0000 |
commit | 6f264b755501fd322d1e357187b42120210a7ba3 (patch) | |
tree | 0ea8915de1e41bed87c62deffa19d61a0e6ff288 | |
parent | 3fcb6ccac0baa34b820534184f863884e072cfb2 (diff) |
Add Qt.hsva() function
This is more convenient than the alternative hsla() function in many
cases as color pickers in other applications default to the HSV color
space e.g. GIMP, kcolorchooser.
[ChangeLog][QtQml] Added Qt.hsva() function
Change-Id: Id5c1a78173757bf9842b164d90b31682e9a41749
Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
-rw-r--r-- | src/qml/qml/qqmlglobal.cpp | 1 | ||||
-rw-r--r-- | src/qml/qml/qqmlglobal_p.h | 1 | ||||
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 28 | ||||
-rw-r--r-- | src/qml/qml/v8/qqmlbuiltinfunctions_p.h | 1 | ||||
-rw-r--r-- | src/quick/util/qquickglobal.cpp | 5 | ||||
-rw-r--r-- | tests/auto/qml/qqmlqt/data/hsva.qml | 11 | ||||
-rw-r--r-- | tests/auto/qml/qqmlqt/tst_qqmlqt.cpp | 23 |
7 files changed, 70 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlglobal.cpp b/src/qml/qml/qqmlglobal.cpp index a4554f3513..d904242f93 100644 --- a/src/qml/qml/qqmlglobal.cpp +++ b/src/qml/qml/qqmlglobal.cpp @@ -317,6 +317,7 @@ QVariant QQmlColorProvider::colorFromString(const QString &, bool *ok) { if (ok) unsigned QQmlColorProvider::rgbaFromString(const QString &, bool *ok) { if (ok) *ok = false; return 0; } QVariant QQmlColorProvider::fromRgbF(double, double, double, double) { return QVariant(); } QVariant QQmlColorProvider::fromHslF(double, double, double, double) { return QVariant(); } +QVariant QQmlColorProvider::fromHsvF(double, double, double, double) { return QVariant(); } QVariant QQmlColorProvider::lighter(const QVariant &, qreal) { return QVariant(); } QVariant QQmlColorProvider::darker(const QVariant &, qreal) { return QVariant(); } QVariant QQmlColorProvider::tint(const QVariant &, const QVariant &) { return QVariant(); } diff --git a/src/qml/qml/qqmlglobal_p.h b/src/qml/qml/qqmlglobal_p.h index ef08ab2876..7856d85376 100644 --- a/src/qml/qml/qqmlglobal_p.h +++ b/src/qml/qml/qqmlglobal_p.h @@ -279,6 +279,7 @@ public: virtual QVariant fromRgbF(double, double, double, double); virtual QVariant fromHslF(double, double, double, double); + virtual QVariant fromHsvF(double, double, double, double); virtual QVariant lighter(const QVariant &, qreal); virtual QVariant darker(const QVariant &, qreal); virtual QVariant tint(const QVariant &, const QVariant &); diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp index 109629f5f1..8e5cbb8e96 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp @@ -101,6 +101,7 @@ Heap::QtObject::QtObject(ExecutionEngine *v4, QQmlEngine *qmlEngine) o->defineDefaultProperty(QStringLiteral("isQtObject"), QV4::QtObject::method_isQtObject); o->defineDefaultProperty(QStringLiteral("rgba"), QV4::QtObject::method_rgba); o->defineDefaultProperty(QStringLiteral("hsla"), QV4::QtObject::method_hsla); + o->defineDefaultProperty(QStringLiteral("hsva"), QV4::QtObject::method_hsva); o->defineDefaultProperty(QStringLiteral("colorEqual"), QV4::QtObject::method_colorEqual); o->defineDefaultProperty(QStringLiteral("rect"), QV4::QtObject::method_rect); o->defineDefaultProperty(QStringLiteral("point"), QV4::QtObject::method_point); @@ -215,6 +216,33 @@ ReturnedValue QtObject::method_hsla(QV4::CallContext *ctx) } /*! +\qmlmethod color Qt::hsva(real hue, real saturation, real value, real alpha) + +Returns a color with the specified \c hue, \c saturation, \c value and \c alpha components. +All components should be in the range 0-1 inclusive. + +\since 5.5 +*/ +ReturnedValue QtObject::method_hsva(QV4::CallContext *ctx) +{ + int argCount = ctx->argc(); + if (argCount < 3 || argCount > 4) + V4THROW_ERROR("Qt.hsva(): Invalid arguments"); + + double h = ctx->args()[0].toNumber(); + double s = ctx->args()[1].toNumber(); + double v = ctx->args()[2].toNumber(); + double a = (argCount == 4) ? ctx->args()[3].toNumber() : 1; + + h = qBound(0.0, h, 1.0); + s = qBound(0.0, s, 1.0); + v = qBound(0.0, v, 1.0); + a = qBound(0.0, a, 1.0); + + return ctx->engine()->fromVariant(QQml_colorProvider()->fromHsvF(h, s, v, a)); +} + +/*! \qmlmethod color Qt::colorEqual(color lhs, string rhs) Returns true if both \c lhs and \c rhs yield equal color values. Both arguments diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h index dc45429aeb..b78375118b 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions_p.h +++ b/src/qml/qml/v8/qqmlbuiltinfunctions_p.h @@ -83,6 +83,7 @@ struct QtObject : Object static ReturnedValue method_isQtObject(CallContext *ctx); static ReturnedValue method_rgba(CallContext *ctx); static ReturnedValue method_hsla(CallContext *ctx); + static ReturnedValue method_hsva(CallContext *ctx); static ReturnedValue method_colorEqual(CallContext *ctx); static ReturnedValue method_font(CallContext *ctx); static ReturnedValue method_rect(CallContext *ctx); diff --git a/src/quick/util/qquickglobal.cpp b/src/quick/util/qquickglobal.cpp index ad2be02334..6aa7bedc5b 100644 --- a/src/quick/util/qquickglobal.cpp +++ b/src/quick/util/qquickglobal.cpp @@ -98,6 +98,11 @@ public: return QVariant(QColor::fromHslF(h, s, l, a)); } + QVariant fromHsvF(double h, double s, double v, double a) + { + return QVariant(QColor::fromHsvF(h, s, v, a)); + } + QVariant lighter(const QVariant &var, qreal factor) { QColor color = var.value<QColor>(); diff --git a/tests/auto/qml/qqmlqt/data/hsva.qml b/tests/auto/qml/qqmlqt/data/hsva.qml new file mode 100644 index 0000000000..4b73bf12eb --- /dev/null +++ b/tests/auto/qml/qqmlqt/data/hsva.qml @@ -0,0 +1,11 @@ +import QtQuick 2.0 + +QtObject { + property color test1: Qt.hsva(1, 0, 0, 0.8); + property color test2: Qt.hsva(1, 0.5, 0.3); + property color test3: Qt.hsva(1, 1); + property color test4: Qt.hsva(1, 1, 1, 1, 1); + property color test5: Qt.hsva(1.2, 1.3, 1.4, 1.5); + property color test6: Qt.hsva(-0.1, -0.2, -0.3, -0.4); +} + diff --git a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp index a881cdab80..671f7b5e73 100644 --- a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp +++ b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp @@ -61,6 +61,7 @@ private slots: void enums(); void rgba(); void hsla(); + void hsva(); void colorEqual(); void rect(); void point(); @@ -155,6 +156,28 @@ void tst_qqmlqt::hsla() delete object; } +void tst_qqmlqt::hsva() +{ + QQmlComponent component(&engine, testFileUrl("hsva.qml")); + + QString warning1 = component.url().toString() + ":6: Error: Qt.hsva(): Invalid arguments"; + QString warning2 = component.url().toString() + ":7: Error: Qt.hsva(): Invalid arguments"; + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1)); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2)); + + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromHsvF(1, 0, 0, 0.8)); + QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor::fromHsvF(1, 0.5, 0.3, 1)); + QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor()); + QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor()); + QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor::fromHsvF(1, 1, 1, 1)); + QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor::fromHsvF(0, 0, 0, 0)); + + delete object; +} + void tst_qqmlqt::colorEqual() { QQmlComponent component(&engine, testFileUrl("colorEqual.qml")); |