33 const QDomElement oldElement = element.firstChildElement( identifier );
34 if ( !oldElement.isNull() )
35 element.removeChild( oldElement );
38 QDomElement colorElement = document.createElement( identifier );
39 if ( !color.isValid() )
41 colorElement.setAttribute( u
"invalid"_s, u
"1"_s );
46 switch ( color.spec() )
52 case QColor::ExtendedRgb:
60 color.getRgbF( &red, &green, &blue );
75 color.getHsvF( &h, &s, &v );
90 color.getHslF( &h, &s, &l );
106 color.getCmykF( &
c, &y, &m, &k );
114 colorElement.setAttribute( u
"spec"_s, spec );
115 if ( color.alphaF() < 1.0 )
120 element.appendChild( colorElement );
125 const QDomElement colorElement = element.firstChildElement( identifier );
126 if ( colorElement.isNull() )
129 const bool invalid = colorElement.attribute( u
"invalid"_s, u
"0"_s ).toInt();
134 const QString spec = colorElement.attribute( u
"spec"_s );
135 if ( spec ==
"rgb"_L1 )
138 const double red = colorElement.attribute( u
"red"_s ).toDouble();
139 const double green = colorElement.attribute( u
"green"_s ).toDouble();
140 const double blue = colorElement.attribute( u
"blue"_s ).toDouble();
141 res = QColor::fromRgbF( red, green, blue );
143 else if ( spec ==
"hsv"_L1 )
145 const double hue = colorElement.attribute( u
"hue"_s ).toDouble();
146 const double saturation = colorElement.attribute( u
"saturation"_s ).toDouble();
147 const double value = colorElement.attribute( u
"value"_s ).toDouble();
148 res = QColor::fromHsvF( hue, saturation, value );
150 else if ( spec ==
"hsl"_L1 )
152 const double hue = colorElement.attribute( u
"hue"_s ).toDouble();
153 const double saturation = colorElement.attribute( u
"saturation"_s ).toDouble();
154 const double value = colorElement.attribute( u
"lightness"_s ).toDouble();
155 res = QColor::fromHslF( hue, saturation, value );
157 else if ( spec ==
"cmyk"_L1 )
159 const double cyan = colorElement.attribute( u
"c"_s ).toDouble();
160 const double magenta = colorElement.attribute( u
"m"_s ).toDouble();
161 const double yellow = colorElement.attribute( u
"y"_s ).toDouble();
162 const double black = colorElement.attribute( u
"k"_s ).toDouble();
163 res = QColor::fromCmykF( cyan, magenta, yellow, black );
167 const double alpha = colorElement.attribute( u
"alpha"_s, u
"1"_s ).toDouble();
168 res.setAlphaF( alpha );
176 if ( !color.isValid() )
181 const QString compatString = u
"%1,%2,%3,%4,"_s.arg( color.red() ).arg( color.green() ).arg( color.blue() ).arg( color.alpha() );
183 switch ( color.spec() )
185 case QColor::Invalid:
189 case QColor::ExtendedRgb:
198 color.getRgbF( &red, &green, &blue, &alpha );
212 color.getHsvF( &h, &s, &v, &alpha );
226 color.getHslF( &h, &s, &l, &alpha );
241 color.getCmykF( &
c, &m, &y, &k, &alpha );
254 if (
string.isEmpty() )
257 const thread_local QRegularExpression rx( u
"^(.*),([a-z]+):([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),?([\\d\\.\\-]*)$"_s );
258 const QRegularExpressionMatch match = rx.match(
string );
259 if ( !match.hasMatch() )
262 const QStringList lst =
string.split(
',' );
263 if ( lst.count() < 3 )
265 return QColor(
string );
267 int red, green, blue, alpha;
268 red = lst[0].toInt();
269 green = lst[1].toInt();
270 blue = lst[2].toInt();
271 alpha = lst.count() > 3 ? lst[3].toInt() : 255;
272 return QColor( red, green, blue, alpha );
275 const QString spec = match.captured( 2 );
277 if ( spec ==
"rgb"_L1 )
280 const double red = match.captured( 3 ).toDouble();
281 const double green = match.captured( 4 ).toDouble();
282 const double blue = match.captured( 5 ).toDouble();
283 const double alpha = match.captured( 6 ).toDouble();
284 return QColor::fromRgbF( red, green, blue, alpha );
286 else if ( spec ==
"hsv"_L1 )
288 const double hue = match.captured( 3 ).toDouble();
289 const double saturation = match.captured( 4 ).toDouble();
290 const double value = match.captured( 5 ).toDouble();
291 const double alpha = match.captured( 6 ).toDouble();
292 return QColor::fromHsvF( hue, saturation, value, alpha );
294 else if ( spec ==
"hsl"_L1 )
296 const double hue = match.captured( 3 ).toDouble();
297 const double saturation = match.captured( 4 ).toDouble();
298 const double lightness = match.captured( 5 ).toDouble();
299 const double alpha = match.captured( 6 ).toDouble();
300 return QColor::fromHslF( hue, saturation, lightness, alpha );
302 else if ( spec ==
"cmyk"_L1 )
304 const double cyan = match.captured( 3 ).toDouble();
305 const double magenta = match.captured( 4 ).toDouble();
306 const double yellow = match.captured( 5 ).toDouble();
307 const double black = match.captured( 6 ).toDouble();
308 const double alpha = match.captured( 7 ).toDouble();
309 return QColor::fromCmykF( cyan, magenta, yellow, black, alpha );
316 if ( iccProfileFilePath.isEmpty() )
317 return QColorSpace();
319 QFile file( iccProfileFilePath );
320 if ( !file.open( QIODevice::ReadOnly ) )
322 errorMsg = QObject::tr(
"Failed to open ICC Profile: %1" ).arg( iccProfileFilePath );
323 return QColorSpace();
326 QColorSpace colorSpace = QColorSpace::fromIccProfile( file.readAll() );
327 if ( !colorSpace.isValid() )
329 errorMsg = QObject::tr(
"Invalid ICC Profile: %1" ).arg( iccProfileFilePath );
339 if ( !colorSpace.isValid() )
340 return QObject::tr(
"Invalid ICC profile" );
343 if ( !
iccProfile.open( QIODevice::WriteOnly ) )
344 return QObject::tr(
"File access error '%1'" ).arg( iccProfileFilePath );
346 if (
iccProfile.write( colorSpace.iccProfile() ) < 0 )
347 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.
A container for the context for various read/write operations on objects.