Factor rgb_to_hex, hex_to_int and int_to_hex away from StringMobject and to utils/color

This commit is contained in:
Grant Sanderson 2022-12-19 17:28:22 -08:00
parent e0e7e24351
commit 073a62bf03
4 changed files with 26 additions and 21 deletions

View file

@ -3,6 +3,8 @@ from __future__ import annotations
import re import re
from manimlib.mobject.svg.string_mobject import StringMobject from manimlib.mobject.svg.string_mobject import StringMobject
from manimlib.utils.color import color_to_hex
from manimlib.utils.color import hex_to_int
from manimlib.utils.tex_file_writing import tex_content_to_svg_file from manimlib.utils.tex_file_writing import tex_content_to_svg_file
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -159,7 +161,7 @@ class MTex(StringMobject):
@staticmethod @staticmethod
def get_color_command(rgb_hex: str) -> str: def get_color_command(rgb_hex: str) -> str:
rgb = MTex.hex_to_int(rgb_hex) rgb = hex_to_int(rgb_hex)
rg, b = divmod(rgb, 256) rg, b = divmod(rgb, 256)
r, g = divmod(rg, 256) r, g = divmod(rg, 256)
return f"\\color[RGB]{{{r}, {g}, {b}}}" return f"\\color[RGB]{{{r}, {g}, {b}}}"
@ -181,7 +183,7 @@ class MTex(StringMobject):
suffix_lines = [] suffix_lines = []
if not is_labelled: if not is_labelled:
prefix_lines.append(self.get_color_command( prefix_lines.append(self.get_color_command(
self.color_to_hex(self.base_color) color_to_hex(self.base_color)
)) ))
if self.alignment: if self.alignment:
prefix_lines.append(self.alignment) prefix_lines.append(self.alignment)

View file

@ -10,8 +10,9 @@ from manimlib.constants import WHITE
from manimlib.logger import log from manimlib.logger import log
from manimlib.mobject.svg.svg_mobject import SVGMobject from manimlib.mobject.svg.svg_mobject import SVGMobject
from manimlib.mobject.types.vectorized_mobject import VGroup from manimlib.mobject.types.vectorized_mobject import VGroup
from manimlib.utils.color import color_to_rgb from manimlib.utils.color import color_to_hex
from manimlib.utils.color import rgb_to_hex from manimlib.utils.color import hex_to_int
from manimlib.utils.color import int_to_hex
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -117,7 +118,7 @@ class StringMobject(SVGMobject, ABC):
for submob, labelled_svg_submob in zip( for submob, labelled_svg_submob in zip(
self.submobjects, labelled_svg.submobjects self.submobjects, labelled_svg.submobjects
): ):
label = self.hex_to_int(self.color_to_hex( label = hex_to_int(color_to_hex(
labelled_svg_submob.get_fill_color() labelled_svg_submob.get_fill_color()
)) ))
if label >= labels_count: if label >= labels_count:
@ -129,7 +130,7 @@ class StringMobject(SVGMobject, ABC):
"Unrecognizable color labels detected (%s). " + \ "Unrecognizable color labels detected (%s). " + \
"The result could be unexpected.", "The result could be unexpected.",
", ".join( ", ".join(
self.int_to_hex(color) int_to_hex(color)
for color in unrecognizable_colors for color in unrecognizable_colors
) )
) )
@ -196,18 +197,6 @@ class StringMobject(SVGMobject, ABC):
def span_contains(span_0: Span, span_1: Span) -> bool: def span_contains(span_0: Span, span_1: Span) -> bool:
return span_0[0] <= span_1[0] and span_0[1] >= span_1[1] return span_0[0] <= span_1[0] and span_0[1] >= span_1[1]
@staticmethod
def color_to_hex(color: ManimColor) -> str:
return rgb_to_hex(color_to_rgb(color))
@staticmethod
def hex_to_int(rgb_hex: str) -> int:
return int(rgb_hex[1:], 16)
@staticmethod
def int_to_hex(rgb_int: int) -> str:
return f"#{rgb_int:06x}".upper()
# Parsing # Parsing
def parse(self) -> None: def parse(self) -> None:
@ -380,7 +369,7 @@ class StringMobject(SVGMobject, ABC):
lambda label, flag, attr_dict: self.get_command_string( lambda label, flag, attr_dict: self.get_command_string(
attr_dict, attr_dict,
is_end=flag < 0, is_end=flag < 0,
label_hex=self.int_to_hex(label) if is_labelled else None label_hex=int_to_hex(label) if is_labelled else None
) )
) )
prefix, suffix = self.get_content_prefix_and_suffix( prefix, suffix = self.get_content_prefix_and_suffix(

View file

@ -15,6 +15,8 @@ from manimlib.constants import NORMAL
from manimlib.logger import log from manimlib.logger import log
from manimlib.mobject.svg.string_mobject import StringMobject from manimlib.mobject.svg.string_mobject import StringMobject
from manimlib.utils.customization import get_customization from manimlib.utils.customization import get_customization
from manimlib.utils.color import color_to_hex
from manimlib.utils.color import int_to_hex
from manimlib.utils.directories import get_downloads_dir from manimlib.utils.directories import get_downloads_dir
from manimlib.utils.directories import get_text_dir from manimlib.utils.directories import get_text_dir
from manimlib.utils.simple_functions import hash_string from manimlib.utils.simple_functions import hash_string
@ -366,7 +368,7 @@ class MarkupText(StringMobject):
self, is_labelled: bool self, is_labelled: bool
) -> tuple[str, str]: ) -> tuple[str, str]:
global_attr_dict = { global_attr_dict = {
"foreground": self.color_to_hex(self.base_color), "foreground": color_to_hex(self.base_color),
"font_family": self.font, "font_family": self.font,
"font_style": self.slant, "font_style": self.slant,
"font_weight": self.weight, "font_weight": self.weight,
@ -394,7 +396,7 @@ class MarkupText(StringMobject):
self.get_command_string( self.get_command_string(
global_attr_dict, global_attr_dict,
is_end=is_end, is_end=is_end,
label_hex=self.int_to_hex(0) if is_labelled else None label_hex=int_to_hex(0) if is_labelled else None
) )
for is_end in (False, True) for is_end in (False, True)
) )

View file

@ -62,6 +62,18 @@ def color_to_int_rgba(color: ManimColor, opacity: float = 1.0) -> np.ndarray[int
return np.array([*color_to_int_rgb(color), alpha], dtype=np.uint8) return np.array([*color_to_int_rgb(color), alpha], dtype=np.uint8)
def color_to_hex(color: ManimColor) -> str:
return rgb_to_hex(color_to_rgb(color))
def hex_to_int(rgb_hex: str) -> int:
return int(rgb_hex[1:], 16)
def int_to_hex(rgb_int: int) -> str:
return f"#{rgb_int:06x}".upper()
def color_gradient( def color_gradient(
reference_colors: Iterable[ManimColor], reference_colors: Iterable[ManimColor],
length_of_output: int length_of_output: int