Skip to content

Commit 117563e

Browse files
Added :meth:.set_opacity_by_tex method for setting the opacity of parts of Tex mobjects (#3159)
* Added MathTex.set_opacity_by_tex and MathTex.fade_all_but_tex * Completed unittests for set_opacity_by_tex and fade_all_but_tex * Added control_data for `fade_all_but_tex` and `set_opacity_by_tex` * Removed ade_all_but_tex and integrated functionality into set_opacity_by_tex. Included a second parameter 'remaining_opacity' to define the opacity value of the remaining tex * Added type hints for `set_opacity_by_tex`. Removed type information in docstring. Improved logic of `set_opacity_by_tex` to reduce redundant calls for tex parts --------- Co-authored-by: Benjamin Hackl <[email protected]>
1 parent b9784a0 commit 117563e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

manim/mobject/text/tex_mobject.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,29 @@ def set_color_by_tex(self, tex, color, **kwargs):
381381
part.set_color(color)
382382
return self
383383

384+
def set_opacity_by_tex(
385+
self, tex: str, opacity: float = 0.5, remaining_opacity: float = None, **kwargs
386+
):
387+
"""
388+
Sets the opacity of the tex specified. If 'remaining_opacity' is specified,
389+
then the remaining tex will be set to that opacity.
390+
391+
Parameters
392+
----------
393+
tex
394+
The tex to set the opacity of.
395+
opacity
396+
Default 0.5. The opacity to set the tex to
397+
remaining_opacity
398+
Default None. The opacity to set the remaining tex to.
399+
If None, then the remaining tex will not be changed
400+
"""
401+
if remaining_opacity is not None:
402+
self.set_opacity(opacity=remaining_opacity)
403+
for part in self.get_parts_by_tex(tex):
404+
part.set_opacity(opacity)
405+
return self
406+
384407
def set_color_by_tex_to_color_map(self, texs_to_color_map, **kwargs):
385408
for texs, color in list(texs_to_color_map.items()):
386409
try:
Binary file not shown.

tests/test_graphical_units/test_tex_mobject.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ def test_color_inheritance(scene):
1818
VMobject.set_default()
1919

2020
scene.add(vgr)
21+
22+
23+
@frames_comparison
24+
def test_set_opacity_by_tex(scene):
25+
"""Test that set_opacity_by_tex works correctly."""
26+
tex = MathTex("f(x) = y", substrings_to_isolate=["f(x)"])
27+
tex.set_opacity_by_tex("f(x)", 0.2, 0.5)
28+
scene.add(tex)

0 commit comments

Comments
 (0)