A community-maintained Python framework for creating mathematical animations.
Color module enhancements (#1504)
Anton Ballmaier567f0c6dc995
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.pyindex 9050062bf..4b1d87ee7 100644--- a/manim/mobject/types/vectorized_mobject.py+++ b/manim/mobject/types/vectorized_mobject.py@@ -1445,7 +1445,7 @@ class VGroup(VMobject):class ArcShapeIris(Scene):def construct(self):- colors = [DARK_BLUE, DARK_BROWN, BLUE_E, BLUE_D, BLUE_A, TEAL_B, GREEN_B, YELLOW_E]+ colors = [DARK_BROWN, BLUE_E, BLUE_D, BLUE_A, TEAL_B, GREEN_B, YELLOW_E]radius = [1 + rad * 0.1 for rad in range(len(colors))]circles_group = VGroup()diff --git a/manim/utils/color.py b/manim/utils/color.pyindex 9672cda50..188144d9a 100644--- a/manim/utils/color.py+++ b/manim/utils/color.py@@ -1,4 +1,4 @@-"""Utility functions for conversion between color models."""+"""Colors and utility functions for conversion between different color models."""__all__ = ["color_to_rgb",@@ -16,81 +16,11 @@ __all__ = ["random_bright_color","random_color","get_shaded_rgb",- "DARK_BLUE",- "DARK_BROWN",- "LIGHT_BROWN",- "BLUE_E",- "BLUE_D",- "BLUE_C",- "BLUE",- "BLUE_B",- "BLUE_A",- "TEAL_E",- "TEAL_D",- "TEAL_C",- "TEAL",- "TEAL_B",- "TEAL_A",- "GREEN_E",- "GREEN_D",- "GREEN_C",- "GREEN",- "GREEN_B",- "GREEN_A",- "YELLOW_E",- "YELLOW_D",- "YELLOW_C",- "YELLOW",- "YELLOW_B",- "YELLOW_A",- "GOLD_E",- "GOLD_D",- "GOLD_C",- "GOLD",- "GOLD_B",- "GOLD_A",- "RED_E",- "RED_D",- "RED_C",- "RED",- "RED_B",- "RED_A",- "MAROON_E",- "MAROON_D",- "MAROON_C",- "MAROON",- "MAROON_B",- "MAROON_A",- "PURPLE_E",- "PURPLE_D",- "PURPLE_C",- "PURPLE",- "PURPLE_B",- "PURPLE_A",- "WHITE",- "BLACK",- "LIGHT_GRAY",- "LIGHT_GREY",- "GRAY",- "GREY",- "DARK_GREY",- "DARK_GRAY",- "DARKER_GREY",- "DARKER_GRAY",- "GREY_E",- "GREY_D",- "GREY_C",- "GREY_B",- "GREY_A",- "GREY_BROWN",- "PINK",- "LIGHT_PINK",- "GREEN_SCREEN",- "ORANGE",]import randomfrom enum import Enum+from typing import Iterable, List, Unionimport numpy as npfrom colour import Color@@ -106,24 +36,128 @@ class Colors(Enum):Examples--------- .. manim:: ColorExample+ .. manim:: ColorsOverview:save_last_frame:+ :hide_source:from manim.utils.color import Colors- class ColorExample(Scene):+ class ColorsOverview(Scene):def construct(self):- cols = Colors._member_names_- s = VGroup(*[Line(DOWN, UP, stroke_width=15).set_color(Colors[cols[i]].value) for i in range(0, len(cols))])- s.arrange_submobjects(buff=0.2)- self.add(s)-- The preferred way of using these colors is+ def color_group(color):+ group = VGroup(+ *[+ Line(ORIGIN, RIGHT * 1.5, stroke_width=35, color=Colors[name].value)+ for name in subnames(color)+ ]+ ).arrange_submobjects(buff=0.4, direction=DOWN)++ name = Text(color).scale(0.6).next_to(group, UP, buff=0.3)+ if any(decender in color for decender in "gjpqy"):+ name.shift(DOWN * 0.08)+ group.add(name)+ return group++ def subnames(name):+ return [name + "_" + char for char in "abcde"]++ color_groups = VGroup(+ *[+ color_group(color)+ for color in [+ "blue",+ "teal",+ "green",+ "yellow",+ "gold",+ "red",+ "maroon",+ "purple",+ ]+ ]+ ).arrange_submobjects(buff=0.2, aligned_edge=DOWN)++ for line, char in zip(color_groups[0], "abcde"):+ color_groups.add(Text(char).scale(0.6).next_to(line, LEFT, buff=0.2))++ def named_lines_group(length, colors, names, text_colors, align_to_block):+ lines = VGroup(+ *[+ Line(+ ORIGIN,+ RIGHT * length,+ stroke_width=55,+ color=Colors[color].value,+ )+ for color in colors+ ]+ ).arrange_submobjects(buff=0.6, direction=DOWN)++ for line, name, color in zip(lines, names, text_colors):+ line.add(Text(name, color=color).scale(0.6).move_to(line))+ lines.next_to(color_groups, DOWN, buff=0.5).align_to(+ color_groups[align_to_block], LEFT+ )+ return lines++ other_colors = (+ "pink",+ "light_pink",+ "orange",+ "light_brown",+ "dark_brown",+ "gray_brown",+ )++ other_lines = named_lines_group(+ 3.2,+ other_colors,+ other_colors,+ [BLACK] * 4 + [WHITE] * 2,+ 0,+ )++ gray_lines = named_lines_group(+ 6.6,+ ["white"] + subnames("gray") + ["black"],+ [+ "white",+ "lighter_gray / gray_a",+ "light_gray / gray_b",+ "gray / gray_c",+ "dark_gray / gray_d",+ "darker_gray / gray_e",+ "black",+ ],+ [BLACK] * 3 + [WHITE] * 4,+ 2,+ )++ pure_colors = (+ "pure_red",+ "pure_green",+ "pure_blue",+ )++ pure_lines = named_lines_group(+ 3.2,+ pure_colors,+ pure_colors,+ [BLACK, BLACK, WHITE],+ 6,+ )++ self.add(color_groups, other_lines, gray_lines, pure_lines)++ VGroup(*self.mobjects).move_to(ORIGIN)+++ The preferred way of using these colors is by importing their constants from manim:.. code-block:: pycon- >>> import manim.utils.color as C- >>> C.WHITE- '#FFFFFF'+ >>> from manim import RED, GREEN, BLUE+ >>> RED+ '#FC6255'Note this way uses the name of the colors in UPPERCASE.@@ -134,158 +168,200 @@ class Colors(Enum):.. code-block:: pycon>>> from manim.utils.color import Colors- >>> Colors.white.value- '#FFFFFF'+ >>> Colors.red.value+ '#FC6255'++ .. note::++ The colors of type "C" have an alias equal to the colorname without a letter,+ e.g. GREEN = GREEN_C"""- dark_blue = "#236B8E"- dark_brown = "#8B4513"- light_brown = "#CD853F"- blue_e = "#1C758A"- blue_d = "#29ABCA"- blue_c = "#58C4DD"- blue = "#58C4DD"- blue_b = "#9CDCEB"+ white = "#FFFFFF"+ gray_a = "#DDDDDD"+ gray_b = "#BBBBBB"+ gray_c = "#888888"+ gray_d = "#444444"+ gray_e = "#222222"+ black = "#000000"+ lighter_gray = gray_a+ light_gray = gray_b+ gray = gray_c+ dark_gray = gray_d+ darker_gray = gray_e+blue_a = "#C7E9F1"- teal_e = "#49A88F"- teal_d = "#55C1A7"- teal_c = "#5CD0B3"- teal = "#5CD0B3"- teal_b = "#76DDC0"+ blue_b = "#9CDCEB"+ blue_c = "#58C4DD"+ blue_d = "#29ABCA"+ blue_e = "#236B8E"+ pure_blue = "#0000FF"+ blue = blue_c+teal_a = "#ACEAD7"- green_e = "#699C52"- green_d = "#77B05D"- green_c = "#83C167"- green = "#83C167"- green_b = "#A6CF8C"+ teal_b = "#76DDC0"+ teal_c = "#5CD0B3"+ teal_d = "#55C1A7"+ teal_e = "#49A88F"+ teal = teal_c+green_a = "#C9E2AE"- yellow_e = "#E8C11C"- yellow_d = "#F4D345"- yellow_c = "#FFFF00"- yellow = "#FFFF00"- yellow_b = "#FFEA94"+ green_b = "#A6CF8C"+ green_c = "#83C167"+ green_d = "#77B05D"+ green_e = "#699C52"+ pure_green = "#00FF00"+ green = green_c+yellow_a = "#FFF1B6"- gold_e = "#C78D46"- gold_d = "#E1A158"- gold_c = "#F0AC5F"- gold = "#F0AC5F"- gold_b = "#F9B775"+ yellow_b = "#FFEA94"+ yellow_c = "#FFFF00"+ yellow_d = "#F4D345"+ yellow_e = "#E8C11C"+ yellow = yellow_c+gold_a = "#F7C797"- red_e = "#CF5044"- red_d = "#E65A4C"- red_c = "#FC6255"- red = "#FC6255"- red_b = "#FF8080"+ gold_b = "#F9B775"+ gold_c = "#F0AC5F"+ gold_d = "#E1A158"+ gold_e = "#C78D46"+ gold = gold_c+red_a = "#F7A1A3"- maroon_e = "#94424F"- maroon_d = "#A24D61"- maroon_c = "#C55F73"- maroon = "#C55F73"- maroon_b = "#EC92AB"+ red_b = "#FF8080"+ red_c = "#FC6255"+ red_d = "#E65A4C"+ red_e = "#CF5044"+ pure_red = "#FF0000"+ red = red_c+maroon_a = "#ECABC1"- purple_e = "#644172"- purple_d = "#715582"- purple_c = "#9A72AC"- purple = "#9A72AC"- purple_b = "#B189C6"+ maroon_b = "#EC92AB"+ maroon_c = "#C55F73"+ maroon_d = "#A24D61"+ maroon_e = "#94424F"+ maroon = maroon_c+purple_a = "#CAA3E8"- white = "#FFFFFF"- black = "#000000"- light_gray = "#BBBBBB"- light_grey = "#BBBBBB"- gray = "#888888"- grey = "#888888"- dark_grey = "#444444"- dark_gray = "#444444"- darker_grey = "#222222"- darker_gray = "#222222"- grey_e = "#222222"- grey_d = "#444444"- grey_c = "#888888"- grey_b = "#BBBBBB"- grey_a = "#DDDDDD"- grey_brown = "#736357"+ purple_b = "#B189C6"+ purple_c = "#9A72AC"+ purple_d = "#715582"+ purple_e = "#644172"+ purple = purple_c+pink = "#D147BD"light_pink = "#DC75CD"- green_screen = "#00FF00"+orange = "#FF862F"+ light_brown = "#CD853F"+ dark_brown = "#8B4513"+ gray_brown = "#736357"+++# Create constants from Colors enum+constants_names = []+for name, value in Colors.__members__.items():+ name = name.upper()+ value = value.value+ constants_names.append(name)+ locals()[name] = value+ if "GRAY" in name:+ name = name.replace("GRAY", "GREY")+ locals()[name] = value+ constants_names.append(name)++# Add constants to module exports. Simply adding constants_names would work fine, but+# would make it hard for IDEs to understand that colors are exported. Therefor the+# result of the following print statement is added instead.++# print(constants_names)++__all__ += [ # noqa: F822 # used to stop flake8 from complaining about undefined vars+ "WHITE",+ "GRAY_A",+ "GREY_A",+ "GRAY_B",+ "GREY_B",+ "GRAY_C",+ "GREY_C",+ "GRAY_D",+ "GREY_D",+ "GRAY_E",+ "GREY_E",+ "BLACK",+ "LIGHTER_GRAY",+ "LIGHTER_GREY",+ "LIGHT_GRAY",+ "LIGHT_GREY",+ "GRAY",+ "GREY",+ "DARK_GRAY",+ "DARK_GREY",+ "DARKER_GRAY",+ "DARKER_GREY",+ "BLUE_A",+ "BLUE_B",+ "BLUE_C",+ "BLUE_D",+ "BLUE_E",+ "PURE_BLUE",+ "BLUE",+ "TEAL_A",+ "TEAL_B",+ "TEAL_C",+ "TEAL_D",+ "TEAL_E",+ "TEAL",+ "GREEN_A",+ "GREEN_B",+ "GREEN_C",+ "GREEN_D",+ "GREEN_E",+ "PURE_GREEN",+ "GREEN",+ "YELLOW_A",+ "YELLOW_B",+ "YELLOW_C",+ "YELLOW_E",+ "YELLOW_D",+ "YELLOW",+ "GOLD_A",+ "GOLD_B",+ "GOLD_C",+ "GOLD_D",+ "GOLD_E",+ "GOLD",+ "RED_A",+ "RED_B",+ "RED_C",+ "RED_D",+ "RED_E",+ "PURE_RED",+ "RED",+ "MAROON_A",+ "MAROON_B",+ "MAROON_C",+ "MAROON_D",+ "MAROON_E",+ "MAROON",+ "PURPLE_A",+ "PURPLE_B",+ "PURPLE_C",+ "PURPLE_D",+ "PURPLE_E",+ "PURPLE",+ "PINK",+ "LIGHT_PINK",+ "ORANGE",+ "LIGHT_BROWN",+ "DARK_BROWN",+ "GRAY_BROWN",+ "GREY_BROWN",+]-DARK_BLUE = Colors.dark_blue.value-DARK_BROWN = Colors.dark_brown.value-LIGHT_BROWN = Colors.dark_brown.value-BLUE_E = Colors.blue_e.value-BLUE_D = Colors.blue_d.value-BLUE_C = Colors.blue_c.value-BLUE = Colors.blue.value-BLUE_B = Colors.blue_b.value-BLUE_A = Colors.blue_a.value-TEAL_E = Colors.teal_e.value-TEAL_D = Colors.teal_d.value-TEAL_C = Colors.teal_c.value-TEAL = Colors.teal.value-TEAL_B = Colors.teal_b.value-TEAL_A = Colors.teal_a.value-GREEN_E = Colors.green_e.value-GREEN_D = Colors.green_d.value-GREEN_C = Colors.green_c.value-GREEN = Colors.green.value-GREEN_B = Colors.green_b.value-GREEN_A = Colors.green_a.value-YELLOW_E = Colors.yellow_e.value-YELLOW_D = Colors.yellow_d.value-YELLOW_C = Colors.yellow_c.value-YELLOW = Colors.yellow.value-YELLOW_B = Colors.yellow_b.value-YELLOW_A = Colors.yellow_a.value-GOLD_E = Colors.gold_e.value-GOLD_D = Colors.gold_d.value-GOLD_C = Colors.gold_c.value-GOLD = Colors.gold.value-GOLD_B = Colors.gold_b.value-GOLD_A = Colors.gold_a.value-RED_E = Colors.red_e.value-RED_D = Colors.red_d.value-RED_C = Colors.red_c.value-RED = Colors.red.value-RED_B = Colors.red_b.value-RED_A = Colors.red_a.value-MAROON_E = Colors.maroon_e.value-MAROON_D = Colors.maroon_d.value-MAROON_C = Colors.maroon_c.value-MAROON = Colors.maroon.value-MAROON_B = Colors.maroon_b.value-MAROON_A = Colors.maroon_a.value-PURPLE_E = Colors.purple_e.value-PURPLE_D = Colors.purple_d.value-PURPLE_C = Colors.purple_c.value-PURPLE = Colors.purple.value-PURPLE_B = Colors.purple_b.value-PURPLE_A = Colors.purple_a.value-WHITE = Colors.white.value-BLACK = Colors.black.value-LIGHT_GRAY = Colors.light_gray.value-LIGHT_GREY = Colors.light_grey.value-GRAY = Colors.gray.value-GREY = Colors.grey.value-DARK_GREY = Colors.dark_grey.value-DARK_GRAY = Colors.dark_gray.value-DARKER_GREY = Colors.darker_gray.value-DARKER_GRAY = Colors.darker_gray.value-GREY_BROWN = Colors.grey_brown.value-GREY_E = Colors.grey_e.value-GREY_D = Colors.grey_d.value-GREY_C = Colors.grey_c.value-GREY_B = Colors.grey_b.value-GREY_A = Colors.grey_a.value-PINK = Colors.pink.value-LIGHT_PINK = Colors.light_pink.value-GREEN_SCREEN = Colors.green_screen.value-ORANGE = Colors.orange.value---def color_to_rgb(color):+def color_to_rgb(color: Union[Color, str]) -> np.ndarray:if isinstance(color, str):return hex_to_rgb(color)elif isinstance(color, Color):@@ -294,46 +370,45 @@ def color_to_rgb(color):raise ValueError("Invalid color type")-def color_to_rgba(color, alpha=1):+def color_to_rgba(color: Union[Color, str], alpha: float = 1) -> np.ndarray:return np.array([*color_to_rgb(color), alpha])-def rgb_to_color(rgb):- try:- return Color(rgb=rgb)- except Exception:- return Color(WHITE)+def rgb_to_color(rgb: Iterable[float]) -> Color:+ return Color(rgb=rgb)-def rgba_to_color(rgba):+def rgba_to_color(rgba: Iterable[float]) -> Color:return rgb_to_color(rgba[:3])-def rgb_to_hex(rgb):+def rgb_to_hex(rgb: Iterable[float]) -> str:return "#" + "".join("%02x" % int(255 * x) for x in rgb)-def hex_to_rgb(hex_code):+def hex_to_rgb(hex_code: str) -> np.ndarray:hex_part = hex_code[1:]if len(hex_part) == 3:hex_part = "".join([2 * c for c in hex_part])return np.array([int(hex_part[i : i + 2], 16) / 255 for i in range(0, 6, 2)])-def invert_color(color):+def invert_color(color: Color) -> Color:return rgb_to_color(1.0 - color_to_rgb(color))-def color_to_int_rgb(color):+def color_to_int_rgb(color: Color) -> np.ndarray:return (255 * color_to_rgb(color)).astype("uint8")-def color_to_int_rgba(color, opacity=1.0):+def color_to_int_rgba(color: Color, opacity: float = 1.0) -> np.ndarray:alpha = int(255 * opacity)return np.append(color_to_int_rgb(color), alpha)-def color_gradient(reference_colors, length_of_output):+def color_gradient(+ reference_colors: Iterable[Color], length_of_output: int+) -> List[Color]:if length_of_output == 0:return reference_colors[0]rgbs = list(map(color_to_rgb, reference_colors))@@ -349,29 +424,34 @@ def color_gradient(reference_colors, length_of_output):]-def interpolate_color(color1, color2, alpha):+def interpolate_color(color1: Color, color2: Color, alpha: float) -> Color:rgb = interpolate(color_to_rgb(color1), color_to_rgb(color2), alpha)return rgb_to_color(rgb)-def average_color(*colors):+def average_color(*colors: Color) -> Color:rgbs = np.array(list(map(color_to_rgb, colors)))mean_rgb = np.apply_along_axis(np.mean, 0, rgbs)return rgb_to_color(mean_rgb)-def random_bright_color():+def random_bright_color() -> Color:color = random_color()curr_rgb = color_to_rgb(color)new_rgb = interpolate(curr_rgb, np.ones(len(curr_rgb)), 0.5)return Color(rgb=new_rgb)-def random_color():+def random_color() -> Color:return random.choice([c.value for c in list(Colors)])-def get_shaded_rgb(rgb, point, unit_normal_vect, light_source):+def get_shaded_rgb(+ rgb: np.ndarray,+ point: np.ndarray,+ unit_normal_vect: np.ndarray,+ light_source: np.ndarray,+) -> np.ndarray:to_sun = normalize(light_source - point)factor = 0.5 * np.dot(unit_normal_vect, to_sun) ** 3if factor < 0:diff --git a/tests/control_data/graphical_units_data/threed/AmbientCameraMoveTest.npz b/tests/control_data/graphical_units_data/threed/AmbientCameraMoveTest.npzindex 0a0834e2a..b918d65ee 100644Binary files a/tests/control_data/graphical_units_data/threed/AmbientCameraMoveTest.npz and b/tests/control_data/graphical_units_data/threed/AmbientCameraMoveTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/Arrow3DTest.npz b/tests/control_data/graphical_units_data/threed/Arrow3DTest.npzindex f1e0721a9..3ebbfddd1 100644Binary files a/tests/control_data/graphical_units_data/threed/Arrow3DTest.npz and b/tests/control_data/graphical_units_data/threed/Arrow3DTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/AxesTest.npz b/tests/control_data/graphical_units_data/threed/AxesTest.npzindex 99df093fa..c219a448a 100644Binary files a/tests/control_data/graphical_units_data/threed/AxesTest.npz and b/tests/control_data/graphical_units_data/threed/AxesTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/CameraMoveTest.npz b/tests/control_data/graphical_units_data/threed/CameraMoveTest.npzindex c6f80a1c0..f43882a93 100644Binary files a/tests/control_data/graphical_units_data/threed/CameraMoveTest.npz and b/tests/control_data/graphical_units_data/threed/CameraMoveTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/ConeTest.npz b/tests/control_data/graphical_units_data/threed/ConeTest.npzindex 64fd64381..cc277a0bd 100644Binary files a/tests/control_data/graphical_units_data/threed/ConeTest.npz and b/tests/control_data/graphical_units_data/threed/ConeTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/CubeTest.npz b/tests/control_data/graphical_units_data/threed/CubeTest.npzindex a226d9283..a066a789c 100644Binary files a/tests/control_data/graphical_units_data/threed/CubeTest.npz and b/tests/control_data/graphical_units_data/threed/CubeTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/CylinderTest.npz b/tests/control_data/graphical_units_data/threed/CylinderTest.npzindex 7898966cb..13d1eda80 100644Binary files a/tests/control_data/graphical_units_data/threed/CylinderTest.npz and b/tests/control_data/graphical_units_data/threed/CylinderTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/Dot3DTest.npz b/tests/control_data/graphical_units_data/threed/Dot3DTest.npzindex 4f1c6446e..04140b872 100644Binary files a/tests/control_data/graphical_units_data/threed/Dot3DTest.npz and b/tests/control_data/graphical_units_data/threed/Dot3DTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/Line3DTest.npz b/tests/control_data/graphical_units_data/threed/Line3DTest.npzindex 275fad16a..d47f535fa 100644Binary files a/tests/control_data/graphical_units_data/threed/Line3DTest.npz and b/tests/control_data/graphical_units_data/threed/Line3DTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/SphereTest.npz b/tests/control_data/graphical_units_data/threed/SphereTest.npzindex 85c81c82a..1e5316426 100644Binary files a/tests/control_data/graphical_units_data/threed/SphereTest.npz and b/tests/control_data/graphical_units_data/threed/SphereTest.npz differdiff --git a/tests/control_data/graphical_units_data/threed/TorusTest.npz b/tests/control_data/graphical_units_data/threed/TorusTest.npzindex f9c4673af..5dded1569 100644Binary files a/tests/control_data/graphical_units_data/threed/TorusTest.npz and b/tests/control_data/graphical_units_data/threed/TorusTest.npz differ