From 1def4e685f7f746826a5c5cf946e3dda30251000 Mon Sep 17 00:00:00 2001 From: christopher-hampson Date: Sun, 11 Aug 2024 09:57:20 +0100 Subject: [PATCH 1/6] feat: preserve original Tex colors --- manim/mobject/text/tex_mobject.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index c6810d8f65..6f12885725 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -12,7 +12,7 @@ from __future__ import annotations -from manim.utils.color import ManimColor +from manim.utils.color import BLACK, WHITE, ManimColor, ParsableManimColor __all__ = [ "SingleStringMathTex", @@ -34,7 +34,7 @@ from manim.constants import * from manim.mobject.geometry.line import Line from manim.mobject.svg.svg_mobject import SVGMobject -from manim.mobject.types.vectorized_mobject import VGroup, VMobject +from manim.mobject.types.vectorized_mobject import VGroup from manim.utils.tex import TexTemplate from manim.utils.tex_file_writing import tex_to_svg_file @@ -62,13 +62,9 @@ def __init__( tex_environment: str = "align*", tex_template: TexTemplate | None = None, font_size: float = DEFAULT_FONT_SIZE, + color: ParsableManimColor | None = WHITE, **kwargs, ): - if kwargs.get("color") is None: - # makes it so that color isn't explicitly passed for these mobs, - # and can instead inherit from the parent - kwargs["color"] = VMobject().color - self._font_size = font_size self.organize_left_to_right = organize_left_to_right self.tex_environment = tex_environment @@ -88,6 +84,7 @@ def __init__( should_center=should_center, stroke_width=stroke_width, height=height, + color=color, path_string_config={ "should_subdivide_sharp_curves": True, "should_remove_null_curves": True, @@ -211,10 +208,13 @@ def get_tex_string(self): return self.tex_string def init_colors(self, propagate_colors=True): - if config.renderer == RendererType.OPENGL: - super().init_colors() - elif config.renderer == RendererType.CAIRO: - super().init_colors(propagate_colors=propagate_colors) + for submobject in self.submobjects: + if submobject.color == BLACK: + submobject.color = self.color + if config.renderer == RendererType.OPENGL: + submobject.init_colors() + elif config.renderer == RendererType.CAIRO: + submobject.init_colors(propagate_colors=propagate_colors) class MathTex(SingleStringMathTex): From c4378a8ad0bd7b3e94646153867bee268527e58e Mon Sep 17 00:00:00 2001 From: christopher-hampson Date: Sun, 11 Aug 2024 12:19:10 +0100 Subject: [PATCH 2/6] added test for preserving tex color --- .../test_graphical_units/test_tex_mobject.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_graphical_units/test_tex_mobject.py b/tests/test_graphical_units/test_tex_mobject.py index b229e1cb0f..f6da35cf91 100644 --- a/tests/test_graphical_units/test_tex_mobject.py +++ b/tests/test_graphical_units/test_tex_mobject.py @@ -24,3 +24,33 @@ def test_set_opacity_by_tex(scene): tex = MathTex("f(x) = y", substrings_to_isolate=["f(x)"]) tex.set_opacity_by_tex("f(x)", 0.2, 0.5) scene.add(tex) + + +def test_preserve_tex_color(): + """Test that Tex preserves original tex colors.""" + template = TexTemplate(preamble=r"\usepackage{xcolor}") + Tex.set_default(tex_template=template) + + txt = Tex(r"\textcolor{red}{Hello} World") + assert len(txt[0].submobjects) == 10 + assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello" + assert all( + char.fill_color.to_hex() == WHITE.to_hex() for char in txt[0][-5:] + ) # "World" + + txt = Tex(r"\textcolor{red}{Hello} World", color=BLUE) + assert len(txt[0].submobjects) == 10 + assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello" + assert all( + char.fill_color.to_hex() == BLUE.to_hex() for char in txt[0][-5:] + ) # "World" + + Tex.set_default(color=GREEN) + txt = Tex(r"\textcolor{red}{Hello} World") + assert len(txt[0].submobjects) == 10 + assert all(char.fill_color.to_hex() == "#FF0000" for char in txt[0][:5]) # "Hello" + assert all( + char.fill_color.to_hex() == GREEN.to_hex() for char in txt[0][-5:] + ) # "World" + + Tex.set_default() From 37771cb3e8b28b9711e717b0d9d01b6a760fc450 Mon Sep 17 00:00:00 2001 From: christopher-hampson Date: Mon, 12 Aug 2024 16:03:49 +0100 Subject: [PATCH 3/6] fix: fixed breaking change to tex color inheritance --- manim/mobject/text/tex_mobject.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 6f12885725..5164656b7a 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -12,7 +12,7 @@ from __future__ import annotations -from manim.utils.color import BLACK, WHITE, ManimColor, ParsableManimColor +from manim.utils.color import BLACK, ManimColor, ParsableManimColor __all__ = [ "SingleStringMathTex", @@ -34,7 +34,7 @@ from manim.constants import * from manim.mobject.geometry.line import Line from manim.mobject.svg.svg_mobject import SVGMobject -from manim.mobject.types.vectorized_mobject import VGroup +from manim.mobject.types.vectorized_mobject import VGroup, VMobject from manim.utils.tex import TexTemplate from manim.utils.tex_file_writing import tex_to_svg_file @@ -62,9 +62,12 @@ def __init__( tex_environment: str = "align*", tex_template: TexTemplate | None = None, font_size: float = DEFAULT_FONT_SIZE, - color: ParsableManimColor | None = WHITE, + color: ParsableManimColor | None = None, **kwargs, ): + if color is None: + color = VMobject().color + self._font_size = font_size self.organize_left_to_right = organize_left_to_right self.tex_environment = tex_environment From e1c5646172e66144619a9fa4231b0ef2e1e918ba Mon Sep 17 00:00:00 2001 From: christopher-hampson Date: Sun, 18 Aug 2024 12:20:04 +0100 Subject: [PATCH 4/6] updated docstring and comment --- manim/mobject/text/tex_mobject.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 5164656b7a..24aa85e1ec 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -212,12 +212,15 @@ def get_tex_string(self): def init_colors(self, propagate_colors=True): for submobject in self.submobjects: - if submobject.color == BLACK: - submobject.color = self.color - if config.renderer == RendererType.OPENGL: - submobject.init_colors() - elif config.renderer == RendererType.CAIRO: - submobject.init_colors(propagate_colors=propagate_colors) + # needed to preserve original (non-black) + # TeX colors of individual submobjects + if submobject.color != BLACK: + continue + submobject.color = self.color + if config.renderer == RendererType.OPENGL: + submobject.init_colors() + elif config.renderer == RendererType.CAIRO: + submobject.init_colors(propagate_colors=propagate_colors) class MathTex(SingleStringMathTex): @@ -428,7 +431,9 @@ def sort_alphabetically(self): class Tex(MathTex): - r"""A string compiled with LaTeX in normal mode. + r"""A string compiled with LaTeX in normal mode. The color can be set using + the `color` argument. Any parts of the `tex_string` that are colored by the + commands`\color` or `\textcolor` will retain their original color. Tests ----- From 98cee17b39e903c489e8f47a5796449d57a24d7e Mon Sep 17 00:00:00 2001 From: Christopher Hampson Date: Sun, 1 Sep 2024 16:10:49 +0100 Subject: [PATCH 5/6] Update manim/mobject/text/tex_mobject.py Co-authored-by: Aarush Deshpande <110117391+JasonGrace2282@users.noreply.github.com> --- manim/mobject/text/tex_mobject.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 24aa85e1ec..3abfade2f7 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -431,9 +431,11 @@ def sort_alphabetically(self): class Tex(MathTex): - r"""A string compiled with LaTeX in normal mode. The color can be set using - the `color` argument. Any parts of the `tex_string` that are colored by the - commands`\color` or `\textcolor` will retain their original color. + r"""A string compiled with LaTeX in normal mode. + + The color can be set using + the ``color`` argument. Any parts of the ``tex_string`` that are colored by the + TeX commands ``\color`` or ``\textcolor`` will retain their original color. Tests ----- From c99477b6c8f0642db1e436e41b7f2d4a3fefaf70 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Sep 2024 15:11:02 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/text/tex_mobject.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 3abfade2f7..d9540e30c2 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -432,7 +432,7 @@ def sort_alphabetically(self): class Tex(MathTex): r"""A string compiled with LaTeX in normal mode. - + The color can be set using the ``color`` argument. Any parts of the ``tex_string`` that are colored by the TeX commands ``\color`` or ``\textcolor`` will retain their original color.