21#include <QDomDocument>
22#include <QRegularExpression>
23#include <QRegularExpressionMatch>
29 const QDomElement oldElement = element.firstChildElement( identifier );
30 if ( !oldElement.isNull() )
31 element.removeChild( oldElement );
34 QDomElement colorElement = document.createElement( identifier );
35 if ( !color.isValid() )
37 colorElement.setAttribute( QStringLiteral(
"invalid" ), QStringLiteral(
"1" ) );
42 switch ( color.spec() )
48 case QColor::ExtendedRgb:
51 spec = QStringLiteral(
"rgb" );
52#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
61 color.getRgbF( &red, &green, &blue );
63 colorElement.setAttribute( QStringLiteral(
"green" ),
qgsDoubleToString( green ) );
70 spec = QStringLiteral(
"hsv" );
72#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
81 color.getHsvF( &h, &s, &v );
83 colorElement.setAttribute( QStringLiteral(
"saturation" ),
qgsDoubleToString( s ) );
90 spec = QStringLiteral(
"hsl" );
92#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
101 color.getHslF( &h, &s, &l );
103 colorElement.setAttribute( QStringLiteral(
"saturation" ),
qgsDoubleToString( s ) );
104 colorElement.setAttribute( QStringLiteral(
"lightness" ),
qgsDoubleToString( l ) );
110 spec = QStringLiteral(
"cmyk" );
112#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
124 color.getCmykF( &
c, &y, &m, &k );
132 colorElement.setAttribute( QStringLiteral(
"spec" ), spec );
133 if ( color.alphaF() < 1.0 )
135 colorElement.setAttribute( QStringLiteral(
"alpha" ),
qgsDoubleToString( color.alphaF() ) );
138 element.appendChild( colorElement );
143 const QDomElement colorElement = element.firstChildElement( identifier );
144 if ( colorElement.isNull() )
147 const bool invalid = colorElement.attribute( QStringLiteral(
"invalid" ), QStringLiteral(
"0" ) ).toInt();
152 const QString spec = colorElement.attribute( QStringLiteral(
"spec" ) );
153 if ( spec == QLatin1String(
"rgb" ) )
156 const double red = colorElement.attribute( QStringLiteral(
"red" ) ).toDouble();
157 const double green = colorElement.attribute( QStringLiteral(
"green" ) ).toDouble();
158 const double blue = colorElement.attribute( QStringLiteral(
"blue" ) ).toDouble();
159 res = QColor::fromRgbF( red, green, blue );
161 else if ( spec == QLatin1String(
"hsv" ) )
163 const double hue = colorElement.attribute( QStringLiteral(
"hue" ) ).toDouble();
164 const double saturation = colorElement.attribute( QStringLiteral(
"saturation" ) ).toDouble();
165 const double value = colorElement.attribute( QStringLiteral(
"value" ) ).toDouble();
166 res = QColor::fromHsvF( hue, saturation, value );
168 else if ( spec == QLatin1String(
"hsl" ) )
170 const double hue = colorElement.attribute( QStringLiteral(
"hue" ) ).toDouble();
171 const double saturation = colorElement.attribute( QStringLiteral(
"saturation" ) ).toDouble();
172 const double value = colorElement.attribute( QStringLiteral(
"lightness" ) ).toDouble();
173 res = QColor::fromHslF( hue, saturation, value );
175 else if ( spec == QLatin1String(
"cmyk" ) )
177 const double cyan = colorElement.attribute( QStringLiteral(
"c" ) ).toDouble();
178 const double magenta = colorElement.attribute( QStringLiteral(
"m" ) ).toDouble();
179 const double yellow = colorElement.attribute( QStringLiteral(
"y" ) ).toDouble();
180 const double black = colorElement.attribute( QStringLiteral(
"k" ) ).toDouble();
181 res = QColor::fromCmykF( cyan, magenta, yellow, black );
185 const double alpha = colorElement.attribute( QStringLiteral(
"alpha" ), QStringLiteral(
"1" ) ).toDouble();
186 res.setAlphaF( alpha );
194 if ( !color.isValid() )
199 const QString compatString = QStringLiteral(
"%1,%2,%3,%4," ).arg( color.red() ).arg( color.green() ).arg( color.blue() ).arg( color.alpha() );
201 switch ( color.spec() )
203 case QColor::Invalid:
207 case QColor::ExtendedRgb:
210#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
221 color.getRgbF( &red, &green, &blue, &alpha );
222 return compatString + QStringLiteral(
"rgb:%1,%2,%3,%4" ).arg(
qgsDoubleToString( red ),
230#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
241 color.getHsvF( &h, &s, &v, &alpha );
242 return compatString + QStringLiteral(
"hsv:%1,%2,%3,%4" ).arg(
qgsDoubleToString( h ),
250#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
261 color.getHslF( &h, &s, &l, &alpha );
262 return compatString + QStringLiteral(
"hsl:%1,%2,%3,%4" ).arg(
qgsDoubleToString( h ),
270#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
284 color.getCmykF( &
c, &y, &m, &k, &alpha );
285 return compatString + QStringLiteral(
"cmyk:%1,%2,%3,%4,%5" ).arg(
qgsDoubleToString(
c ),
297 if (
string.isEmpty() )
300 const thread_local QRegularExpression rx( QStringLiteral(
"^(.*),([a-z]+):([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),?([\\d\\.\\-]*)$" ) );
301 const QRegularExpressionMatch match = rx.match(
string );
302 if ( !match.hasMatch() )
305 const thread_local QRegularExpression rgbArx( QStringLiteral(
"^(\\d+),(\\d+),(\\d+),(\\d+)$" ) );
306 const QRegularExpressionMatch match = rgbArx.match(
string );
307 if ( !match.hasMatch() )
310 const int red = match.captured( 1 ).toInt();
311 const int green = match.captured( 2 ).toInt();
312 const int blue = match.captured( 3 ).toInt();
313 const int alpha = match.captured( 4 ).toInt();
314 return QColor( red, green, blue, alpha );
317 const QString spec = match.captured( 2 );
319 if ( spec == QLatin1String(
"rgb" ) )
322 const double red = match.captured( 3 ).toDouble();
323 const double green = match.captured( 4 ).toDouble();
324 const double blue = match.captured( 5 ).toDouble();
325 const double alpha = match.captured( 6 ).toDouble();
326 return QColor::fromRgbF( red, green, blue, alpha );
328 else if ( spec == QLatin1String(
"hsv" ) )
330 const double hue = match.captured( 3 ).toDouble();
331 const double saturation = match.captured( 4 ).toDouble();
332 const double value = match.captured( 5 ).toDouble();
333 const double alpha = match.captured( 6 ).toDouble();
334 return QColor::fromHsvF( hue, saturation, value, alpha );
336 else if ( spec == QLatin1String(
"hsl" ) )
338 const double hue = match.captured( 3 ).toDouble();
339 const double saturation = match.captured( 4 ).toDouble();
340 const double lightness = match.captured( 5 ).toDouble();
341 const double alpha = match.captured( 6 ).toDouble();
342 return QColor::fromHslF( hue, saturation, lightness, alpha );
344 else if ( spec == QLatin1String(
"cmyk" ) )
346 const double cyan = match.captured( 3 ).toDouble();
347 const double magenta = match.captured( 4 ).toDouble();
348 const double yellow = match.captured( 5 ).toDouble();
349 const double black = match.captured( 6 ).toDouble();
350 const double alpha = match.captured( 7 ).toDouble();
351 return QColor::fromCmykF( cyan, magenta, yellow, black, alpha );
static QColor readXml(const QDomElement &element, const QString &identifier, const QgsReadWriteContext &context)
Reads a color from an XML element, matching the specified identifier string.
static void writeXml(const QColor &color, const QString &identifier, QDomDocument &document, QDomElement &element, const QgsReadWriteContext &context)
Writes a color to an XML element, storing it under the specified identifier.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
The class is used as a container of context for various read/write operations on other objects.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.