Skip to content

fix(code): inherit container background and adjust line number color#4816

Open
Helbronner wants to merge 3 commits into
ManimCommunity:mainfrom
Helbronner:fix-code-background-color
Open

fix(code): inherit container background and adjust line number color#4816
Helbronner wants to merge 3 commits into
ManimCommunity:mainfrom
Helbronner:fix-code-background-color

Conversation

@Helbronner

@Helbronner Helbronner commented Jun 17, 2026

Copy link
Copy Markdown

Overview: What does this pull request change?

This pull request fixes an issue (#4811 ) where custom formatter_style (especially in a light theme like xcode) was previously overridden by the hardcoded dark ManimColor("#222") default fill_color. It also updates the line number color logic to dynamically respect the active Pygments style.

Fixes #4811

Motivation and Explanation: Why and how do your changes improve the library?

Previously, specifying a light formatter_style (e.g., xcode) resulted in an incorrect dark container background because of the hardcoded ManimColor("#222") default.

This pull request resolves the issue by dynamically extracting the native background_color from the active Pygments style. Additionally, line number colors are now dynamically fetched from the style context (falling back to GRAY if unspecified) to prevent invisible white text on light backgrounds.

Q&A

Q: Why is the fill_color attribute preserved instead of deleted?
A: To maintain backward compatibility. While deprecating fill_color aligns with the WYSIWYG principle (since Pygments styles should dictate the theme), removing it immediately would break existing user configurations. It is recommended to deprecate this attribute now and remove it during a future major refactor of the Code class.

Links to added or changed documentation pages

Further Information and Comments

Verification

This fix successfully resolves the issue. Below is the demonstration of the expected behavior:

Test.mp4

Test Comparison

Category Style Before After
Light xcode xcode-before xcode-after
Light solarized-light solarized-light-before solarized-light-after
Dark lightbulb lightbulb-before lightbulb-after
Dark dracula dracula-before dracula-after

Test Script

Code for verifying the fix
from typing import Any

from manim import *
from pygments.styles import STYLE_MAP


class Test(Scene):
    def construct(self):

        default_background_config: dict[str, Any] = {
            "buff": 0.3,
            # "fill_color": ManimColor("#222"),  # Here
            "stroke_color": WHITE,
            "corner_radius": 0.2,
            "stroke_width": 1,
            "fill_opacity": 1,
        }

        default_paragraph_config: dict[str, Any] = {
            "font": "Monospace",
            "font_size": 24,
            "line_spacing": 0.5,
            "disable_ligatures": True,
        }

        # file: str = "code_snippets.py"
        code_snippets: str = """from collections.abc import Iterator


# This is an example
class Math:
    @staticmethod
    def fib(n: int) -> Iterator[int]:
        \"""Fibonacci series up to n.\"""
        a, b = 0, 1
        while a < n:
            yield a
            a, b = b, a + b


result = sum(Math.fib(42))
print(f"The answer is {result}")
"""

        for style in list(STYLE_MAP.keys())[:]:
            mark: str = style
            rendered_code = Code(
                # code_file=file,
                code_string=mark + "\n\n" + code_snippets,
                language="python",
                formatter_style=style,  # Here
                tab_width=4,
                add_line_numbers=True,
                line_numbers_from=1,
                background="window",
                background_config=default_background_config,
                paragraph_config=default_paragraph_config,
            )

            self.add(rendered_code)
            self.wait(2)
            self.remove(rendered_code)

Related Resources

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Thanks for your time and review!

Using a custom `formatter_style` (such as a light theme) was previously overridden by the hardcoded dark `ManimCommunity#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 ManimCommunity#4811
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code class: background color should adapt to formatter_style instead of defaulting to #222

1 participant