Skip to content

Refactor color_gradient#4663

Open
GoThrones wants to merge 4 commits intoManimCommunity:mainfrom
GoThrones:refactor_color_gradient
Open

Refactor color_gradient#4663
GoThrones wants to merge 4 commits intoManimCommunity:mainfrom
GoThrones:refactor_color_gradient

Conversation

@GoThrones
Copy link
Copy Markdown
Contributor

@GoThrones GoThrones commented Mar 29, 2026

Overview: What does this pull request change?

This PR refactors the color_gradient method in core.py. It introduces two new parameters: transition_mode and blend_variation for the case when number_of_colors_provided = 1.

Motivation and Explanation: Why and how do your changes improve the library?

The code of color_gradient of main branch doesn't work as intended for the case when number of colors provided is 1. It returns back the same color, same brightness.

The PR makes it possible that when number of color provided is 1, then either the colors can be progressively lighter or darker as chosen, but the color itself will be of same hue. Also the brightness of successive colors can be varied using blend_variation. See the output below:

I used this code to test:

class trycolorgradient(Scene):
    def construct(self):
        #s = Text("India")
        sgroup = VGroup()
        colors = color_gradient([RED], 5)
        for i in range(len(colors)):
            sq = Square().set_color(colors[i])
            sgroup.add(sq)
        self.add(sgroup.arrange(RIGHT))

Here's the output of the main branch: All the squares have same colors and same brightness. That means no gradient at all. Since the method's name is color_gradient, the effect should have gradient.
main

Here's the output given by PR branch:
When transition_mode = "lighter". Each next square is lighter than the previous one.
PR lighter

When transition_mode = "darker". Each next square is darker than the previous one.
PR darker

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@chopan050
Copy link
Copy Markdown
Member

Thanks for your contribution!

This is just a comment, not an approval nor a request of changes. I would like other developers to discuss and give their opinion on this change.

The code of color_gradient of main branch doesn't work as intended for the case when number of colors provided is 1. It returns back the same color, same brightness.

IMO, it does work as intended. color_gradient(color_list, n) remaps the color_list into a new list of colors of length n by interpolating between the original colors. Thus, if color_list only contains one color, it does make sense that the output list contains n times that same color.

If one would want to create a gradient of 5 colors between, say, RED and a lighter version of RED, they could use

color_gradient([RED, RED.lighter(blend_variation)], 5)

which is roughly equivalent to your proposal

color_gradient([RED], 5, "lighter", blend_variation)

@GoThrones
Copy link
Copy Markdown
Contributor Author

The way i see it, the method's name has the word gradient in it. So, the output of this method must be some sort of gradient. If that isn't happening, when only 1 color has been provided, then why would anyone invoke this method for 1 color. That would me that the user would be forced to look for some other way to make gradient of a single color. The way I've refactored it, is more useful. Irrespective of 1 color or many, it gives a gradient of colors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants