Commit 75c14dab authored by Thomas Hartmann's avatar Thomas Hartmann

QmlDesigner.PropertyEditor: Adding and fixing color conversion

Adding proper color conversion as C++ helper functions that can
handle the alpha channel.

Change-Id: If48cd11ff71efb472d331ed6fd79464e1c5d572d
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@digia.com>
parent 29f660ee
......@@ -31,6 +31,47 @@
#include <QQmlContext>
static uchar fromHex(const uchar c, const uchar c2)
{
uchar rv = 0;
if (c >= '0' && c <= '9')
rv += (c - '0') * 16;
else if (c >= 'A' && c <= 'F')
rv += (c - 'A' + 10) * 16;
else if (c >= 'a' && c <= 'f')
rv += (c - 'a' + 10) * 16;
if (c2 >= '0' && c2 <= '9')
rv += (c2 - '0');
else if (c2 >= 'A' && c2 <= 'F')
rv += (c2 - 'A' + 10);
else if (c2 >= 'a' && c2 <= 'f')
rv += (c2 - 'a' + 10);
return rv;
}
static uchar fromHex(const QString &s, int idx)
{
uchar c = s.at(idx).toLatin1();
uchar c2 = s.at(idx + 1).toLatin1();
return fromHex(c, c2);
}
QColor convertColorFromString(const QString &s)
{
if (s.length() == 9 && s.startsWith(QLatin1Char('#'))) {
uchar a = fromHex(s, 1);
uchar r = fromHex(s, 3);
uchar g = fromHex(s, 5);
uchar b = fromHex(s, 7);
return QColor(r, g, b, a);
} else {
QColor rv(s);
return rv;
}
}
namespace QmlDesigner {
PropertyEditorContextObject::PropertyEditorContextObject(QObject *parent) :
......@@ -47,6 +88,25 @@ PropertyEditorContextObject::PropertyEditorContextObject(QObject *parent) :
}
QString PropertyEditorContextObject::convertColorToString(const QColor &color)
{
QString colorString = color.name();
if (color.alpha() != 255) {
const QString hexAlpha = QString::number(color.alpha(), 16);
qDebug() << "alpha" << hexAlpha;
colorString.remove(0,1);
colorString.prepend(hexAlpha);
colorString.prepend(QStringLiteral("#"));
}
return colorString;
}
QColor PropertyEditorContextObject::colorFromString(const QString &colorString)
{
return convertColorFromString(colorString);
}
int PropertyEditorContextObject::majorVersion() const
{
return m_majorVersion;
......
......@@ -72,7 +72,8 @@ public:
QQmlPropertyMap* backendValues() const { return m_backendValues; }
Q_INVOKABLE QString convertColorToString(const QColor &color) { return color.name(); }
Q_INVOKABLE QString convertColorToString(const QColor &color);
Q_INVOKABLE QColor colorFromString(const QString &colorString);
int majorVersion() const;
int majorQtQuickVersion() const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment