ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
26.7k stars 1.83k forks source link

Add darker, lighter and contrasting methods to ManimColor #3992

Closed hchargois closed 16 hours ago

hchargois commented 3 weeks ago

Overview

This adds 3 new methods on ManimColors: darker(), lighter() and contrasting(), which return new colors.

Motivation

This is useful to derive related colors from a base color, for example to write a function that builds VMobjects with some fill color, a slightly darker border, and an inset text in a contrasting color, all that from a single color parameter instead of 3:

def colored_square(color: ManimColor) -> Square:
    s = Square()
    s.set_fill(color, opacity=1)
    s.set_stroke(color.darker(), width=10)
    s.add(Text("hello", color=color.contrasting()))
    return s

Here's a slightly more exhaustive example:

DemoColors_ManimCE_v0 18 1

Generated by this Scene:

def named_swatch(name: str, color: ManimColor) -> VMobject:
    swatch = Rectangle(width=2.25, height=0.6)
    swatch.set_fill(color, opacity=1)
    swatch.set_stroke(width=0)
    swatch.add(Text(name, font_size=24, color=color.contrasting()))
    return swatch

def lighter_darker_swatches(name: str, color: ManimColor) -> VGroup:
    return VGroup(
        named_swatch(".darker(0.4)", color.darker(0.4)),
        named_swatch(".darker()", color.darker()),
        named_swatch(name, color),
        named_swatch(".lighter()", color.lighter()),
        named_swatch(".lighter(0.4)", color.lighter(0.4)),
    ).arrange_submobjects(RIGHT)

class DemoColors(Scene):
    def construct(self):
        swatches = VGroup(
            lighter_darker_swatches("RED", RED),
            lighter_darker_swatches("BLUE", BLUE),
            lighter_darker_swatches("GREEN", GREEN),
            lighter_darker_swatches("GOLD", GOLD),
            lighter_darker_swatches("PURPLE", PURPLE),
            lighter_darker_swatches("GRAY", GRAY),
            lighter_darker_swatches("WHITE", WHITE),
            lighter_darker_swatches("BLACK", BLACK),
        ).arrange_submobjects(DOWN)
        self.add(swatches)
hchargois commented 1 day ago

@behackl Thanks, I've responded to your comments in their own threads, let me know if I need to make any more changes, otherwise I'll probably need to squash and rebase?

behackl commented 17 hours ago

@behackl Thanks, I've responded to your comments in their own threads, let me know if I need to make any more changes, otherwise I'll probably need to squash and rebase?

We'll take it from here, I'll run tests one more time locally and then merge it.

behackl commented 16 hours ago

Thanks again for your contribution! 🚀