pyvista.Color

Contents

pyvista.Color#

class Color(
color: ColorLike | None = None,
opacity: float | str | None = None,
default_color: ColorLike | None = None,
default_opacity: float | str = 255,
)[source]#

Helper class to convert between different color representations used in the pyvista library.

Many pyvista methods accept ColorLike parameters. This helper class is used to convert such parameters to the necessary format, used by underlying (VTK) methods. Any color name (str), hex string (str) or RGB(A) sequence (tuple, list or numpy.ndarray of int or float) is considered a ColorLike parameter and can be converted by this class.

See Color Table for a list of supported colors.

Parameters:
colorColorLike, optional

Either a string, RGB sequence, RGBA sequence, or hex color string. RGB(A) sequences should either be provided as floats between 0 and 1 or as ints between 0 and 255. Hex color strings can contain optional '#' or '0x' prefixes. If no opacity is provided, the default_opacity will be used. If color is None, the default_color is used instead. The following examples all denote the color ‘white’:

  • 'white'

  • 'w'

  • [1.0, 1.0, 1.0]

  • [255, 255, 255, 255]

  • '#FFFFFF'

opacityint | float | str, optional

Opacity of the represented color. Overrides any opacity associated with the provided color. Allowed opacities are floats between 0 and 1, ints between 0 and 255 or hexadecimal strings of length 2 (plus the length of the optional prefix). The following examples all denote a fully opaque color:

  • 1.0

  • 255

  • '#ff'

default_colorColorLike, optional

Default color to use when color is None. If this value is None, then defaults to the global theme color. Format is identical to color.

default_opacityint | float | str, optional

Default opacity of the represented color. Used when color does not specify an opacity and opacity is None. Format is identical to opacity.

Examples

Create a transparent green color using a color name, float RGBA sequence, integer RGBA sequence and RGBA hexadecimal string.

>>> import pyvista as pv
>>> pv.Color("green", opacity=0.5)
Color(name='green', hex='#00800080', opacity=128)
>>> pv.Color([0.0, 0.5, 0.0, 0.5])
Color(name='green', hex='#00800080', opacity=128)
>>> pv.Color([0, 128, 0, 128])
Color(name='green', hex='#00800080', opacity=128)
>>> pv.Color("#00800080")
Color(name='green', hex='#00800080', opacity=128)

Methods

Color.convert_color_channel(val)

Convert the given color channel value to the integer representation.

Color.from_dict(dict_)

Construct from dictionary for JSON deserialization.

Color.linear_to_srgb()

Convert from linear color values to sRGB color values.

Color.srgb_to_linear()

Convert from sRGB color values to linear color values.

Color.strip_hex_prefix(h)

Strip any '#' or '0x' prefix from a hexadecimal string.

Color.to_dict()

Convert to dictionary for JSON serialization.

Attributes

Color.float_rgb

Get the color value as an RGB float tuple.

Color.float_rgba

Get the color value as an RGBA float tuple.

Color.hex_rgb

Get the color value as an RGB hexadecimal value.

Color.hex_rgba

Get the color value as an RGBA hexadecimal value.

Color.int_rgb

Get the color value as an RGB integer tuple.

Color.int_rgba

Get the color value as an RGBA integer tuple.

Color.name

Get the color name.

Color.opacity

Return the opacity of this color in the range of (0-255).

Color.vtk_c3ub

Get the color value as a VTK Color3ub instance.