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( u
"invalid"_s, u
"1"_s );
43 switch ( color.spec() )
49 case QColor::ExtendedRgb:
57 color.getRgbF( &red, &green, &blue );
72 color.getHsvF( &h, &s, &v );
87 color.getHslF( &h, &s, &l );
103 color.getCmykF( &
c, &y, &m, &k );
111 colorElement.setAttribute( u
"spec"_s, spec );
112 if ( color.alphaF() < 1.0 )
117 element.appendChild( colorElement );
122 const QDomElement colorElement = element.firstChildElement( identifier );
123 if ( colorElement.isNull() )
126 const bool invalid = colorElement.attribute( u
"invalid"_s, u
"0"_s ).toInt();
131 const QString spec = colorElement.attribute( u
"spec"_s );
132 if ( spec ==
"rgb"_L1 )
135 const double red = colorElement.attribute( u
"red"_s ).toDouble();
136 const double green = colorElement.attribute( u
"green"_s ).toDouble();
137 const double blue = colorElement.attribute( u
"blue"_s ).toDouble();
138 res = QColor::fromRgbF( red, green, blue );
140 else if ( spec ==
"hsv"_L1 )
142 const double hue = colorElement.attribute( u
"hue"_s ).toDouble();
143 const double saturation = colorElement.attribute( u
"saturation"_s ).toDouble();
144 const double value = colorElement.attribute( u
"value"_s ).toDouble();
145 res = QColor::fromHsvF( hue, saturation, value );
147 else if ( spec ==
"hsl"_L1 )
149 const double hue = colorElement.attribute( u
"hue"_s ).toDouble();
150 const double saturation = colorElement.attribute( u
"saturation"_s ).toDouble();
151 const double value = colorElement.attribute( u
"lightness"_s ).toDouble();
152 res = QColor::fromHslF( hue, saturation, value );
154 else if ( spec ==
"cmyk"_L1 )
156 const double cyan = colorElement.attribute( u
"c"_s ).toDouble();
157 const double magenta = colorElement.attribute( u
"m"_s ).toDouble();
158 const double yellow = colorElement.attribute( u
"y"_s ).toDouble();
159 const double black = colorElement.attribute( u
"k"_s ).toDouble();
160 res = QColor::fromCmykF( cyan, magenta, yellow, black );
164 const double alpha = colorElement.attribute( u
"alpha"_s, u
"1"_s ).toDouble();
165 res.setAlphaF( alpha );
173 if ( !color.isValid() )
178 const QString compatString = u
"%1,%2,%3,%4,"_s.arg( color.red() ).arg( color.green() ).arg( color.blue() ).arg( color.alpha() );
180 switch ( color.spec() )
182 case QColor::Invalid:
186 case QColor::ExtendedRgb:
195 color.getRgbF( &red, &green, &blue, &alpha );
209 color.getHsvF( &h, &s, &v, &alpha );
223 color.getHslF( &h, &s, &l, &alpha );
238 color.getCmykF( &
c, &m, &y, &k, &alpha );
251 if (
string.isEmpty() )
254 const thread_local QRegularExpression rx( u
"^(.*),([a-z]+):([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),([\\d\\.\\-]+),?([\\d\\.\\-]*)$"_s );
255 const QRegularExpressionMatch match = rx.match(
string );
256 if ( !match.hasMatch() )
259 const QStringList lst =
string.split(
',' );
260 if ( lst.count() < 3 )
262 return QColor(
string );
264 int red, green, blue, alpha;
265 red = lst[0].toInt();
266 green = lst[1].toInt();
267 blue = lst[2].toInt();
268 alpha = lst.count() > 3 ? lst[3].toInt() : 255;
269 return QColor( red, green, blue, alpha );
272 const QString spec = match.captured( 2 );
274 if ( spec ==
"rgb"_L1 )
277 const double red = match.captured( 3 ).toDouble();
278 const double green = match.captured( 4 ).toDouble();
279 const double blue = match.captured( 5 ).toDouble();
280 const double alpha = match.captured( 6 ).toDouble();
281 return QColor::fromRgbF( red, green, blue, alpha );
283 else if ( spec ==
"hsv"_L1 )
285 const double hue = match.captured( 3 ).toDouble();
286 const double saturation = match.captured( 4 ).toDouble();
287 const double value = match.captured( 5 ).toDouble();
288 const double alpha = match.captured( 6 ).toDouble();
289 return QColor::fromHsvF( hue, saturation, value, alpha );
291 else if ( spec ==
"hsl"_L1 )
293 const double hue = match.captured( 3 ).toDouble();
294 const double saturation = match.captured( 4 ).toDouble();
295 const double lightness = match.captured( 5 ).toDouble();
296 const double alpha = match.captured( 6 ).toDouble();
297 return QColor::fromHslF( hue, saturation, lightness, alpha );
299 else if ( spec ==
"cmyk"_L1 )
301 const double cyan = match.captured( 3 ).toDouble();
302 const double magenta = match.captured( 4 ).toDouble();
303 const double yellow = match.captured( 5 ).toDouble();
304 const double black = match.captured( 6 ).toDouble();
305 const double alpha = match.captured( 7 ).toDouble();
306 return QColor::fromCmykF( cyan, magenta, yellow, black, alpha );
313 if ( iccProfileFilePath.isEmpty() )
314 return QColorSpace();
316 QFile file( iccProfileFilePath );
317 if ( !file.open( QIODevice::ReadOnly ) )
319 errorMsg = QObject::tr(
"Failed to open ICC Profile: %1" ).arg( iccProfileFilePath );
320 return QColorSpace();
323 QColorSpace colorSpace = QColorSpace::fromIccProfile( file.readAll() );
324 if ( !colorSpace.isValid() )
326 errorMsg = QObject::tr(
"Invalid ICC Profile: %1" ).arg( iccProfileFilePath );
336 if ( !colorSpace.isValid() )
337 return QObject::tr(
"Invalid ICC profile" );
340 if ( !
iccProfile.open( QIODevice::WriteOnly ) )
341 return QObject::tr(
"File access error '%1'" ).arg( iccProfileFilePath );
343 if (
iccProfile.write( colorSpace.iccProfile() ) < 0 )
344 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.