Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add darker, lighter and contrasting methods to ManimColor #3992

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

hchargois
Copy link
Contributor

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)

Copy link

@OliverStrait OliverStrait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I liked darker and lighter methods. Doing only one thing.
  • Contrasting is more problematic because it clearly is a solution for very spesific problem around texts. It does not actually calculate true contrasting/complementary color which is default expectation from that name. Something like text_color() would be better.

manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Outdated Show resolved Hide resolved
Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! I've just left a bunch of minor comments; mainly concerning the use of pre-existing color constants and minor improvements to the documentaiton.

Otherwise, these changes are perfectly fine (and very useful)! Please take a look and let us know what you think!

manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Outdated Show resolved Hide resolved
manim/utils/color/core.py Dismissed Show dismissed Hide dismissed
manim/utils/color/core.py Dismissed Show dismissed Hide dismissed
manim/utils/color/core.py Dismissed Show dismissed Hide dismissed
Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your efforts!

@hchargois
Copy link
Contributor Author

@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
Copy link
Member

behackl commented Nov 25, 2024

@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 behackl added the new feature Enhancement specifically adding a new feature (feature request should be used for issues instead) label Nov 25, 2024
@behackl behackl added this to the v0.19.0 milestone Nov 25, 2024
@behackl behackl merged commit fac0aa5 into ManimCommunity:main Nov 25, 2024
18 checks passed
@behackl
Copy link
Member

behackl commented Nov 25, 2024

Thanks again for your contribution! 🚀

@hchargois hchargois deleted the color-darker-lighter branch November 25, 2024 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Enhancement specifically adding a new feature (feature request should be used for issues instead)
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants