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 @property implemented on ManimColor with the goal of keeping a consistent internal representation which can be referenced by other functions in ManimColor. 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 type ManimColorInternal.

    • 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 @property implemented on ManimColor with 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.0 to 1.0.

    Additionally, your __init__ must support this format as an initialization value without additional parameters to ensure correct functionality of all other methods in ManimColor.

  • _from_internal(): a @classmethod which converts an [r,g,b,a] value into suitable parameters for your __init__ method and calls the cls parameter.

Type Aliases

class ParsableManimColor
ManimColor | int | str | IntRGBLike | FloatRGBLike | IntRGBALike | FloatRGBALike

ParsableManimColor represents all the types which can be parsed to a ManimColor in Manim.

TypeVar’s

class ManimColorT
TypeVar('ManimColorT', bound=ManimColor)

Classes

HSV

HSV Color Space

ManimColor

Internal representation of a color.

RGBA

RGBA Color Space

RandomColorGenerator

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:

ManimColor

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 rgb color of some surface which is located at a given point in space and facing in the direction of unit_normal_vect, depending on whether the surface is facing a light_source or away from it.

Parameters:
  • rgb (FloatRGB) – An RGB array of floats.

  • point (Point3D) – The location of the colored surface.

  • unit_normal_vect (Vector3D) – The direction in which the colored surface is facing.

  • light_source (Point3D) – The location of a light source which might illuminate the surface.

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:
Returns:

The interpolated ManimColor.

Return type:

ManimColor

invert_color(color)[source]

Helper function for use in functional style programming. Refer to ManimColor.invert()

Parameters:

color (ManimColorT) – The ManimColor to invert.

Returns:

The linearly inverted ManimColor.

Return type:

ManimColor

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:

ManimColor

random_color()[source]

Return a random ManimColor.

Returns:

A random ManimColor.

Return type:

ManimColor

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:

ManimColor

rgb_to_hex(rgb)[source]

Helper function for use in functional style programming. Refer to ManimColor.from_rgb() and ManimColor.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:

ManimColor