ManimColor#

Qualified name: manim.utils.color.core.ManimColor

class ManimColor(value, alpha=1.0)[source]#

Bases: object

Internal representation of a color.

The ManimColor class is the main class for the representation of a color. It’s internal representation is a 4 element array of floats corresponding to a [r,g,b,a] value where r,g,b,a can be between 0 to 1.

This is done in order to reduce the amount of color inconsitencies by constantly casting between integers and floats which introduces errors.

The class can accept any value of type ParsableManimColor i.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_Float

ManimColor itself only accepts singular values and will directly interpret them into a single color if possible Be careful when passing strings to ManimColor it can create a big overhead for the color processing.

If you want to parse a list of colors use the function parse() in ManimColor which assumes that you are going to pass a list of color so arrays will not bei interpreted as a single color.

Warning

If you pass an array of numbers to parse() it will interpret the r,g,b,a numbers in that array as colors so instead of the expect singular color you get and array with 4 colors.

For conversion behaviors see the _internal functions for further documentation

Parameters:
  • value (ParsableManimColor | None) – Some representation of a color (e.g., a string or a suitable tuple).

  • alpha (float) – The opacity of the color. By default, colors are fully opaque (value 1.0).

Methods

from_hex

Creates a Manim Color from a hex string, prefixes allowed # and 0x

from_hsv

Creates a ManimColor from an HSV Array

from_rgb

Creates a ManimColor from an RGB Array.

from_rgba

Creates a ManimColor from an RGBA Array.

gradient

This is not implemented by now refer to color_gradient() for a working implementation for now

interpolate

Interpolates between the current and the given ManimColor an returns the interpolated color

invert

Returns an linearly inverted version of the color (no inplace changes)

parse

Handles the parsing of a list of colors or a single color.

to_hex

Converts the manim color to a hexadecimal representation of the color

to_hsv

Converts the Manim Color to HSV array.

to_int_rgb

Converts the current ManimColor into a rgb array of int

to_int_rgba

Converts the current ManimColor into a rgba array of int

to_int_rgba_with_alpha

Converts the current ManimColor into a rgba array of integers as to_int_rgba() but you can change the alpha value.

to_integer

Converts the current ManimColor into an integer

to_rgb

Converts the current ManimColor into a rgb array of floats

to_rgba

Converts the current ManimColor into a rgba array of floats

to_rgba_with_alpha

Converts the current ManimColor into a rgba array of float as to_rgba() but you can change the alpha value.

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 (str) – hex string to be parsed

  • alpha (float) – alpha value used for the color

Returns:

Internal color representation

Return type:

ManimColorInternal

static _internal_from_int_rgb(rgb, alpha=1.0)[source]#

Internal function for converting a rgb tuple of integers into the internal representation of a ManimColor.

For internal use only

Parameters:
  • rgb (RGB_Tuple_Int) – integer rgb tuple to be parsed

  • alpha (float, optional) – optional alpha value, by default 1.0

Returns:

Internal color representation

Return type:

ManimColorInternal

static _internal_from_int_rgba(rgba)[source]#

Internal function for converting a rgba tuple of integers into the internal representation of a ManimColor.

For internal use only

Parameters:

rgba (RGBA_Tuple_Int) – int rgba tuple to be parsed

Returns:

Internal color representation

Return type:

ManimColorInternal

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 (RGB_Tuple_Float) – float rgb tuple to be parsed

  • alpha (float, optional) – optional alpha value, by default 1.0

Returns:

Internal color representation

Return type:

ManimColorInternal

static _internal_from_rgba(rgba)[source]#

Internal function for converting a rgba tuple of floats into the internal representation of a ManimColor.

For internal use only

Parameters:

rgba (RGBA_Tuple_Float) – int rgba tuple to be parsed

Returns:

Internal color representation

Return type:

ManimColorInternal

static _internal_from_string(name)[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.

Returns:

Internal color representation

Return type:

ManimColorInternal

Raises:

ValueError – Raises a ValueError if the color name is not present with manim

property _internal_value: ndarray[Any, dtype[float64]]#

Returns the internal value of the current Manim color [r,g,b,a] float array

Returns:

internal color representation

Return type:

ManimColorInternal

classmethod from_hex(hex, alpha=1.0)[source]#

Creates a Manim Color from a hex string, prefixes allowed # and 0x

Parameters:
  • hex (str) – The hex string to be converted (currently only supports 6 nibbles)

  • alpha (float, optional) – alpha value to be used for the hex string, by default 1.0

Returns:

The ManimColor represented by the hex string

Return type:

ManimColor

classmethod from_hsv(hsv, alpha=1.0)[source]#

Creates a ManimColor from an HSV Array

Parameters:
  • hsv (HSV_Array_Float | HSV_Tuple_Float) – Any 3 Element Iterable containing floats from 0-1

  • alpha (float, optional) – the alpha value to be used, by default 1.0

Returns:

The ManimColor with the corresponding RGB values to the HSV

Return type:

ManimColor

classmethod from_rgb(rgb, alpha=1.0)[source]#

Creates a ManimColor from an RGB Array. Automagically decides which type it is int/float

Warning

Please make sure that your elements are not floats if you want integers. A 5.0 will result in the input being interpreted as if it was a float rgb array with the value 5.0 and not the integer 5

Parameters:
  • rgb (RGB_Array_Float | RGB_Tuple_Float | RGB_Array_Int | RGB_Tuple_Int) – Any 3 Element Iterable

  • alpha (float, optional) – alpha value to be used in the color, by default 1.0

Returns:

Returns the ManimColor object

Return type:

ManimColor

classmethod from_rgba(rgba)[source]#

Creates a ManimColor from an RGBA Array. Automagically decides which type it is int/float

Warning

Please make sure that your elements are not floats if you want integers. A 5.0 will result in the input being interpreted as if it was a float rgb array with the value 5.0 and not the integer 5

Parameters:

rgba (RGBA_Array_Float | RGBA_Tuple_Float | RGBA_Array_Int | RGBA_Tuple_Int) – Any 4 Element Iterable

Returns:

Returns the ManimColor object

Return type:

ManimColor

static gradient(colors, length)[source]#

This is not implemented by now refer to color_gradient() for a working implementation for now

Parameters:
interpolate(other, alpha)[source]#

Interpolates between the current and the given ManimColor an returns the interpolated color

Parameters:
  • other (ManimColor) – The other ManimColor to be used for interpolation

  • alpha (float) –

    A point on the line in rgba colorspace connecting the two colors i.e. the interpolation point

    0 corresponds to the current ManimColor and 1 corresponds to the other ManimColor

Returns:

The interpolated ManimColor

Return type:

ManimColor

invert(with_alpha=False)[source]#

Returns an linearly inverted version of the color (no inplace changes)

Parameters:

with_alpha (bool, optional) –

if true the alpha value will be inverted too, by default False

Note

This can result in unintended behavior where objects are not displayed because their alpha value is suddenly 0 or very low. Please keep that in mind when setting this to true

Returns:

The linearly inverted ManimColor

Return type:

ManimColor

classmethod parse(color: Optional[Union[ManimColor, int, str, Tuple[int, int, int], Tuple[float, float, float], Tuple[int, int, int, int], Tuple[float, float, float, float], ndarray[Any, dtype[int64]], ndarray[Any, dtype[float64]]]], alpha: float = 1.0) Self[source]#
classmethod parse(color: Sequence[Union[ManimColor, int, str, Tuple[int, int, int], Tuple[float, float, float], Tuple[int, int, int, int], Tuple[float, float, float, float], ndarray[Any, dtype[int64]], ndarray[Any, dtype[float64]]]], alpha: float = 1.0) list[Self]

Handles the parsing of a list of colors or a single color.

Parameters:
  • color – The color or list of colors to parse. Note that this function can not accept rgba tuples. It will assume that you mean list[ManimColor] and will return a list of ManimColors.

  • alpha – The alpha value to use if a single color is passed. or if a list of colors is passed to set the value of all colors.

Returns:

Either a list of colors or a singular color depending on the input

Return type:

ManimColor

to_hex(with_alpha=False)[source]#

Converts the manim color to a hexadecimal representation of the color

Parameters:

with_alpha (bool, optional) – Changes the result from 6 to 8 values where the last 2 nibbles represent the alpha value of 0-255, by default False

Returns:

A hex string starting with a # with either 6 or 8 nibbles depending on your input, by default 6 i.e #XXXXXX

Return type:

str

to_hsv()[source]#

Converts the Manim Color to 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 so you might want to annotate the usage of this function in your code by typing the variables with HSV_Array_Float in order to differentiate between rgb arrays and hsv arrays

Returns:

A hsv array containing 3 elements of type float ranging from 0 to 1

Return type:

HSV_Array_Float

to_int_rgb()[source]#

Converts the current ManimColor into a rgb array of int

Returns:

rgb array with 3 elements of type int

Return type:

RGB_Array_Int

to_int_rgba()[source]#

Converts the current ManimColor into a rgba array of int

Returns:

rgba array with 4 elements of type int

Return type:

RGBA_Array_Int

to_int_rgba_with_alpha(alpha)[source]#

Converts the current ManimColor into a rgba array of integers as to_int_rgba() but you can change the alpha value.

Parameters:

alpha (float) – alpha value to be used for the return value. (Will automatically be scaled from 0-1 to 0-255 so just pass 0-1)

Returns:

rgba array with 4 elements of type int

Return type:

RGBA_Array_Int

to_integer()[source]#

Converts the current ManimColor into an integer

Returns:

  • int – integer representation of the color

  • .. warning:: – This will return only the rgb part of the color

Return type:

int

to_rgb()[source]#

Converts the current ManimColor into a rgb array of floats

Returns:

rgb array with 3 elements of type float

Return type:

RGB_Array_Float

to_rgba()[source]#

Converts the current ManimColor into a rgba array of floats

Returns:

rgba array with 4 elements of type float

Return type:

RGBA_Array_Float

to_rgba_with_alpha(alpha)[source]#

Converts the current ManimColor into a rgba array of float as to_rgba() but you can change the alpha value.

Parameters:

alpha (float) – alpha value to be used in the return value

Returns:

rgba array with 4 elements of type float

Return type:

RGBA_Array_Float