A community-maintained Python framework for creating mathematical animations.
fix(code): inherit container background and adjust line number color (#4816)
Helbronnerac0a83ac45f4
diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.pyindex 4c917d0e2..0351011a1 100644--- a/manim/mobject/text/code_mobject.py+++ b/manim/mobject/text/code_mobject.py@@ -8,13 +8,15 @@ __all__ = [import refrom pathlib import Path-from typing import Any, Literal+from typing import Any, Literal, castfrom bs4 import BeautifulSoup, Tagfrom pygments import highlightfrom pygments.formatters.html import HtmlFormatterfrom pygments.lexers import get_lexer_by_name, guess_lexer, guess_lexer_for_filename-from pygments.styles import get_all_styles+from pygments.style import Style+from pygments.styles import get_all_styles, get_style_by_name+from pygments.token import Text as TextTokenfrom manim.constants import *from manim.mobject.geometry.arc import Dot@@ -22,7 +24,7 @@ from manim.mobject.geometry.shape_matchers import SurroundingRectanglefrom manim.mobject.opengl.opengl_compatibility import ConvertToOpenGLfrom manim.mobject.types.vectorized_mobject import VGroup, VMobjectfrom manim.typing import StrPath-from manim.utils.color import WHITE, ManimColor+from manim.utils.color import BLACK, WHITEclass Code(VMobject, metaclass=ConvertToOpenGL):@@ -79,9 +81,11 @@ class Code(VMobject, metaclass=ConvertToOpenGL):The programming language of the code. If not specified, it will beguessed from the file extension or the code itself.formatter_style- The style to use for the code highlighting. Defaults to ``"vim"``.- A list of all available styles can be obtained by calling- :meth:`.Code.get_styles_list`.+ The style to use for the code highlighting. This can be either the name+ of a Pygments style or a custom Pygments style class. Defaults to+ ``"vim"``. A list of all available styles can be obtained by calling+ :meth:`.Code.get_styles_list`; style classes can be retrieved with+ :meth:`.Code.get_pygments_style`.tab_widthThe width of a tab character in spaces. Defaults to 4.add_line_numbers@@ -95,19 +99,63 @@ class Code(VMobject, metaclass=ConvertToOpenGL):Keyword arguments passed to the background constructor. Defaultsettings are stored in the class attribute:attr:`.default_background_config` (which can also be modified- directly).+ directly). If ``fill_color`` is not specified, it is taken from+ the selected ``formatter_style``.paragraph_configKeyword arguments passed to the constructor of the:class:`.Paragraph` objects holding the code, and the linenumbers. Default settings are stored in the class attribute:attr:`.default_paragraph_config` (which can also be modified- directly).+ directly). The ``color`` setting is ignored because colors are+ determined by the selected Pygments style.++ Notes+ -----+ .. note::++ The Pygments style controls the colors of the rendered code, including+ its default foreground, background, and line number colors. To+ customize the color scheme, pass a custom Pygments style class via+ ``formatter_style`` rather than setting ``paragraph_config["color"]``.+ See `Creating own styles with Pygments+ <https://pygments.org/docs/styledevelopment/>`_ for details.++ For example, a built-in style can be subclassed without importing its+ style class directly:++ .. code-block:: python++ from manim import *+ from pygments.token import Comment++ BaseStyle = Code.get_pygments_style("vim")+++ class CustomStyle(BaseStyle):+ background_color = "#1e1e1e"+ line_number_color = "#858585"+ styles = {+ **BaseStyle.styles,+ Comment: "italic #6a9955",+ }+++ class Example(Scene):+ def construct(self):+ rendered_code = Code(+ code_string="print('Hello, world!') # greeting",+ language="python",+ formatter_style=CustomStyle,+ )++ self.add(rendered_code)+ self.wait(2)"""_styles_list_cache: list[str] | None = Nonedefault_background_config: dict[str, Any] = {"buff": 0.3,- "fill_color": ManimColor("#222"),+ "fill_color": None,"stroke_color": WHITE,"corner_radius": 0.2,"stroke_width": 1,@@ -126,7 +174,7 @@ class Code(VMobject, metaclass=ConvertToOpenGL):code_file: StrPath | None = None,code_string: str | None = None,language: str | None = None,- formatter_style: str = "vim",+ formatter_style: str | type[Style] = "vim",tab_width: int = 4,add_line_numbers: bool = True,line_numbers_from: int = 1,@@ -158,6 +206,7 @@ class Code(VMobject, metaclass=ConvertToOpenGL):noclasses=True,cssclasses="",)+ selected_style = formatter.stylesoup = BeautifulSoup(highlight(code_string, lexer, formatter), features="html.parser")@@ -166,15 +215,19 @@ class Code(VMobject, metaclass=ConvertToOpenGL):code_lines = self._code_html.get_text().removesuffix("\n").split("\n")- if paragraph_config is None:- paragraph_config = {}base_paragraph_config = self.default_paragraph_config.copy()- base_paragraph_config.update(paragraph_config)+ base_paragraph_config.update(paragraph_config or {})+ base_paragraph_config.pop("color", None)+ default_text_color = selected_style.style_for_token(TextToken).get("color")+ foreground_color = (+ BLACK if default_text_color is None else f"#{default_text_color}"+ )from manim.mobject.text.text_mobject import Paragraphself.code_lines = Paragraph(*code_lines,+ color=foreground_color,**base_paragraph_config,)@@ -201,7 +254,11 @@ class Code(VMobject, metaclass=ConvertToOpenGL):i_char += 1if add_line_numbers:- base_paragraph_config.update({"alignment": "right"})+ line_number_config = base_paragraph_config.copy()+ line_number_config["alignment"] = "right"+ line_number_color = selected_style.line_number_color+ if line_number_color == "inherit":+ line_number_color = foreground_colorself.line_numbers = Paragraph(*[str(i)@@ -209,7 +266,8 @@ class Code(VMobject, metaclass=ConvertToOpenGL):line_numbers_from, line_numbers_from + len(self.code_lines))],- **base_paragraph_config,+ color=line_number_color,+ **line_number_config,)self.line_numbers.next_to(self.code_lines, direction=LEFT).align_to(self.code_lines, UP@@ -220,10 +278,10 @@ class Code(VMobject, metaclass=ConvertToOpenGL):line.submobjects = [c for c in line if not isinstance(c, Dot)]self.add(self.code_lines)- if background_config is None:- background_config = {}background_config_base = self.default_background_config.copy()- background_config_base.update(background_config)+ background_config_base.update(background_config or {})+ if background_config_base["fill_color"] is None:+ background_config_base["fill_color"] = selected_style.background_colorif background == "rectangle":self.background = SurroundingRectangle(@@ -247,6 +305,22 @@ class Code(VMobject, metaclass=ConvertToOpenGL):self.add_to_back(self.background)+ @classmethod+ def get_pygments_style(cls, name: str) -> type[Style]:+ """Return the Pygments style registered under ``name``.++ Parameters+ ----------+ name+ The name of the Pygments style to retrieve.++ Returns+ -------+ type[Style]+ The corresponding Pygments style class.+ """+ return cast(type[Style], get_style_by_name(name))+@classmethoddef get_styles_list(cls) -> list[str]:"""Get the list of all available formatter styles."""diff --git a/tests/test_code_mobject.py b/tests/test_code_mobject.pyindex b06a299e4..674ef147f 100644--- a/tests/test_code_mobject.py+++ b/tests/test_code_mobject.py@@ -25,15 +25,98 @@ class FadeInSquare(Scene):def test_code_initialization_from_file():rendered_code = Code(- code_file="tests/test_code_mobject.py",+ code_file="example_scenes/basic.py",language="python",background="window",background_config={"fill_color": "#101010"},+ paragraph_config={"font_size": 14},)assert len(rendered_code.code_lines) == len(rendered_code.line_numbers)assert rendered_code.background.fill_color == ManimColor("#101010")+ ("formatter_style", "background_color", "text_color"),+ [+ ("xcode", "#ffffff", "#000000"),+ ("vim", "#000000", "#cccccc"),+ ],+)+def test_code_uses_formatter_style_colors(+ formatter_style, background_color, text_color+):+ rendered_code = Code(+ code_string="plain text",+ language="text",+ formatter_style=formatter_style,+ add_line_numbers=False,+ )++ assert rendered_code.background.fill_color == ManimColor(background_color)+ assert rendered_code.code_lines[0][0].get_color() == ManimColor(text_color)+++def test_code_ignores_paragraph_color():+ rendered_code = Code(+ code_string="plain text",+ language="text",+ formatter_style="vim",+ paragraph_config={"color": "#ff0000"},+ )++ assert rendered_code.code_lines[0][0].get_color() == ManimColor("#cccccc")+ assert rendered_code.line_numbers[0][0].get_color() == ManimColor("#cccccc")+++def test_code_ignores_default_paragraph_color(monkeypatch):+ monkeypatch.setitem(Code.default_paragraph_config, "color", "#ff0000")++ rendered_code = Code(+ code_string="plain text",+ language="text",+ formatter_style="vim",+ )++ assert rendered_code.code_lines[0][0].get_color() == ManimColor("#cccccc")+ assert rendered_code.line_numbers[0][0].get_color() == ManimColor("#cccccc")+++def test_code_accepts_custom_pygments_style():+ class CustomStyle(Code.get_pygments_style("xcode")):+ background_color = "#123456"+ line_number_color = "#abcdef"++ rendered_code = Code(+ code_string="plain text",+ language="text",+ formatter_style=CustomStyle,+ )++ assert rendered_code.background.fill_color == ManimColor("#123456")+ assert rendered_code.code_lines[0][0].get_color() == ManimColor("#000000")+ assert rendered_code.line_numbers[0][0].get_color() == ManimColor("#abcdef")+++ ("formatter_style", "line_number_color"),+ [+ ("solarized-light", ManimColor("#93a1a1")),+ ("vim", ManimColor("#cccccc")),+ ("xcode", ManimColor("#000000")),+ ],+)+def test_code_line_numbers_use_formatter_style_color(+ formatter_style, line_number_color+):+ rendered_code = Code(+ code_string="plain text",+ language="text",+ formatter_style=formatter_style,+ )++ assert rendered_code.line_numbers[0][0].get_color() == line_number_color++def test_line_heights_initial_whitespace():rendered_code = Code(code_string="""print('Hello, World!')