QgsColorRampTransformer class

QgsPropertyTransformer subclass for transforming a numeric value into a color from a color ramp.

Base classes

class QgsPropertyTransformer
Abstract base class for objects which transform the calculated value of a property.

Constructors, destructors, conversion operators

QgsColorRampTransformer(double minValue = 0.0, double maxValue = 1.0, QgsColorRamp* ramp = nullptr, const QColor& nullColor = QColor(0, 0, 0, 0))
Constructor for QgsColorRampTransformer.
QgsColorRampTransformer(const QgsColorRampTransformer& other)
Copy constructor.

Public functions

auto clone() const -> QgsColorRampTransformer* override
Returns a clone of the transformer.
auto color(double value) const -> QColor
Calculates the color corresponding to a specific value.
auto colorRamp() const -> QgsColorRamp*
Returns the color ramp used for calculating property colors.
auto loadVariant(const QVariant& transformer) -> bool override
Loads this transformer from a QVariantMap, wrapped in a QVariant.
auto nullColor() const -> QColor
Returns the color corresponding to a null value.
auto rampName() const -> QString
Returns the color ramp's name.
void setColorRamp(QgsColorRamp* ramp)
Sets the color ramp to use for calculating property colors.
void setNullColor(const QColor& color)
Sets the color corresponding to a null value.
void setRampName(const QString& name)
Sets the color ramp's name.
auto toExpression(const QString& baseExpression) const -> QString override
Converts the transformer to a QGIS expression string.
auto toVariant() const -> QVariant override
Saves this transformer to a QVariantMap, wrapped in a QVariant.
auto transform(const QgsExpressionContext& context, const QVariant& value) const -> QVariant override
Calculates the transform of a value.
auto transformerType() const -> Type override
Returns the transformer type.

Function documentation

QgsColorRampTransformer::QgsColorRampTransformer(double minValue = 0.0, double maxValue = 1.0, QgsColorRamp* ramp = nullptr, const QColor& nullColor = QColor(0, 0, 0, 0))

Constructor for QgsColorRampTransformer.

Parameters
minValue minimum expected value
maxValue maximum expected value
ramp source color ramp. Ownership is transferred to the transformer.
nullColor color to return for null values

QColor QgsColorRampTransformer::color(double value) const

Calculates the color corresponding to a specific value.

Parameters
value value to calculate color for
Returns calculated color using transformer's parameters and type

QgsColorRamp* QgsColorRampTransformer::colorRamp() const

Returns the color ramp used for calculating property colors.

Returns color ramp

bool QgsColorRampTransformer::loadVariant(const QVariant& transformer) override

Loads this transformer from a QVariantMap, wrapped in a QVariant.

You can use QgsXmlUtils::readVariant to read it from an XML document.

QColor QgsColorRampTransformer::nullColor() const

Returns the color corresponding to a null value.

QString QgsColorRampTransformer::rampName() const

Returns the color ramp's name.

void QgsColorRampTransformer::setColorRamp(QgsColorRamp* ramp)

Sets the color ramp to use for calculating property colors.

Parameters
ramp color ramp, ownership of ramp is transferred to the transformer.

void QgsColorRampTransformer::setNullColor(const QColor& color)

Sets the color corresponding to a null value.

Parameters
color null color

void QgsColorRampTransformer::setRampName(const QString& name)

Sets the color ramp's name.

The ramp name must be set to match a color ramp available in the style database for conversion to expression to work correctly.

QString QgsColorRampTransformer::toExpression(const QString& baseExpression) const override

Converts the transformer to a QGIS expression string.

The baseExpression string consists of a sub-expression reflecting the parent property's state.

QVariant QgsColorRampTransformer::toVariant() const override

Saves this transformer to a QVariantMap, wrapped in a QVariant.

You can use QgsXmlUtils::writeVariant to save it to an XML document.

QVariant QgsColorRampTransformer::transform(const QgsExpressionContext& context, const QVariant& value) const override

Calculates the transform of a value.

Parameters
context expression context
value input value to transform

Derived classes must implement this to perform their transformations on input values