core¶
Manim’s (internal) color data structure and some utilities for color conversion.
This module contains the implementation of ManimColor, the data structure
internally used to represent colors.
The preferred way of using these colors is by importing their constants from Manim:
>>> from manim import RED, GREEN, BLUE
>>> print(RED)
#FC6255
Note that this way uses the name of the colors in UPPERCASE.
Note
The colors with a _C suffix have an alias equal to the colorname without a
letter. For example, GREEN = GREEN_C.
Custom Color Spaces¶
Hello, dear visitor. You seem to be interested in implementing a custom color class for a color space we don’t currently support.
The current system is using a few indirections for ensuring a consistent behavior with all other color types in Manim.
To implement a custom color space, you must subclass ManimColor and implement
three important methods:
_internal_value: a@propertyimplemented onManimColorwith the goal of keeping a consistent internal representation which can be referenced by other functions inManimColor. This property acts as a proxy to whatever representation you need in your class.The getter should always return a NumPy array in the format
[r,g,b,a], in accordance with the typeManimColorInternal.The setter should always accept a value in the format
[r,g,b,a]which can be converted to whatever attributes you need.
_internal_space: a read-only@propertyimplemented onManimColorwith the goal of providing a useful representation which can be used by operators, interpolation and color transform functions.The only constraints on this value are:
It must be a NumPy array.
The last value must be the opacity in a range
0.0to1.0.
Additionally, your
__init__must support this format as an initialization value without additional parameters to ensure correct functionality of all other methods inManimColor._from_internal(): a@classmethodwhich converts an[r,g,b,a]value into suitable parameters for your__init__method and calls theclsparameter.
Type Aliases
- class ParsableManimColor¶
ManimColor | int | str |
IntRGBLike|FloatRGBLike|IntRGBALike|FloatRGBALikeParsableManimColorrepresents all the types which can be parsed to aManimColorin Manim.
TypeVar’s
- class ManimColorT¶
TypeVar('ManimColorT', bound=ManimColor)
Classes
HSV Color Space |
|
Internal representation of a color. |
|
RGBA Color Space |
|
A generator for producing random colors from a given list of Manim colors, optionally in a reproducible sequence using a seed value. |
Functions
- average_color(*colors)[source]¶
Determine the average color between the given parameters.
Note
This operation does not consider the alphas (opacities) of the colors. The generated color has an alpha or opacity of 1.0.
- Returns:
The average color of the input.
- Return type:
- Parameters:
colors (ParsableManimColor)
- color_gradient(reference_colors, length_of_output)[source]¶
Create a list of colors interpolated between the input array of colors with a specific number of colors.
- Parameters:
reference_colors (Iterable[TypeAliasForwardRef('~manim.utils.color.core.ParsableManimColor')]) – The colors to be interpolated between or spread apart.
length_of_output (int) – The number of colors that the output should have, ideally more than the input.
- Returns:
A list of interpolated
ManimColor’s.- Return type:
list[ManimColor]
- color_to_int_rgb(color)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.to_int_rgb().- Parameters:
color (ParsableManimColor) – A color to convert to an RGB integer array.
- Returns:
The corresponding RGB integer array.
- Return type:
RGB_Array_Int
- color_to_int_rgba(color, alpha=1.0)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.to_int_rgba_with_alpha().- Parameters:
color (ParsableManimColor) – A color to convert to an RGBA integer array.
alpha (float) – An alpha value between 0.0 and 1.0 to be used as opacity in the color. Default is 1.0.
- Returns:
The corresponding RGBA integer array.
- Return type:
RGBA_Array_Int
- color_to_rgb(color)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.to_rgb().- Parameters:
color (ParsableManimColor) – A color to convert to an RGB float array.
- Returns:
The corresponding RGB float array.
- Return type:
RGB_Array_Float
- color_to_rgba(color, alpha=1.0)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.to_rgba_with_alpha().- Parameters:
color (ParsableManimColor) – A color to convert to an RGBA float array.
alpha (float) – An alpha value between 0.0 and 1.0 to be used as opacity in the color. Default is 1.0.
- Returns:
The corresponding RGBA float array.
- Return type:
RGBA_Array_Float
- get_shaded_rgb(rgb, point, unit_normal_vect, light_source)[source]¶
Add light or shadow to the
rgbcolor of some surface which is located at a givenpointin space and facing in the direction ofunit_normal_vect, depending on whether the surface is facing alight_sourceor away from it.- Parameters:
- Returns:
The color with added light or shadow, depending on the direction of the colored surface.
- Return type:
RGB_Array_Float
- hex_to_rgb(hex_code)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.to_rgb().- Parameters:
hex_code (str) – A hex string representing a color.
- Returns:
An RGB array representing the color.
- Return type:
RGB_Array_Float
- interpolate_color(color1, color2, alpha)[source]¶
Standalone function to interpolate two ManimColors and get the result. Refer to
ManimColor.interpolate().- Parameters:
color1 (ManimColorT) – The first
ManimColor.color2 (ManimColorT) – The second
ManimColor.alpha (float) – The alpha value determining the point of interpolation between the colors.
- Returns:
The interpolated ManimColor.
- Return type:
- invert_color(color)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.invert()- Parameters:
color (ManimColorT) – The
ManimColorto invert.- Returns:
The linearly inverted
ManimColor.- Return type:
- random_bright_color()[source]¶
Return a random bright color: a random color averaged with
WHITE.Warning
This operation is very expensive. Please keep in mind the performance loss.
- Returns:
A random bright
ManimColor.- Return type:
- random_color()[source]¶
Return a random
ManimColor.- Returns:
A random
ManimColor.- Return type:
- rgb_to_color(rgb)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.from_rgb().- Parameters:
rgb (TypeAliasForwardRef('~manim.typing.FloatRGBLike') | TypeAliasForwardRef('~manim.typing.IntRGBLike')) – A 3 element iterable.
- Returns:
A ManimColor with the corresponding value.
- Return type:
- rgb_to_hex(rgb)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.from_rgb()andManimColor.to_hex().- Parameters:
rgb (TypeAliasForwardRef('~manim.typing.FloatRGBLike') | TypeAliasForwardRef('~manim.typing.IntRGBLike')) – A 3 element iterable.
- Returns:
A hex representation of the color.
- Return type:
str
- rgba_to_color(rgba)[source]¶
Helper function for use in functional style programming. Refer to
ManimColor.from_rgba().- Parameters:
rgba (TypeAliasForwardRef('~manim.typing.FloatRGBALike') | TypeAliasForwardRef('~manim.typing.IntRGBALike')) – A 4 element iterable.
- Returns:
A ManimColor with the corresponding value
- Return type: