pyvista.Color#
- class Color(
- color: ColorLike | None = None,
- opacity: float | str | None = None,
- default_color: ColorLike | None = None,
- default_opacity: float | str = 255,
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
ornumpy.ndarray
ofint
orfloat
) is considered aColorLike
parameter and can be converted by this class.See Color Table for a list of supported colors.
- Parameters:
- color
ColorLike
,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, thedefault_opacity
will be used. Ifcolor
isNone
, thedefault_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'
- opacity
int
|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_color
ColorLike
,optional
Default color to use when
color
isNone
. If this value isNone
, then defaults to the global theme color. Format is identical tocolor
.- default_opacity
int
|float
|str
,optional
Default opacity of the represented color. Used when
color
does not specify an opacity andopacity
isNone
. Format is identical toopacity
.
- color
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
Convert the given color channel value to the integer representation.
Color.from_dict
(dict_)Construct from dictionary for JSON deserialization.
Convert from linear color values to sRGB color values.
Convert from sRGB color values to linear color values.
Strip any
'#'
or'0x'
prefix from a hexadecimal string.Convert to dictionary for JSON serialization.
Attributes
Get the color value as an RGB float tuple.
Get the color value as an RGBA float tuple.
Get the color value as an RGB hexadecimal value.
Get the color value as an RGBA hexadecimal value.
Get the color value as an RGB integer tuple.
Get the color value as an RGBA integer tuple.
Get the color name.
Return the opacity of this color in the range of
(0-255)
.Get the color value as a VTK Color3ub instance.