Make QColor understand #AARRGGBB
This way I can have in my QtQuick something like Text { text: "<font color='#ff0000'>H</font> <font color='#99ff0000'>H</font>" } and it works properly QtQuick already supports #AARRGGBB for color: properties so I've decided to go the notation Once this is merged we can remove the extra code in QQuickColorProvider::QColorFromString I've also added some tests for the hex -> QColor conversion that where non existent Change-Id: I1dd4a2ec113293aec26968329b2e4930df6fdcb7 Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
This commit is contained in:
parent
08585f02dc
commit
9b021a1fbd
@ -90,7 +90,8 @@ QT_BEGIN_NAMESPACE
|
||||
specified.
|
||||
|
||||
A color can be set by passing an RGB string (such as "#112233"),
|
||||
or a color name (such as "blue"), to the setNamedColor() function.
|
||||
or an ARGB string (such as "#ff112233") or a color name (such as "blue"),
|
||||
to the setNamedColor() function.
|
||||
The color names are taken from the SVG 1.0 color names. The name()
|
||||
function returns the name of the color in the format
|
||||
"#RRGGBB". Colors can also be set using setRgb(), setHsv() and
|
||||
@ -300,6 +301,17 @@ QT_BEGIN_NAMESPACE
|
||||
\sa spec(), convertTo()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\enum QColor::NameFormat
|
||||
|
||||
How to format the output of the name() function
|
||||
|
||||
\value HexRgb #RRGGBB A "#" character followed by three two-digit hexadecimal numbers (i.e. \c{#RRGGBB}).
|
||||
\value HexArgb #AARRGGBB A "#" character followed by four two-digit hexadecimal numbers (i.e. \c{#AARRGGBB}).
|
||||
|
||||
\sa name()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn Spec QColor::spec() const
|
||||
|
||||
@ -488,9 +500,27 @@ QColor::QColor(Spec spec)
|
||||
*/
|
||||
|
||||
QString QColor::name() const
|
||||
{
|
||||
return name(HexRgb);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the name of the color in the specified \a format.
|
||||
|
||||
\sa setNamedColor(), NameFormat
|
||||
*/
|
||||
|
||||
QString QColor::name(NameFormat format) const
|
||||
{
|
||||
QString s;
|
||||
s.sprintf("#%02x%02x%02x", red(), green(), blue());
|
||||
switch (format) {
|
||||
case HexRgb:
|
||||
s.sprintf("#%02x%02x%02x", red(), green(), blue());
|
||||
break;
|
||||
case HexArgb:
|
||||
s.sprintf("#%02x%02x%02x%02x", alpha(), red(), green(), blue());
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@ -501,6 +531,7 @@ QString QColor::name() const
|
||||
\list
|
||||
\li #RGB (each of R, G, and B is a single hex digit)
|
||||
\li #RRGGBB
|
||||
\li #AARRGGBB
|
||||
\li #RRRGGGBBB
|
||||
\li #RRRRGGGGBBBB
|
||||
\li A name from the list of colors defined in the list of \l{http://www.w3.org/TR/SVG/types.html#ColorKeywords}{SVG color keyword names}
|
||||
@ -545,9 +576,9 @@ bool QColor::setColorFromString(const QString &name)
|
||||
}
|
||||
|
||||
if (name.startsWith(QLatin1Char('#'))) {
|
||||
QRgb rgb;
|
||||
if (qt_get_hex_rgb(name.constData(), name.length(), &rgb)) {
|
||||
setRgb(rgb);
|
||||
QRgb rgba;
|
||||
if (qt_get_hex_rgb(name.constData(), name.length(), &rgba)) {
|
||||
setRgba(rgba);
|
||||
return true;
|
||||
} else {
|
||||
invalidate();
|
||||
|
@ -65,6 +65,7 @@ class Q_GUI_EXPORT QColor
|
||||
{
|
||||
public:
|
||||
enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl };
|
||||
enum NameFormat { HexRgb, HexArgb };
|
||||
|
||||
QColor();
|
||||
QColor(Qt::GlobalColor color);
|
||||
@ -77,7 +78,9 @@ public:
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
// ### Qt 6: merge overloads
|
||||
QString name() const;
|
||||
QString name(NameFormat format) const;
|
||||
void setNamedColor(const QString& name);
|
||||
|
||||
static QStringList colorNames();
|
||||
|
@ -79,7 +79,8 @@ bool qt_get_hex_rgb(const char *name, QRgb *rgb)
|
||||
return false;
|
||||
name++;
|
||||
int len = qstrlen(name);
|
||||
int r, g, b;
|
||||
int a, r, g, b;
|
||||
a = 255;
|
||||
if (len == 12) {
|
||||
r = hex2int(name);
|
||||
g = hex2int(name + 4);
|
||||
@ -88,6 +89,11 @@ bool qt_get_hex_rgb(const char *name, QRgb *rgb)
|
||||
r = hex2int(name);
|
||||
g = hex2int(name + 3);
|
||||
b = hex2int(name + 6);
|
||||
} else if (len == 8) {
|
||||
a = hex2int(name);
|
||||
r = hex2int(name + 2);
|
||||
g = hex2int(name + 4);
|
||||
b = hex2int(name + 6);
|
||||
} else if (len == 6) {
|
||||
r = hex2int(name);
|
||||
g = hex2int(name + 2);
|
||||
@ -99,11 +105,11 @@ bool qt_get_hex_rgb(const char *name, QRgb *rgb)
|
||||
} else {
|
||||
r = g = b = -1;
|
||||
}
|
||||
if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255) {
|
||||
if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || (uint)a > 255) {
|
||||
*rgb = 0;
|
||||
return false;
|
||||
}
|
||||
*rgb = qRgb(r, g ,b);
|
||||
*rgb = qRgba(r, g ,b, a);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -61,6 +61,8 @@ private slots:
|
||||
|
||||
void name_data();
|
||||
void name();
|
||||
void namehex_data();
|
||||
void namehex();
|
||||
void setNamedColor();
|
||||
|
||||
void constructNamedColorWithSpace();
|
||||
@ -254,36 +256,75 @@ void tst_QColor::isValid()
|
||||
QVERIFY(color.isValid() == isValid);
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(QColor::NameFormat);
|
||||
|
||||
void tst_QColor::name_data()
|
||||
{
|
||||
QTest::addColumn<QColor>("color");
|
||||
QTest::addColumn<QString>("name");
|
||||
QTest::addColumn<QColor::NameFormat>("nameFormat");
|
||||
|
||||
QTest::newRow("invalid") << QColor() << "#000000";
|
||||
QTest::newRow("global color black") << QColor(Qt::black) << "#000000";
|
||||
QTest::newRow("global color white") << QColor(Qt::white) << "#ffffff";
|
||||
QTest::newRow("global color darkGray") << QColor(Qt::darkGray) << "#808080";
|
||||
QTest::newRow("global color gray") << QColor(Qt::gray) << "#a0a0a4";
|
||||
QTest::newRow("global color lightGray") << QColor(Qt::lightGray) << "#c0c0c0";
|
||||
QTest::newRow("global color red") << QColor(Qt::red) << "#ff0000";
|
||||
QTest::newRow("global color green") << QColor(Qt::green) << "#00ff00";
|
||||
QTest::newRow("global color blue") << QColor(Qt::blue) << "#0000ff";
|
||||
QTest::newRow("global color cyan") << QColor(Qt::cyan) << "#00ffff";
|
||||
QTest::newRow("global color magenta") << QColor(Qt::magenta) << "#ff00ff";
|
||||
QTest::newRow("global color yellow") << QColor(Qt::yellow) << "#ffff00";
|
||||
QTest::newRow("global color darkRed") << QColor(Qt::darkRed) << "#800000";
|
||||
QTest::newRow("global color darkGreen") << QColor(Qt::darkGreen) << "#008000";
|
||||
QTest::newRow("global color darkBlue") << QColor(Qt::darkBlue) << "#000080";
|
||||
QTest::newRow("global color darkCyan") << QColor(Qt::darkCyan) << "#008080";
|
||||
QTest::newRow("global color darkMagenta") << QColor(Qt::darkMagenta) << "#800080";
|
||||
QTest::newRow("global color darkYellow") << QColor(Qt::darkYellow) << "#808000";
|
||||
QTest::newRow("invalid") << QColor() << "#000000" << QColor::HexRgb;
|
||||
QTest::newRow("global color black") << QColor(Qt::black) << "#000000" << QColor::HexRgb;
|
||||
QTest::newRow("global color white") << QColor(Qt::white) << "#ffffff" << QColor::HexRgb;
|
||||
QTest::newRow("global color darkGray") << QColor(Qt::darkGray) << "#808080" << QColor::HexRgb;
|
||||
QTest::newRow("global color gray") << QColor(Qt::gray) << "#a0a0a4" << QColor::HexRgb;
|
||||
QTest::newRow("global color lightGray") << QColor(Qt::lightGray) << "#c0c0c0" << QColor::HexRgb;
|
||||
QTest::newRow("global color red") << QColor(Qt::red) << "#ff0000" << QColor::HexRgb;
|
||||
QTest::newRow("global color green") << QColor(Qt::green) << "#00ff00" << QColor::HexRgb;
|
||||
QTest::newRow("global color blue") << QColor(Qt::blue) << "#0000ff" << QColor::HexRgb;
|
||||
QTest::newRow("global color cyan") << QColor(Qt::cyan) << "#00ffff" << QColor::HexRgb;
|
||||
QTest::newRow("global color magenta") << QColor(Qt::magenta) << "#ff00ff" << QColor::HexRgb;
|
||||
QTest::newRow("global color yellow") << QColor(Qt::yellow) << "#ffff00" << QColor::HexRgb;
|
||||
QTest::newRow("global color darkRed") << QColor(Qt::darkRed) << "#800000" << QColor::HexRgb;
|
||||
QTest::newRow("global color darkGreen") << QColor(Qt::darkGreen) << "#008000" << QColor::HexRgb;
|
||||
QTest::newRow("global color darkBlue") << QColor(Qt::darkBlue) << "#000080" << QColor::HexRgb;
|
||||
QTest::newRow("global color darkCyan") << QColor(Qt::darkCyan) << "#008080" << QColor::HexRgb;
|
||||
QTest::newRow("global color darkMagenta") << QColor(Qt::darkMagenta) << "#800080" << QColor::HexRgb;
|
||||
QTest::newRow("global color darkYellow") << QColor(Qt::darkYellow) << "#808000" << QColor::HexRgb;
|
||||
QTest::newRow("transparent red") << QColor(255, 0, 0, 102) << "#66ff0000" << QColor::HexArgb;
|
||||
}
|
||||
|
||||
void tst_QColor::name()
|
||||
{
|
||||
QFETCH(QColor, color);
|
||||
QFETCH(QString, name);
|
||||
QCOMPARE(color.name(), name);
|
||||
QFETCH(QColor::NameFormat, nameFormat);
|
||||
QCOMPARE(color.name(nameFormat), name);
|
||||
}
|
||||
|
||||
void tst_QColor::namehex_data()
|
||||
{
|
||||
QTest::addColumn<QString>("hexcolor");
|
||||
QTest::addColumn<QColor>("color");
|
||||
|
||||
QTest::newRow("global color black") << "#000000" << QColor(Qt::black);
|
||||
QTest::newRow("global color white") << "#ffffff" << QColor(Qt::white);
|
||||
QTest::newRow("global color darkGray") << "#808080" << QColor(Qt::darkGray);
|
||||
QTest::newRow("global color gray") << "#a0a0a4" << QColor(Qt::gray);
|
||||
QTest::newRow("global color lightGray") << "#c0c0c0" << QColor(Qt::lightGray);
|
||||
QTest::newRow("global color red") << "#ff0000" << QColor(Qt::red);
|
||||
QTest::newRow("global color green") << "#00ff00" << QColor(Qt::green);
|
||||
QTest::newRow("global color blue") << "#0000ff" << QColor(Qt::blue);
|
||||
QTest::newRow("global color cyan") << "#00ffff" << QColor(Qt::cyan);
|
||||
QTest::newRow("global color magenta") << "#ff00ff" << QColor(Qt::magenta);
|
||||
QTest::newRow("global color yellow") << "#ffff00" << QColor(Qt::yellow);
|
||||
QTest::newRow("global color darkRed") << "#800000" << QColor(Qt::darkRed);
|
||||
QTest::newRow("global color darkGreen") << "#008000" << QColor(Qt::darkGreen);
|
||||
QTest::newRow("global color darkBlue") << "#000080" << QColor(Qt::darkBlue);
|
||||
QTest::newRow("global color darkCyan") << "#008080" << QColor(Qt::darkCyan);
|
||||
QTest::newRow("global color darkMagenta") << "#800080" << QColor(Qt::darkMagenta);
|
||||
QTest::newRow("global color darkYellow") << "#808000" << QColor(Qt::darkYellow);
|
||||
QTest::newRow("transparent red") << "#66ff0000" << QColor(255, 0, 0, 102);
|
||||
QTest::newRow("invalid red") << "#gg0000" << QColor();
|
||||
QTest::newRow("invalid transparent") << "#gg00ff00" << QColor();
|
||||
}
|
||||
|
||||
void tst_QColor::namehex()
|
||||
{
|
||||
QFETCH(QString, hexcolor);
|
||||
QFETCH(QColor, color);
|
||||
QCOMPARE(QColor(hexcolor), color);
|
||||
}
|
||||
|
||||
void tst_QColor::globalColors_data()
|
||||
|
@ -344,8 +344,6 @@ void tst_QCssParser::term_data()
|
||||
val.variant = QVariant(QColor("#ffbb00"));
|
||||
QTest::newRow("hexcolor2") << true << "#fb0" << val;
|
||||
|
||||
QTest::newRow("hexcolor_failure") << false << "#cafebabe" << val;
|
||||
|
||||
val.type = QCss::Value::Uri;
|
||||
val.variant = QString("www.kde.org");
|
||||
QTest::newRow("uri1") << true << "url(\"www.kde.org\")" << val;
|
||||
@ -367,9 +365,6 @@ void tst_QCssParser::term()
|
||||
QFETCH(QString, css);
|
||||
QFETCH(QCss::Value, expectedValue);
|
||||
|
||||
if (strcmp(QTest::currentDataTag(), "hexcolor_failure") == 0)
|
||||
QTest::ignoreMessage(QtWarningMsg, "QCssParser::parseHexColor: Unknown color name '#cafebabe'");
|
||||
|
||||
QCss::Parser parser(css);
|
||||
QCss::Value val;
|
||||
QVERIFY(parser.testTerm());
|
||||
|
Loading…
Reference in New Issue
Block a user