Class: QgsGradientColorRamp

Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra color stops.

Class Hierarchy

Inheritance diagram of qgis.core.QgsGradientColorRamp

Base classes

QgsColorRamp

Abstract base class for color ramps.

Subclasses

QgsCptCityColorRamp

A color ramp from the CPT City collection.

Methods

addStopsToGradient

Copy color ramp stops to a QGradient

color1

Returns the gradient start color.

color2

Returns the gradient end color.

colorSpec

Returns the color specification in which the color component interpolation will occur.

convertToDiscrete

Converts a gradient with existing color stops to or from discrete interpolation.

direction

Returns the direction to traverse the color wheel using when interpolating hue-based color specifications.

info

Returns any additional info attached to the gradient ramp (e.g., authorship notes)

isDiscrete

Returns True if the gradient is using discrete interpolation, rather than smoothly interpolating between colors.

setColor1

Sets the gradient start color.

setColor2

Sets the gradient end color.

setColorSpec

Sets the color specification in which the color component interpolation will occur.

setDirection

Sets the direction to traverse the color wheel using when interpolating hue-based color specifications.

setDiscrete

Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating between colors.

setInfo

Sets additional info to attach to the gradient ramp (e.g., authorship notes)

setStops

Sets the list of intermediate gradient stops for the ramp.

stops

Returns the list of intermediate gradient stops for the ramp.

Static Methods

create

Creates a new QgsColorRamp from a map of properties

typeString

Returns the string identifier for QgsGradientColorRamp.

class qgis.core.QgsGradientColorRamp[source]

Bases: QgsColorRamp

__init__(color1: QColor | Qt.GlobalColor = DEFAULT_GRADIENT_COLOR1, color2: QColor | Qt.GlobalColor = DEFAULT_GRADIENT_COLOR2, discrete: bool = False, stops: Iterable[QgsGradientStop] = [])

Constructor for QgsGradientColorRamp

Parameters:
  • color1 (Union[QColor, Qt.GlobalColor] = DEFAULT_GRADIENT_COLOR1) – start color, corresponding to a position of 0.0

  • color2 (Union[QColor, Qt.GlobalColor] = DEFAULT_GRADIENT_COLOR2) – end color, corresponding to a position of 1.0

  • discrete (bool = False) – set to True for discrete interpolation instead of smoothly interpolating between colors

  • stops (Iterable[QgsGradientStop] = []) – optional list of additional color stops

__init__(a0: QgsGradientColorRamp)
Parameters:

a0 (QgsGradientColorRamp)

addStopsToGradient(self, gradient: QGradient | None, opacity: float = 1)[source]

Copy color ramp stops to a QGradient

Parameters:
  • gradient (Optional[QGradient]) – gradient to copy stops into

  • opacity (float = 1) – opacity multiplier. Opacity of colors will be multiplied by this factor before adding to the gradient.

color1(self) QColor[source]

Returns the gradient start color.

See also

setColor1()

See also

color2()

Return type:

QColor

color2(self) QColor[source]

Returns the gradient end color.

See also

setColor2()

See also

color1()

Return type:

QColor

colorSpec(self) QColor.Spec[source]

Returns the color specification in which the color component interpolation will occur.

For multi-stop gradients this color spec will be used for the portion of the color ramp leading into the final stop (i.e. color2()).

See also

setColorSpec()

Added in version 3.24.

Return type:

QColor.Spec

convertToDiscrete(self, discrete: bool)[source]

Converts a gradient with existing color stops to or from discrete interpolation.

Parameters:

discrete (bool) – set to True to convert the gradient stops to discrete, or False to convert them to smooth interpolation

See also

isDiscrete()

static create(properties: Dict[str, Any] = {}) QgsColorRamp | None[source]

Creates a new QgsColorRamp from a map of properties

Parameters:

properties (Dict[str, Any] = {})

Return type:

Optional[QgsColorRamp]

direction(self) Qgis.AngularDirection[source]

Returns the direction to traverse the color wheel using when interpolating hue-based color specifications.

For multi-stop gradients this direction will be used for the portion of the color ramp leading into the final stop (i.e. color2()).

See also

setDirection()

Added in version 3.24.

Return type:

Qgis.AngularDirection

info(self) Dict[str, str][source]

Returns any additional info attached to the gradient ramp (e.g., authorship notes)

See also

setInfo()

Return type:

Dict[str, str]

isDiscrete(self) bool[source]

Returns True if the gradient is using discrete interpolation, rather than smoothly interpolating between colors.

See also

setDiscrete()

Return type:

bool

setColor1(self, color: QColor | Qt.GlobalColor)[source]

Sets the gradient start color.

Parameters:

color (Union[QColor, Qt.GlobalColor]) – start color

See also

color1()

See also

setColor2()

setColor2(self, color: QColor | Qt.GlobalColor)[source]

Sets the gradient end color.

Parameters:

color (Union[QColor, Qt.GlobalColor]) – end color

See also

color2()

See also

setColor1()

setColorSpec(self, spec: QColor.Spec)[source]

Sets the color specification in which the color component interpolation will occur.

Only QColor.Spec.Rgb, QColor.Spec.Hsv and QColor.Spec.Hsl are currently supported.

For multi-stop gradients this color spec will be used for the portion of the color ramp leading into the final stop (i.e. color2()).

See also

colorSpec()

Added in version 3.24.

Parameters:

spec (QColor.Spec)

setDirection(self, direction: Qgis.AngularDirection)[source]

Sets the direction to traverse the color wheel using when interpolating hue-based color specifications.

For multi-stop gradients this direction will be used for the portion of the color ramp leading into the final stop (i.e. color2()).

See also

direction()

Added in version 3.24.

Parameters:

direction (Qgis.AngularDirection)

setDiscrete(self, discrete: bool)[source]

Sets whether the gradient should use discrete interpolation, rather than smoothly interpolating between colors.

Parameters:

discrete (bool) – set to True to use discrete interpolation

See also

isDiscrete()

setInfo(self, info: Dict[str | None, str | None])[source]

Sets additional info to attach to the gradient ramp (e.g., authorship notes)

Parameters:

info (Dict[Optional[str], Optional[str]]) – map of string info to attach

See also

info()

setStops(self, stops: Iterable[QgsGradientStop])[source]

Sets the list of intermediate gradient stops for the ramp.

Parameters:

stops (Iterable[QgsGradientStop]) – list of stops. Any existing color stops will be replaced. The stop list will be automatically reordered so that stops are listed in ascending offset order.

See also

stops()

stops(self) List[QgsGradientStop][source]

Returns the list of intermediate gradient stops for the ramp.

See also

setStops()

Return type:

List[QgsGradientStop]

static typeString() str[source]

Returns the string identifier for QgsGradientColorRamp.

Added in version 3.16.

Return type:

str