From f9079e3619bc30afd3c7ac526bec75c16d36dac8 Mon Sep 17 00:00:00 2001 From: Helbronner <185764645+Helbronner@users.noreply.github.com> Date: Wed, 17 Jun 2026 16:09:57 +0800 Subject: [PATCH 1/2] fix(code): inherit container background and adjust line number color Using a custom `formatter_style` (such as a light theme) was previously overridden by the hardcoded dark `#222` default `fill_color`. This change dynamically extracts the native `background_color` attribute from the specified Pygments style to ensure the container matches the theme. Additionally, updated the line number color logic: it now respects the color defined by the active Pygments style, falling back to `GRAY` if unspecified, preventing invisible white text on light backgrounds. Fixes #4811 --- manim/mobject/text/code_mobject.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py index c9b66a9e3b..721e1581f2 100644 --- a/manim/mobject/text/code_mobject.py +++ b/manim/mobject/text/code_mobject.py @@ -14,7 +14,7 @@ from pygments import highlight from pygments.formatters.html import HtmlFormatter from pygments.lexers import get_lexer_by_name, guess_lexer, guess_lexer_for_filename -from pygments.styles import get_all_styles +from pygments.styles import get_all_styles, get_style_by_name from manim.constants import * from manim.mobject.geometry.arc import Dot @@ -22,7 +22,7 @@ from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL from manim.mobject.types.vectorized_mobject import VGroup, VMobject from manim.typing import StrPath -from manim.utils.color import WHITE, ManimColor +from manim.utils.color import BLACK, GRAY, WHITE, ManimColor class Code(VMobject, metaclass=ConvertToOpenGL): @@ -107,7 +107,7 @@ def construct(self): _styles_list_cache: list[str] | None = None default_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, @@ -203,6 +203,7 @@ def __init__( paragraph_config = {} base_paragraph_config = self.default_paragraph_config.copy() base_paragraph_config.update(paragraph_config) + base_paragraph_config.update({"color": BLACK}) from manim.mobject.text.text_mobject import Paragraph @@ -214,8 +215,16 @@ def __init__( for start, end, color in color_range: line[start:end].set_color(color) + selected_style = get_style_by_name(formatter_style) if add_line_numbers: base_paragraph_config.update({"alignment": "right"}) + + line_number_color: str = selected_style.line_number_color + if line_number_color == "inherit": + base_paragraph_config.update({"color": GRAY}) + else: + base_paragraph_config.update({"color": line_number_color}) + self.line_numbers = Paragraph( *[ str(i) @@ -239,6 +248,12 @@ def __init__( background_config_base = self.default_background_config.copy() background_config_base.update(background_config) + if background_config_base["fill_color"] is None: + background_color: str = selected_style.background_color + background_config_base.update({"fill_color": background_color}) + else: + background_config_base.update({"fill_color": ManimColor("#222")}) + if background == "rectangle": self.background = SurroundingRectangle( self, From dd6c87b94f2b4522b0b97e09328a740d95ee3864 Mon Sep 17 00:00:00 2001 From: Helbronner <185764645+Helbronner@users.noreply.github.com> Date: Wed, 17 Jun 2026 20:53:29 +0800 Subject: [PATCH 2/2] fix(code): respect user-defined fill_color for container background Previously, user-defined background `fill_color` was accidentally overridden by the default fallback color. This fix ensures that the custom value is preserved, passing the related test assertions. --- manim/mobject/text/code_mobject.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py index 721e1581f2..1021d32697 100644 --- a/manim/mobject/text/code_mobject.py +++ b/manim/mobject/text/code_mobject.py @@ -22,7 +22,7 @@ from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL from manim.mobject.types.vectorized_mobject import VGroup, VMobject from manim.typing import StrPath -from manim.utils.color import BLACK, GRAY, WHITE, ManimColor +from manim.utils.color import BLACK, GRAY, WHITE class Code(VMobject, metaclass=ConvertToOpenGL): @@ -251,8 +251,6 @@ def __init__( if background_config_base["fill_color"] is None: background_color: str = selected_style.background_color background_config_base.update({"fill_color": background_color}) - else: - background_config_base.update({"fill_color": ManimColor("#222")}) if background == "rectangle": self.background = SurroundingRectangle(