Improve QColor <==> #AARRGGBB conversion
This commit is contained in:
+19
-1
@@ -77,7 +77,7 @@ bool XlsxColor::saveToXml(QXmlStreamWriter &writer, const QString &node) const
|
||||
writer.writeEmptyElement(QStringLiteral("color"));
|
||||
|
||||
if (val.userType() == qMetaTypeId<QColor>()) {
|
||||
writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+val.value<QColor>().name().mid(1));//remove #
|
||||
writer.writeAttribute(QStringLiteral("rgb"), XlsxColor::toARGBString(val.value<QColor>()));
|
||||
} else if (val.userType() == QMetaType::QStringList) {
|
||||
QStringList themes = val.toStringList();
|
||||
writer.writeAttribute(QStringLiteral("theme"), themes[0]);
|
||||
@@ -155,6 +155,24 @@ QDataStream &operator>>(QDataStream &s, XlsxColor &color)
|
||||
return s;
|
||||
}
|
||||
|
||||
QColor XlsxColor::fromARGBString(const QString &c)
|
||||
{
|
||||
Q_ASSERT(c.length() == 8);
|
||||
QColor color;
|
||||
color.setAlpha(c.mid(0, 2).toInt(0, 16));
|
||||
color.setRed(c.mid(2, 2).toInt(0, 16));
|
||||
color.setGreen(c.mid(4, 2).toInt(0, 16));
|
||||
color.setBlue(c.mid(6, 2).toInt(0, 16));
|
||||
return color;
|
||||
}
|
||||
|
||||
QString XlsxColor::toARGBString(const QColor &c)
|
||||
{
|
||||
QString color;
|
||||
color.sprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());
|
||||
return color;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace QXlsx
|
||||
|
||||
@@ -66,6 +66,9 @@ public:
|
||||
|
||||
operator QVariant() const;
|
||||
|
||||
static QColor fromARGBString(const QString &c);
|
||||
static QString toARGBString(const QColor &c);
|
||||
|
||||
bool saveToXml(QXmlStreamWriter &writer, const QString &node=QString()) const;
|
||||
bool loadFromXml(QXmlStreamReader &reader);
|
||||
|
||||
|
||||
@@ -1222,7 +1222,7 @@ bool Styles::readIndexedColors(QXmlStreamReader &reader)
|
||||
if (reader.tokenType() == QXmlStreamReader::StartElement) {
|
||||
if (reader.name() == QLatin1String("rgbColor")) {
|
||||
QString color = reader.attributes().value(QLatin1String("rgb")).toString();
|
||||
m_indexedColors.append(fromARGBString(color));
|
||||
m_indexedColors.append(XlsxColor::fromARGBString(color));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,16 +54,6 @@ QStringList splitPath(const QString &path)
|
||||
return QStringList()<<path.left(idx)<<path.mid(idx+1);
|
||||
}
|
||||
|
||||
QColor fromARGBString(const QString &c)
|
||||
{
|
||||
Q_ASSERT(c.length() == 8);
|
||||
QColor color;
|
||||
color.setRed(c.mid(2, 2).toInt(0, 16));
|
||||
color.setGreen(c.mid(4, 2).toInt(0, 16));
|
||||
color.setBlue(c.mid(6, 2).toInt(0, 16));
|
||||
return color;
|
||||
}
|
||||
|
||||
double datetimeToNumber(const QDateTime &dt, bool is1904)
|
||||
{
|
||||
//Note, for number 0, Excel2007 shown as 1900-1-0, which should be 1899-12-31
|
||||
|
||||
@@ -47,7 +47,6 @@ namespace QXlsx {
|
||||
|
||||
XLSX_AUTOTEST_EXPORT int intPow(int x, int p);
|
||||
XLSX_AUTOTEST_EXPORT QStringList splitPath(const QString &path);
|
||||
XLSX_AUTOTEST_EXPORT QColor fromARGBString(const QString &c);
|
||||
XLSX_AUTOTEST_EXPORT double datetimeToNumber(const QDateTime &dt, bool is1904=false);
|
||||
XLSX_AUTOTEST_EXPORT QDateTime datetimeFromNumber(double num, bool is1904=false);
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ void StylesTest::testSolidFillBackgroundColor()
|
||||
|
||||
QByteArray xmlData = styles.saveToXmlData();
|
||||
|
||||
QVERIFY(xmlData.contains("<patternFill patternType=\"solid\"><fgColor rgb=\"FFff0000\"/>"));
|
||||
QVERIFY(xmlData.contains("<patternFill patternType=\"solid\"><fgColor rgb=\"FFFF0000\"/>"));
|
||||
}
|
||||
|
||||
void StylesTest::testWriteBorders()
|
||||
|
||||
@@ -93,7 +93,7 @@ void ConditionalFormattingTest::testDataBarRules()
|
||||
QByteArray res = "<cfRule type=\"dataBar\" priority=\"1\">"
|
||||
"<dataBar><cfvo type=\"min\" val=\"0\"/>"
|
||||
"<cfvo type=\"max\" val=\"0\"/>"
|
||||
"<color rgb=\"FF0000ff\"/></dataBar>"
|
||||
"<color rgb=\"FF0000FF\"/></dataBar>"
|
||||
"</cfRule>";
|
||||
QVERIFY(buffer.buffer().contains(res));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user