-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(vectorized_mobject,mobject): Removing explicit opacity attributes… #3862
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
7e25fca
2861b42
8ac1d38
879e7b6
c2425e0
e151865
b91dbd3
7a0575b
bf3339f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing_extensions import deprecated | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use |
||
|
|
||
| __all__ = [ | ||
| "VMobject", | ||
| "VGroup", | ||
|
|
@@ -15,7 +17,7 @@ | |
| import itertools as it | ||
| import sys | ||
| from collections.abc import Generator, Hashable, Iterable, Mapping, Sequence | ||
| from typing import TYPE_CHECKING, Callable, Literal | ||
| from typing import TYPE_CHECKING, Callable, Literal, cast | ||
|
|
||
| import numpy as np | ||
| from PIL.Image import Image | ||
|
|
@@ -112,13 +114,14 @@ class VMobject(Mobject): | |
|
|
||
| def __init__( | ||
| self, | ||
| color: ParsableManimColor | list[ParsableManimColor] | None = None, | ||
| fill_color: ParsableManimColor | None = None, | ||
| fill_opacity: float = 0.0, | ||
| fill_opacity: float | None = None, | ||
| stroke_color: ParsableManimColor | None = None, | ||
| stroke_opacity: float = 1.0, | ||
| stroke_opacity: float | None = None, | ||
| stroke_width: float = DEFAULT_STROKE_WIDTH, | ||
| background_stroke_color: ParsableManimColor | None = BLACK, | ||
| background_stroke_opacity: float = 1.0, | ||
| background_stroke_opacity: float | None = None, | ||
| background_stroke_width: float = 0, | ||
| sheen_factor: float = 0.0, | ||
| joint_type: LineJointType | None = None, | ||
|
|
@@ -134,14 +137,7 @@ def __init__( | |
| cap_style: CapStyleType = CapStyleType.AUTO, | ||
| **kwargs, | ||
| ): | ||
| self.fill_opacity = fill_opacity | ||
| self.stroke_opacity = stroke_opacity | ||
| self.stroke_width = stroke_width | ||
| if background_stroke_color is not None: | ||
| self.background_stroke_color: ManimColor = ManimColor( | ||
| background_stroke_color | ||
| ) | ||
| self.background_stroke_opacity: float = background_stroke_opacity | ||
| self.background_stroke_width: float = background_stroke_width | ||
| self.sheen_factor: float = sheen_factor | ||
| self.joint_type: LineJointType = ( | ||
|
|
@@ -163,17 +159,33 @@ def __init__( | |
| 0, 1, n_points_per_cubic_curve | ||
| ) | ||
| self.cap_style: CapStyleType = cap_style | ||
| super().__init__(**kwargs) | ||
|
|
||
| # TODO: Refactor color initialization | ||
| # This must be after init | ||
|
|
||
| self.submobjects: list[VMobject] | ||
| if fill_color is not None or stroke_color is not None: | ||
| color = None | ||
|
|
||
| if background_stroke_color is not None: | ||
| self.background_stroke_color: ManimColor = ManimColor( | ||
| background_stroke_color | ||
| ) | ||
| if background_stroke_opacity is not None: | ||
| self.background_stroke_color = self.background_stroke_color.opacity( | ||
| background_stroke_opacity | ||
| ) | ||
|
|
||
| super().__init__(color=color, **kwargs) | ||
|
|
||
| # TODO: Find where color overwrites are happening and remove the color doubling | ||
| # if "color" in kwargs: | ||
| # fill_color = kwargs["color"] | ||
| # stroke_color = kwargs["color"] | ||
| if fill_color is not None: | ||
| self.fill_color = ManimColor.parse(fill_color) | ||
| # if fill_opacity is not None: | ||
| # self.fill_color = self.fill_color.opacity(fill_opacity) | ||
|
|
||
| if stroke_color is not None: | ||
| self.stroke_color = ManimColor.parse(stroke_color) | ||
| # if stroke_opacity is not None: | ||
| # self.stroke_color = self.stroke_color.opacity(stroke_opacity) | ||
|
|
||
|
|
||
| def _assert_valid_submobjects(self, submobjects: Iterable[VMobject]) -> Self: | ||
| return self._assert_valid_submobjects_internal(submobjects, VMobject) | ||
|
|
@@ -191,22 +203,11 @@ def get_mobject_type_class() -> type[VMobject]: | |
| return VMobject | ||
|
|
||
| # Colors | ||
| @deprecated("Sorry") | ||
| def init_colors(self, propagate_colors: bool = True) -> Self: | ||
| self.set_fill( | ||
| color=self.fill_color, | ||
| opacity=self.fill_opacity, | ||
| family=propagate_colors, | ||
| ) | ||
| self.set_stroke( | ||
| color=self.stroke_color, | ||
| width=self.stroke_width, | ||
| opacity=self.stroke_opacity, | ||
| family=propagate_colors, | ||
| ) | ||
| self.set_background_stroke( | ||
| color=self.background_stroke_color, | ||
| width=self.background_stroke_width, | ||
| opacity=self.background_stroke_opacity, | ||
| family=propagate_colors, | ||
| ) | ||
| self.set_sheen( | ||
|
|
@@ -222,7 +223,7 @@ def init_colors(self, propagate_colors: bool = True) -> Self: | |
| return self | ||
|
|
||
| def generate_rgbas_array( | ||
| self, color: ManimColor | list[ManimColor], opacity: float | Iterable[float] | ||
| self, color: ManimColor | list[ManimColor] | ||
| ) -> RGBA_Array_Float: | ||
| """ | ||
| First arg can be either a color, or a tuple/list of colors. | ||
|
|
@@ -232,14 +233,10 @@ def generate_rgbas_array( | |
| will automatically be added for the gradient | ||
| """ | ||
| colors: list[ManimColor] = [ | ||
| ManimColor(c) if (c is not None) else BLACK for c in tuplify(color) | ||
| ] | ||
| opacities: list[float] = [ | ||
| o if (o is not None) else 0.0 for o in tuplify(opacity) | ||
| ManimColor(c) if (c is not None) else BLACK | ||
| for c in cast(tuple[ManimColor], tuplify(color)) | ||
| ] | ||
| rgbas: npt.NDArray[RGBA_Array_Float] = np.array( | ||
| [c.to_rgba_with_alpha(o) for c, o in zip(*make_even(colors, opacities))], | ||
| ) | ||
| rgbas = np.array([c.to_rgba() for c in colors]) | ||
|
|
||
| sheen_factor = self.get_sheen_factor() | ||
| if sheen_factor != 0 and len(rgbas) == 1: | ||
|
|
@@ -252,10 +249,11 @@ def generate_rgbas_array( | |
| def update_rgbas_array( | ||
| self, | ||
| array_name: str, | ||
| color: ManimColor | None = None, | ||
| opacity: float | None = None, | ||
| color: ManimColor | None, | ||
| ) -> Self: | ||
| rgbas = self.generate_rgbas_array(color, opacity) | ||
| if color is None: | ||
| return self | ||
| rgbas = self.generate_rgbas_array(color) | ||
| if not hasattr(self, array_name): | ||
| setattr(self, array_name, rgbas) | ||
| return self | ||
|
|
@@ -269,10 +267,7 @@ def update_rgbas_array( | |
| rgbas = stretch_array_to_length(rgbas, len(curr_rgbas)) | ||
| # Only update rgb if color was not None, and only | ||
| # update alpha channel if opacity was passed in | ||
| if color is not None: | ||
| curr_rgbas[:, :3] = rgbas[:, :3] | ||
| if opacity is not None: | ||
| curr_rgbas[:, 3] = rgbas[:, 3] | ||
| curr_rgbas[:, :4] = rgbas[:, :4] | ||
| return self | ||
|
|
||
| def set_fill( | ||
|
|
@@ -316,23 +311,30 @@ def construct(self): | |
| -------- | ||
| :meth:`~.VMobject.set_style` | ||
| """ | ||
| if color is not None: | ||
| color = ManimColor.parse(color) | ||
| if opacity is not None: | ||
| color = color.opacity(opacity) | ||
|
|
||
| if family: | ||
| for submobject in self.submobjects: | ||
| submobject.set_fill(color, opacity, family) | ||
| self.update_rgbas_array("fill_rgbas", color, opacity) | ||
| self.update_rgbas_array("fill_rgbas", color) | ||
| self.fill_rgbas: RGBA_Array_Float | ||
| if opacity is not None: | ||
| self.fill_opacity = opacity | ||
| return self | ||
|
|
||
| def set_stroke( | ||
| self, | ||
| color: ParsableManimColor = None, | ||
| color: ParsableManimColor | None = None, | ||
| width: float | None = None, | ||
| opacity: float | None = None, | ||
| background=False, | ||
| family: bool = True, | ||
| ) -> Self: | ||
| if color is not None: | ||
| color = ManimColor.parse(color) | ||
| if opacity is not None: | ||
| color = color.opacity(opacity) | ||
| if family: | ||
| for submobject in self.submobjects: | ||
| submobject.set_stroke(color, width, opacity, background, family) | ||
|
|
@@ -344,7 +346,7 @@ def set_stroke( | |
| array_name = "stroke_rgbas" | ||
| width_name = "stroke_width" | ||
| opacity_name = "stroke_opacity" | ||
| self.update_rgbas_array(array_name, color, opacity) | ||
| self.update_rgbas_array(array_name, color) | ||
| if width is not None: | ||
| setattr(self, width_name, width) | ||
| if opacity is not None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -519,6 +519,39 @@ def invert(self, with_alpha=False) -> ManimColor: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ManimColor(1.0 - self._internal_value, with_alpha) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def opacity(self, opacity: float) -> ManimColor: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Returns a new ManimColor with the same color and the given opacity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| opacity : float | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The opacity for the new ManimColor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ManimColor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The new ManimColor object with changed opacity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def opacity(self, opacity: None) -> float: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Returns the opacity of the current ManimColor in a range from zero to one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| float | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The opacity of the ManimColor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def opacity(self, opacity=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Returns a new ManimColor with the same color and a new opacity or changes the opacity""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if opacity is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._internal_value[3] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tmp = self._internal_value.copy() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tmp[3] = opacity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ManimColor.parse(tmp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+634
to
+665
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this overload. Could it be, instead, something like
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def interpolate(self, other: ManimColor, alpha: float) -> ManimColor: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Interpolates between the current and the given ManimColor an returns the interpolated color | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -676,7 +709,7 @@ def gradient(colors: list[ManimColor], length: int): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __repr__(self) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return f"{self.__class__.__name__}('{self.to_hex()}')" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return f"{self.__class__.__name__}('{self.to_hex(True)}')" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __str__(self) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return f"{self.to_hex()}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.