Skip to content

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

Description

@Helbronner

Description of bug / unexpected behavior

Currently, the Code class sets its container's fill_color to ManimColor("#222") by default. However, this default value takes precedence over the background color defined inside the formatter_style provided by Pygments styles.

As a result, even if a user explicitly sets a light theme like formatter_style="xcode", the container still uses the dark #222 background, making the light-themed text unreadable. A similar issue occurs with dark themes, though it is less noticeable since the text remains legible.

Expected behavior

When a user specifies a formatter_style (regardless of whether it is a light or dark theme, such as xcode or lightbulb), the container's background color should match the background color defined by that specific style.

How to reproduce the issue

Code for reproducing the problem
from typing import Any

from manim import *


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}")
"""

        rendered_code = Code(
            # code_file=file,
            code_string=code_snippets,
            language="python",
            formatter_style="xcode",  # 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)

Additional media files

Images/GIFs Image

Logs

Terminal output
No exceptions or terminal logs are generated, as this is a silent visual/logic defect.

Affected versions

  • Manim Community Edition v0.20.1
  • Latest main branch (Reproduced)

Additional comments

After reviewing the source code, I suspect the hardcoded background color might have been a temporary workaround.

However, Pygments styles natively provide a background_color attribute (which is what generates the background style in their HTML output). Since we can now reliably get the background color for every style directly from Pygments, I would be happy to submit a Pull Request to fix this issue if that works for you!

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions