ManimColor¶
Qualified name: manim.utils.color.core.ManimColor
- class ManimColor(value, alpha=1.0)[source]¶
Bases:
objectInternal representation of a color.
The
ManimColorclass is the main class for the representation of a color. Its internal representation is an array of 4 floats corresponding to a[r,g,b,a]value wherer,g,b,acan be between 0.0 and 1.0.This is done in order to reduce the amount of color inconsistencies by constantly casting between integers and floats which introduces errors.
The class can accept any value of type
ParsableManimColori.e.ManimColor, int, str, RGB_Tuple_Int, RGB_Tuple_Float, RGBA_Tuple_Int, RGBA_Tuple_Float, RGB_Array_Int, RGB_Array_Float, RGBA_Array_Int, RGBA_Array_FloatManimColoritself only accepts singular values and will directly interpret them into a single color if possible. Be careful when passing strings toManimColor: it can create a big overhead for the color processing.If you want to parse a list of colors, use the
parse()method, which assumes that you’re going to pass a list of colors so that arrays will not be interpreted as a single color.Warning
If you pass an array of numbers to
parse(), it will interpret ther,g,b,anumbers in that array as colors: Instead of the expected singular color, you will get an array with 4 colors.For conversion behaviors, see the
_internalfunctions for further documentation.You can create a
ManimColorinstance via its classmethods. See the respective methods for more info.mycolor = ManimColor.from_rgb((0, 1, 0.4, 0.5)) myothercolor = ManimColor.from_rgb((153, 255, 255))
You can also convert between different color spaces:
mycolor_hex = mycolor.to_hex() myoriginalcolor = ManimColor.from_hex(mycolor_hex).to_hsv()
- Parameters:
value (ParsableManimColor | None) – Some representation of a color (e.g., a string or a suitable tuple). The default
NoneisBLACK.alpha (float) – The opacity of the color. By default, colors are fully opaque (value 1.0).
Methods
Return one of two colors, light or dark (by default white or black), that contrasts with the current color (depending on its luminance).
Return a new color that is darker than the current color, i.e. interpolated with
BLACK.Create a
ManimColorfrom a hex string.Create a
ManimColorfrom an HSL array.Create a
ManimColorfrom an HSV array.Create a ManimColor from an RGB array.
Create a ManimColor from an RGBA Array.
This method is currently not implemented.
Interpolate between the current and the given
ManimColor, and return the result.Convert the current color into a different colorspace given by
class_type, without changing the_internal_value.Return a new, linearly inverted version of this
ManimColor(no inplace changes).Return a new color that is lighter than the current color, i.e. interpolated with
WHITE.Create a new
ManimColorwith the given opacity and the same color values as before.Parse one color as a
ManimColoror a sequence of colors as a list ofManimColor's.Convert the
ManimColorto a hexadecimal representation of the color.Convert the
ManimColorto an HSL array.Convert the
ManimColorto an HSV array.Convert the current
ManimColorinto an RGB array of integers.Convert the current ManimColor into an RGBA array of integers.
Convert the current
ManimColorinto an RGBA array of integers.Convert the current
ManimColorinto an integer.Convert the current
ManimColorinto an RGB array of floats.Convert the current
ManimColorinto an RGBA array of floats.Convert the current
ManimColorinto an RGBA array of floats.- classmethod _construct_from_space(_space)[source]¶
This function is used as a proxy for constructing a color with an internal value. This can be used by subclasses to hook into the construction of new objects using the internal value format.
- Parameters:
_space (ndarray[tuple[Any, ...], dtype[TypeAliasForwardRef('~manim.typing.ManimFloat')]] | tuple[float, float, float] | tuple[float, float, float, float])
- Return type:
Self
- classmethod _from_internal(value)[source]¶
This method is intended to be overwritten by custom color space classes which are subtypes of
ManimColor.The method constructs a new object of the given class by transforming the value in the internal format
[r,g,b,a]into a format which the constructor of the custom class can understand. Look atHSVfor an example.- Parameters:
value (ManimColorInternal)
- Return type:
Self
- static _internal_from_hex_string(hex_, alpha)[source]¶
Internal function for converting a hex string into the internal representation of a
ManimColor.Warning
This does not accept any prefixes like # or similar in front of the hex string. This is just intended for the raw hex part.
For internal use only
- Parameters:
hex – Hex string to be parsed.
alpha (float) – Alpha value used for the color, if the color is only 3 bytes long. Otherwise, if the color is 4 bytes long, this parameter will not be used.
hex_ (str)
- Returns:
Internal color representation
- Return type:
- static _internal_from_int_rgb(rgb, alpha=1.0)[source]¶
Internal function for converting an RGB tuple of integers into the internal representation of a
ManimColor.For internal use only
- Parameters:
rgb (IntRGBLike) – Integer RGB tuple to be parsed
alpha (float) – Optional alpha value. Default is 1.0.
- Returns:
Internal color representation.
- Return type:
- static _internal_from_int_rgba(rgba)[source]¶
Internal function for converting an RGBA tuple of integers into the internal representation of a
ManimColor.For internal use only
- Parameters:
rgba (IntRGBALike) – Int RGBA tuple to be parsed.
- Returns:
Internal color representation.
- Return type:
- static _internal_from_rgb(rgb, alpha=1.0)[source]¶
Internal function for converting a rgb tuple of floats into the internal representation of a
ManimColor.For internal use only
- Parameters:
rgb (FloatRGBLike) – Float RGB tuple to be parsed.
alpha (float) – Optional alpha value. Default is 1.0.
- Returns:
Internal color representation.
- Return type:
- static _internal_from_rgba(rgba)[source]¶
Internal function for converting an RGBA tuple of floats into the internal representation of a
ManimColor.For internal use only
- Parameters:
rgba (FloatRGBALike) – Int RGBA tuple to be parsed.
- Returns:
Internal color representation.
- Return type:
- static _internal_from_string(name, alpha)[source]¶
Internal function for converting a string into the internal representation of a
ManimColor. This is not used for hex strings: please refer to_internal_from_hex()for this functionality.For internal use only
- Parameters:
name (str) – The color name to be parsed into a color. Refer to the different color modules in the documentation page to find the corresponding color names.
alpha (float)
- Returns:
Internal color representation.
- Return type:
- Raises:
ValueError – If the color name is not present in Manim.
- property _internal_space: ndarray[tuple[Any, ...], dtype[TypeAliasForwardRef('~manim.typing.ManimFloat')]]¶
This is a readonly property which is a custom representation for color space operations. It is used for operators and can be used when implementing a custom color space.
- property _internal_value: ManimColorInternal¶
Return the internal value of the current Manim color
[r,g,b,a]float array.- Returns:
Internal color representation.
- Return type:
- contrasting(threshold=0.5, light=None, dark=None)[source]¶
Return one of two colors, light or dark (by default white or black), that contrasts with the current color (depending on its luminance). This is typically used to set text in a contrasting color that ensures it is readable against a background of the current color.
- Parameters:
threshold (float) – The luminance threshold which dictates whether the current color is considered light or dark (and thus whether to return the dark or light color, respectively). Default is 0.5.
light (Self | None) – The light color to return if the current color is considered dark. Default is
None: in this case, pureWHITEwill be returned.dark (Self | None) – The dark color to return if the current color is considered light, Default is
None: in this case, pureBLACKwill be returned.
- Returns:
The contrasting
ManimColor.- Return type:
- darker(blend=0.2)[source]¶
Return a new color that is darker than the current color, i.e. interpolated with
BLACK. The opacity is unchanged.- Parameters:
blend (float) – The blend ratio for the interpolation, from 0.0 (the current color unchanged) to 1.0 (pure black). Default is 0.2, which results in a slightly darker color.
- Returns:
The darker
ManimColor.- Return type:
See also
- classmethod from_hex(hex_str, alpha=1.0)[source]¶
Create a
ManimColorfrom a hex string.- Parameters:
hex_str (str) – The hex string to be converted. The allowed prefixes for this string are
#and0x. Currently, this method only supports 6 nibbles, i.e. only strings in the format#XXXXXXor0xXXXXXX.alpha (float) – Alpha value to be used for the hex string. Default is 1.0.
- Returns:
The
ManimColorrepresented by the hex string.- Return type:
- classmethod from_hsl(hsl, alpha=1.0)[source]¶
Create a
ManimColorfrom an HSL array.- Parameters:
hsl (FloatHSLLike) – Any iterable containing 3 floats from 0.0 to 1.0.
alpha (float) – The alpha value to be used. Default is 1.0.
- Returns:
The
ManimColorwith the corresponding RGB values to the given HSL array.- Return type:
- classmethod from_hsv(hsv, alpha=1.0)[source]¶
Create a
ManimColorfrom an HSV array.- Parameters:
hsv (FloatHSVLike) – Any iterable containing 3 floats from 0.0 to 1.0.
alpha (float) – The alpha value to be used. Default is 1.0.
- Returns:
The
ManimColorwith the corresponding RGB values to the given HSV array.- Return type:
- classmethod from_rgb(rgb, alpha=1.0)[source]¶
Create a ManimColor from an RGB array. Automagically decides which type it is:
intorfloat.Warning
Please make sure that your elements are not floats if you want integers. A
5.0will result in the input being interpreted as if it was an RGB float array with the value5.0and not the integer5.- Parameters:
rgb (TypeAliasForwardRef('~manim.typing.FloatRGBLike') | TypeAliasForwardRef('~manim.typing.IntRGBLike')) – Any iterable of 3 floats or 3 integers.
alpha (float) – Alpha value to be used in the color. Default is 1.0.
- Returns:
The
ManimColorwhich corresponds to the givenrgb.- Return type:
- classmethod from_rgba(rgba)[source]¶
Create a ManimColor from an RGBA Array. Automagically decides which type it is:
intorfloat.Warning
Please make sure that your elements are not floats if you want integers. A
5.0will result in the input being interpreted as if it was a float RGB array with the float5.0and not the integer5.- Parameters:
rgba (TypeAliasForwardRef('~manim.typing.FloatRGBALike') | TypeAliasForwardRef('~manim.typing.IntRGBALike')) – Any iterable of 4 floats or 4 integers.
- Returns:
The
ManimColorcorresponding to the givenrgba.- Return type:
- static gradient(colors, length)[source]¶
This method is currently not implemented. Refer to
color_gradient()for a working implementation for now.- Parameters:
colors (list[ManimColor])
length (int)
- Return type:
ManimColor | list[ManimColor]
- interpolate(other, alpha)[source]¶
Interpolate between the current and the given
ManimColor, and return the result.- Parameters:
other (Self) – The other
ManimColorto be used for interpolation.alpha (float) – A point on the line in RGBA colorspace connecting the two colors, i.e. the interpolation point. 0.0 corresponds to the current
ManimColorand 1.0 corresponds to the otherManimColor.
- Returns:
The interpolated
ManimColor.- Return type:
- into(class_type)[source]¶
Convert the current color into a different colorspace given by
class_type, without changing the_internal_value.- Parameters:
class_type (type[ManimColorT]) – The class that is used for conversion. It must be a subclass of
ManimColorwhich respects the specification HSV, RGBA, …- Returns:
A new color object of type
class_typeand the same_internal_valueas the original color.- Return type:
- invert(with_alpha=False)[source]¶
Return a new, linearly inverted version of this
ManimColor(no inplace changes).- Parameters:
with_alpha (bool) –
If
True, the alpha value will be inverted too. Default isFalse.Note
Setting
with_alpha=Truecan result in unintended behavior where objects are not displayed because their new alpha value is suddenly 0 or very low.- Returns:
The linearly inverted
ManimColor.- Return type:
- lighter(blend=0.2)[source]¶
Return a new color that is lighter than the current color, i.e. interpolated with
WHITE. The opacity is unchanged.- Parameters:
blend (float) – The blend ratio for the interpolation, from 0.0 (the current color unchanged) to 1.0 (pure white). Default is 0.2, which results in a slightly lighter color.
- Returns:
The lighter
ManimColor.- Return type:
See also
- opacity(opacity)[source]¶
Create a new
ManimColorwith the given opacity and the same color values as before.- Parameters:
opacity (float) – The new opacity value to be used.
- Returns:
The new
ManimColorwith the same color values and the new opacity.- Return type:
- classmethod parse(color: ParsableManimColor | None, alpha: float = 1.0) Self[source]¶
- classmethod parse(color: Sequence[ParsableManimColor], alpha: float = 1.0) list[Self]
Parse one color as a
ManimColoror a sequence of colors as a list ofManimColor’s.- Parameters:
color (TypeAliasForwardRef('~manim.utils.color.core.ParsableManimColor') | Sequence[TypeAliasForwardRef('~manim.utils.color.core.ParsableManimColor')] | None) – The color or list of colors to parse. Note that this function can not accept tuples: it will assume that you mean
Sequence[ParsableManimColor]and will return alist[ManimColor].alpha (float) – The alpha (opacity) value to use for the passed color(s).
- Returns:
Either a list of colors or a singular color, depending on the input.
- Return type:
ManimColor | list[ManimColor]
- to_hex(with_alpha=False)[source]¶
Convert the
ManimColorto a hexadecimal representation of the color.- Parameters:
with_alpha (bool) – If
True, append 2 extra characters to the hex string which represent the alpha value of the color between 0 and 255. Default isFalse.- Returns:
A hex string starting with a
#, with either 6 or 8 nibbles depending on thewith_alphaparameter. By default, it has 6 nibbles, i.e.#XXXXXX.- Return type:
str
- to_hsl()[source]¶
Convert the
ManimColorto an HSL array.Note
Be careful: this returns an array in the form
[h, s, l], where the elements are floats. This might be confusing, because RGB can also be an array of floats. You might want to annotate the usage of this function in your code by typing your HSL array variables asHSL_Array_Floatin order to differentiate them from RGB arrays.- Returns:
An HSL array of 3 floats from 0.0 to 1.0.
- Return type:
HSL_Array_Float
- to_hsv()[source]¶
Convert the
ManimColorto an HSV array.Note
Be careful: this returns an array in the form
[h, s, v], where the elements are floats. This might be confusing, because RGB can also be an array of floats. You might want to annotate the usage of this function in your code by typing your HSV array variables asHSV_Array_Floatin order to differentiate them from RGB arrays.- Returns:
An HSV array of 3 floats from 0.0 to 1.0.
- Return type:
HSV_Array_Float
- to_int_rgb()[source]¶
Convert the current
ManimColorinto an RGB array of integers.- Returns:
RGB array of 3 integers from 0 to 255.
- Return type:
RGB_Array_Int
- to_int_rgba()[source]¶
Convert the current ManimColor into an RGBA array of integers.
- Returns:
RGBA array of 4 integers from 0 to 255.
- Return type:
RGBA_Array_Int
- to_int_rgba_with_alpha(alpha)[source]¶
Convert the current
ManimColorinto an RGBA array of integers. This is similar toto_int_rgba(), but you can change the alpha value.- Parameters:
alpha (float) – Alpha value to be used for the return value. Pass a float between 0.0 and 1.0: it will automatically be scaled to an integer between 0 and 255.
- Returns:
RGBA array of 4 integers from 0 to 255.
- Return type:
RGBA_Array_Int
- to_integer()[source]¶
Convert the current
ManimColorinto an integer.Warning
This will return only the RGB part of the color.
- Returns:
Integer representation of the color.
- Return type:
int
- to_rgb()[source]¶
Convert the current
ManimColorinto an RGB array of floats.- Returns:
RGB array of 3 floats from 0.0 to 1.0.
- Return type:
RGB_Array_Float
- to_rgba()[source]¶
Convert the current
ManimColorinto an RGBA array of floats.- Returns:
RGBA array of 4 floats from 0.0 to 1.0.
- Return type:
RGBA_Array_Float
- to_rgba_with_alpha(alpha)[source]¶
Convert the current
ManimColorinto an RGBA array of floats. This is similar toto_rgba(), but you can change the alpha value.- Parameters:
alpha (float) – Alpha value to be used in the return value.
- Returns:
RGBA array of 4 floats from 0.0 to 1.0.
- Return type:
RGBA_Array_Float