Class: QgsColorUtils

Contains utility functions for working with colors.

Added in version 3.28.

Static Methods

colorFromString

Decodes a string into a color value.

colorToString

Encodes a color into a string value.

iccProfile

Loads an ICC profile from iccProfileFilePath and returns associated color space.

readXml

Reads a color from an XML element, matching the specified identifier string.

saveIccProfile

Save color space colorSpace to an ICC profile file iccProfileFilePath.

writeXml

Writes a color to an XML element, storing it under the specified identifier.

class qgis.core.QgsColorUtils[source]

Bases: object

static colorFromString(string: str | None) QColor[source]

Decodes a string into a color value.

This method losslessly retrieves a color’s definition from a string value. All properties of the color are restored, including the color specification and original values of the color’s components. It is therefore suitable for restoring high color depth colors (such as 16 bit colors), or colors using alternative specifications such as CMYK colors.

An invalid color will be returned if the color could not be read.

See also

colorToString()

Parameters:

string (Optional[str])

Return type:

QColor

static colorToString(color: QColor | Qt.GlobalColor) str[source]

Encodes a color into a string value.

This method losslessly stores a color’s definition into a single string value. All properties of the color are stored, including the color specification and original values of the color’s components. It is therefore suitable for storing high color depth colors (such as 16 bit colors), or colors using alternative specifications such as CMYK colors.

Parameters:

color (Union[QColor, Qt.GlobalColor])

Return type:

str

static iccProfile(iccProfileFilePath: str | None)[source]

Loads an ICC profile from iccProfileFilePath and returns associated color space. If an error occurred, an invalid color space is returned and errorMsg is updated with error message

Parameters:

iccProfileFilePath (Optional[str]) -> (QColorSpace) – ICC profile file path

Returns:

  • loaded color space

  • errorMsg: Will be set to a user-friendly message if an error occurs while loading the ICC profile file

Added in version 3.40.

static readXml(element: QDomElement, identifier: str | None, context: QgsReadWriteContext) QColor[source]

Reads a color from an XML element, matching the specified identifier string.

This method losslessly retrieves a color’s definition from an XML element. All properties of the color are restored, including the color specification and original values of the color’s components. It is therefore suitable for restoring high color depth colors (such as 16 bit colors), or colors using alternative specifications such as CMYK colors.

An invalid color will be returned if the color could not be read.

See also

writeXml()

Parameters:
Return type:

QColor

static saveIccProfile(colorSpace: QColorSpace, iccProfileFilePath: str | None) str[source]

Save color space colorSpace to an ICC profile file iccProfileFilePath.

Return type:

str

Returns:

error message if an error occurred else empty string.

Added in version 3.40.

Parameters:
  • colorSpace (QColorSpace)

  • iccProfileFilePath (Optional[str])

static writeXml(color: QColor | Qt.GlobalColor, identifier: str | None, document: QDomDocument, element: QDomElement, context: QgsReadWriteContext)[source]

Writes a color to an XML element, storing it under the specified identifier.

This method losslessly stores a color’s definition in an XML element. All properties of the color are stored, including the color specification and original values of the color’s components. It is therefore suitable for storing high color depth colors (such as 16 bit colors), or colors using alternative specifications such as CMYK colors.

The identifier string is used to specify the element name for the stored color, allowing for multiple color definitions to be stored in a single element (assuming each uses a unique identifier string).

See also

readXml()

Parameters:
  • color (Union[QColor, Qt.GlobalColor])

  • identifier (Optional[str])

  • document (QDomDocument)

  • element (QDomElement)

  • context (QgsReadWriteContext)