Skip to content

Experimental colors #4233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: experimental
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 0 additions & 89 deletions manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
from manim.utils.color import (
BLACK,
WHITE,
YELLOW_C,
ManimColor,
ParsableManimColor,
color_gradient,
interpolate_color,
)
from manim.utils.exceptions import MultiAnimationOverrideException
Expand Down Expand Up @@ -96,7 +93,6 @@ def __init_subclass__(cls, **kwargs) -> None:

def __init__(
self,
color: ParsableManimColor | list[ParsableManimColor] = WHITE,
name: str | None = None,
dim: int = 3,
target=None,
Expand All @@ -110,11 +106,9 @@ def __init__(
self.submobjects = []
self.updaters: list[Updater] = []
self.updating_suspended = False
self.color = ManimColor.parse(color)

self.reset_points()
self.generate_points()
self.init_colors()

def _assert_valid_submobjects(self, submobjects: Iterable[Mobject]) -> Self:
"""Check that all submobjects are actually instances of
Expand Down Expand Up @@ -408,13 +402,6 @@ def reset_points(self) -> None:
"""Sets :attr:`points` to be an empty array."""
self.points = np.zeros((0, self.dim))

def init_colors(self) -> object:
"""Initializes the colors.

Gets called upon creation. This is an empty method that can be implemented by
subclasses.
"""

def generate_points(self) -> object:
"""Initializes :attr:`points` and therefore the shape.

Expand Down Expand Up @@ -1843,34 +1830,6 @@ def add_background_rectangle_to_family_members_with_points(self, **kwargs) -> Se

# Color functions

def set_color(
self, color: ParsableManimColor = YELLOW_C, family: bool = True
) -> Self:
"""Condition is function which takes in one arguments, (x, y, z).
Here it just recurses to submobjects, but in subclasses this
should be further implemented based on the the inner workings
of color
"""
if family:
for submob in self.submobjects:
submob.set_color(color, family=family)

self.color = ManimColor.parse(color)
return self

def set_color_by_gradient(self, *colors: ParsableManimColor) -> Self:
"""
Parameters
----------
colors
The colors to use for the gradient. Use like `set_color_by_gradient(RED, BLUE, GREEN)`.

self.color = ManimColor.parse(color)
return self
"""
self.set_submobject_colors_by_gradient(*colors)
return self

def set_colors_by_radial_gradient(
self,
center: Point3D | None = None,
Expand All @@ -1886,19 +1845,6 @@ def set_colors_by_radial_gradient(
)
return self

def set_submobject_colors_by_gradient(self, *colors: Iterable[ParsableManimColor]):
if len(colors) == 0:
raise ValueError("Need at least one color")
elif len(colors) == 1:
return self.set_color(*colors)

mobs = self.family_members_with_points()
new_colors = color_gradient(colors, len(mobs))

for mob, color in zip(mobs, new_colors):
mob.set_color(color, family=False)
return self

def set_submobject_colors_by_radial_gradient(
self,
center: Point3D | None = None,
Expand All @@ -1917,41 +1863,6 @@ def set_submobject_colors_by_radial_gradient(

return self

def to_original_color(self) -> Self:
self.set_color(self.color)
return self

def fade_to(
self, color: ParsableManimColor, alpha: float, family: bool = True
) -> Self:
if self.get_num_points() > 0:
new_color = interpolate_color(self.get_color(), color, alpha)
self.set_color(new_color, family=False)
if family:
for submob in self.submobjects:
submob.fade_to(color, alpha)
return self

def fade(self, darkness: float = 0.5, family: bool = True) -> Self:
if family:
for submob in self.submobjects:
submob.fade(darkness, family)
return self

def get_color(self) -> ManimColor:
"""Returns the color of the :class:`~.Mobject`

Examples
--------
::

>>> from manim import Square, RED
>>> Square(color=RED).get_color() == RED
True

"""
return self.color

##

def save_state(self) -> Self:
Expand Down
72 changes: 0 additions & 72 deletions manim/mobject/opengl/opengl_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ class MobjectStatus:

# TODO: add this to the **kwargs of all mobjects that use OpenGLMobject
class MobjectKwargs(TypedDict, total=False):
color: ParsableManimColor | Sequence[ParsableManimColor] | None
opacity: float
reflectiveness: float
shadow: float
gloss: float
is_fixed_in_frame: bool
is_fixed_orientation: bool
depth_test: bool
Expand Down Expand Up @@ -149,22 +144,12 @@ class OpenGLMobject:
# TypedDict above so that autocomplete works for users
def __init__(
self,
color: ParsableManimColor | Sequence[ParsableManimColor] | None = WHITE,
opacity: float = 1.0,
reflectiveness: float = 0.0,
shadow: float = 0.0,
gloss: float = 0.0,
is_fixed_in_frame: bool = False,
is_fixed_orientation: bool = False,
depth_test: bool = True,
name: str | None = None,
**kwargs: Any, # just dump
):
self.color = color
self.opacity = opacity
self.reflectiveness = reflectiveness
self.shadow = shadow
self.gloss = gloss
self.is_fixed_in_frame = is_fixed_in_frame
self.is_fixed_orientation = is_fixed_orientation
self.fixed_orientation_center = (0.0, 0.0, 0.0)
Expand All @@ -191,8 +176,6 @@ def __init__(
self.init_updaters()
self.init_event_listeners()
self.init_points()
self.color = ManimColor.parse(color)
self.init_colors()

@classmethod
def __init_subclass__(cls, **kwargs):
Expand Down Expand Up @@ -273,13 +256,6 @@ def construct(self):
else:
cls.__init__ = cls._original__init__

def init_colors(self):
"""Initializes the colors.

Gets called upon creation
"""
self.set_color(self.color, self.opacity)

def init_points(self) -> object:
"""Initializes :attr:`points` and therefore the shape.

Expand Down Expand Up @@ -2223,54 +2199,6 @@ def put_start_and_end_on(self, start: np.ndarray, end: np.ndarray):
self.shift(start - self.get_start())
return self

# Color functions

def set_color(self, color: ParsableManimColor | None, opacity=None, recurse=True):
# Recurse to submobjects differently from how set_rgba_array
# in case they implement set_color differently
if color is not None:
self.color: ManimColor = ManimColor.parse(color)
if opacity is not None:
self.color.opacity(opacity)
if recurse:
for submob in self.submobjects:
submob.set_color(color, recurse=True)
return self

def set_opacity(self, opacity, recurse=True):
# self.set_rgba_array(color=None, opacity=opacity, recurse=False)
if recurse:
for submob in self.submobjects:
submob.set_opacity(opacity, recurse=True)
return self

def get_color(self) -> ManimColor:
return self.color

def get_opacity(self):
return self.color.opacity()

def set_color_by_gradient(self, *colors: ParsableManimColor):
self.set_submobject_colors_by_gradient(*colors)
return self

def set_submobject_colors_by_gradient(self, *colors):
if len(colors) == 0:
raise Exception("Need at least one color")
elif len(colors) == 1:
return self.set_color(*colors)

# mobs = self.family_members_with_points()
mobs = self.submobjects
new_colors = color_gradient(colors, len(mobs))

for mob, color in zip(mobs, new_colors):
mob.set_color(color)
return self

def fade(self, darkness=0.5, recurse=True):
self.set_opacity(1.0 - darkness, recurse=recurse)

# Background rectangle

def add_background_rectangle(
Expand Down
Loading