30 const QDomElement oldElement = element.firstChildElement( identifier );
31 if ( !oldElement.isNull() )
32 element.removeChild( oldElement );
35 QDomElement colorElement = document.createElement( identifier );
36 if ( !color.isValid() )
38 colorElement.setAttribute( QStringLiteral(
"invalid" ), QStringLiteral(
"1" ) );
43 switch ( color.spec() )
49 case QColor::ExtendedRgb:
52 spec = QStringLiteral(
"rgb" );
53#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
62 color.getRgbF( &red, &green, &blue );
64 colorElement.setAttribute( QStringLiteral(
"green" ),
qgsDoubleToString( green ) );
71 spec = QStringLiteral(
"hsv" );
73#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
82 color.getHsvF( &h, &s, &v );
84 colorElement.setAttribute( QStringLiteral(
"saturation" ),
qgsDoubleToString( s ) );
91 spec = QStringLiteral(
"hsl" );
93#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
102 color.getHslF( &h, &s, &l );
104 colorElement.setAttribute( QStringLiteral(
"saturation" ),
qgsDoubleToString( s ) );
105 colorElement.setAttribute( QStringLiteral(
"lightness" ),
qgsDoubleToString( l ) );
111 spec = QStringLiteral(
"cmyk" );
113#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
125 color.getCmykF( &
c, &y, &m, &k );
133 colorElement.setAttribute( QStringLiteral(
"spec" ), spec );
134 if ( color.alphaF() < 1.0 )
136 colorElement.setAttribute( QStringLiteral(
"alpha" ),
qgsDoubleToString( color.alphaF() ) );
139 element.appendChild( colorElement );
144 const QDomElement colorElement = element.firstChildElement( identifier );
145 if ( colorElement.isNull() )
148 const bool invalid = colorElement.attribute( QStringLiteral(
"invalid" ), QStringLiteral(
"0" ) ).toInt();
153 const QString spec = colorElement.attribute( QStringLiteral(
"spec" ) );
154 if ( spec == QLatin1String(
"rgb" ) )
157 const double red = colorElement.attribute( QStringLiteral(
"red" ) ).toDouble();
158 const double green = colorElement.attribute( QStringLiteral(
"green" ) ).toDouble();
159 const double blue = colorElement.attribute( QStringLiteral(
"blue" ) ).toDouble();
160 res = QColor::fromRgbF( red, green, blue );
162 else if ( spec == QLatin1String(
"hsv" ) )
164 const double hue = colorElement.attribute( QStringLiteral(
"hue" ) ).toDouble();
165 const double saturation = colorElement.attribute( QStringLiteral(
"saturation" ) ).toDouble();
166 const double value = colorElement.attribute( QStringLiteral(
"value" ) ).toDouble();
167 res = QColor::fromHsvF( hue, saturation, value );
169 else if ( spec == QLatin1String(
"hsl" ) )
171 const double hue = colorElement.attribute( QStringLiteral(
"hue" ) ).toDouble();
172 const double saturation = colorElement.attribute( QStringLiteral(
"saturation" ) ).toDouble();
173 const double value = colorElement.attribute( QStringLiteral(
"lightness" ) ).toDouble();
174 res = QColor::fromHslF( hue, saturation, value );
176 else if ( spec == QLatin1String(
"cmyk" ) )
178 const double cyan = colorElement.attribute( QStringLiteral(
"c" ) ).toDouble();
179 const double magenta = colorElement.attribute( QStringLiteral(
"m" ) ).toDouble();
180 const double yellow = colorElement.attribute( QStringLiteral(
"y" ) ).toDouble();
181 const double black = colorElement.attribute( QStringLiteral(
"k" ) ).toDouble();
182 res = QColor::fromCmykF( cyan, magenta, yellow, black );
186 const double alpha = colorElement.attribute( QStringLiteral(
"alpha" ), QStringLiteral(
"1" ) ).toDouble();
187 res.setAlphaF( alpha );
195 if ( !color.isValid() )
200 const QString compatString = QStringLiteral(
"%1,%2,%3,%4," ).arg( color.red() ).arg( color.green() ).arg( color.blue() ).arg( color.alpha() );
202 switch ( color.spec() )
204 case QColor::Invalid:
208 case QColor::ExtendedRgb:
211#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
222 color.getRgbF( &red, &green, &blue, &alpha );
223 return compatString + QStringLiteral(
"rgb:%1,%2,%3,%4" ).arg(
qgsDoubleToString( red ),
231#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
242 color.getHsvF( &h, &s, &v, &alpha );
243 return compatString + QStringLiteral(
"hsv:%1,%2,%3,%4" ).arg(
qgsDoubleToString( h ),
251#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
262 color.getHslF( &h, &s, &l, &alpha );
263 return compatString + QStringLiteral(
"hsl:%1,%2,%3,%4" ).arg(
qgsDoubleToString( h ),
271#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
285 color.getCmykF( &
c, &m, &y, &k, &alpha );
286 return compatString + QStringLiteral(
"cmyk:%1,%2,%3,%4,%5" ).arg(
qgsDoubleToString(
c ),
298 if (
string.isEmpty() )
301 const thread_local QRegularExpression rx( QStringLiteral(
"^(.*),([a-z]+):([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),?([\\d\\.\\-]*)$" ) );
302 const QRegularExpressionMatch match = rx.match(
string );
303 if ( !match.hasMatch() )
306 const QStringList lst =
string.split(
',' );
307 if ( lst.count() < 3 )
309 return QColor(
string );
311 int red, green, blue, alpha;
312 red = lst[0].toInt();
313 green = lst[1].toInt();
314 blue = lst[2].toInt();
315 alpha = lst.count() > 3 ? lst[3].toInt() : 255;
316 return QColor( red, green, blue, alpha );
319 const QString spec = match.captured( 2 );
321 if ( spec == QLatin1String(
"rgb" ) )
324 const double red = match.captured( 3 ).toDouble();
325 const double green = match.captured( 4 ).toDouble();
326 const double blue = match.captured( 5 ).toDouble();
327 const double alpha = match.captured( 6 ).toDouble();
328 return QColor::fromRgbF( red, green, blue, alpha );
330 else if ( spec == QLatin1String(
"hsv" ) )
332 const double hue = match.captured( 3 ).toDouble();
333 const double saturation = match.captured( 4 ).toDouble();
334 const double value = match.captured( 5 ).toDouble();
335 const double alpha = match.captured( 6 ).toDouble();
336 return QColor::fromHsvF( hue, saturation, value, alpha );
338 else if ( spec == QLatin1String(
"hsl" ) )
340 const double hue = match.captured( 3 ).toDouble();
341 const double saturation = match.captured( 4 ).toDouble();
342 const double lightness = match.captured( 5 ).toDouble();
343 const double alpha = match.captured( 6 ).toDouble();
344 return QColor::fromHslF( hue, saturation, lightness, alpha );
346 else if ( spec == QLatin1String(
"cmyk" ) )
348 const double cyan = match.captured( 3 ).toDouble();
349 const double magenta = match.captured( 4 ).toDouble();
350 const double yellow = match.captured( 5 ).toDouble();
351 const double black = match.captured( 6 ).toDouble();
352 const double alpha = match.captured( 7 ).toDouble();
353 return QColor::fromCmykF( cyan, magenta, yellow, black, alpha );
360 if ( iccProfileFilePath.isEmpty() )
361 return QColorSpace();
363 QFile file( iccProfileFilePath );
364 if ( !file.open( QIODevice::ReadOnly ) )
366 errorMsg = QObject::tr(
"Failed to open ICC Profile: %1" ).arg( iccProfileFilePath );
367 return QColorSpace();
370 QColorSpace colorSpace = QColorSpace::fromIccProfile( file.readAll() );
371 if ( !colorSpace.isValid() )
373 errorMsg = QObject::tr(
"Invalid ICC Profile: %1" ).arg( iccProfileFilePath );
383 if ( !colorSpace.isValid() )
384 return QObject::tr(
"Invalid ICC profile" );
387 if ( !
iccProfile.open( QIODevice::WriteOnly ) )
388 return QObject::tr(
"File access error '%1'" ).arg( iccProfileFilePath );
390 if (
iccProfile.write( colorSpace.iccProfile() ) < 0 )
391 return QObject::tr(
"Error while writing to file '%1'" ).arg( iccProfileFilePath );
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.
The class is used as a container of context for various read/write operations on other objects.