Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Better parsing of color styles in CodeMobject (#4454)

Sir James Clark Maxwell3e8f41c9bedc
diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py
index 39fae21db..0f86b20ed 100644
--- a/manim/mobject/text/code_mobject.py
+++ b/manim/mobject/text/code_mobject.py
@@ -6,6 +6,7 @@ __all__ = [
"Code",
]
+import re
from pathlib import Path
from typing import Any, Literal
@@ -169,10 +170,10 @@ class Code(VMobject, metaclass=ConvertToOpenGL):
if child.name == "span":
try:
child_style = child["style"]
- if isinstance(child_style, str):
- color = child_style.removeprefix("color: ")
- else:
- color = None
+ match_ = re.match(
+ r"color: (#[A-Fa-f0-9]{6}|#[A-Fa-f0-9]{3})", child_style
+ )
+ color = None if match_ is None else match_.group(1)
except KeyError:
color = None
current_line_color_ranges.append(
diff --git a/tests/test_code_mobject.py b/tests/test_code_mobject.py
index 98364d964..b06a299e4 100644
--- a/tests/test_code_mobject.py
+++ b/tests/test_code_mobject.py
@@ -1,4 +1,5 @@
import numpy as np
+import pytest
from manim.mobject.text.code_mobject import Code
from manim.utils.color.core import ManimColor
@@ -45,3 +46,17 @@ for _ in range(42):
rendered_code.code_lines[0].height,
rendered_code.code_lines[2].height,
)
+
+
+def test_code_initialization_style_correct_color():
+ for style in Code.get_styles_list():
+ try:
+ Code(
+ code_string="""# This is a comment.
+var = 3
+print(var)
+""",
+ formatter_style=style,
+ )
+ except ValueError as e:
+ pytest.fail(f"Code initialization failed for style {style} with error: {e}")