본문 바로가기

Programming/Manim Lectures

[05-1-D]Coloring methods: set_color, set_color_by_gradient

반응형

You can specify the color of the object. Specifying a single color is set_color, giving a gradient, and specifying it is set_color_by_gradient.

 


set_color

manimlib.mobject.mobject.Mobject.set_color(self, color=YELLOW_C, family=True)

Changes the object color to the specified value. For the color value, you can use the constant defined in constants.py file, or you can directly specify the color value in the form of "#rrggbb", where 'rr' means hexadecimal value of red color, 'gg' is for green and 'bb' is for blue color.

 

In the case of shape type object(Rectangle, Circel, ect), only the outline is affected by the set_color method, and the color of the entire inside of the shape object is affected by the fill_color argument of the object.

 

Parameters: color=YELLOW_C
    the color to be set. 
    constants difined in the manimlib/constants.py file are used to set the color value

Parameters:  family=True
    in case of group object consist of multiple sub-objects,
    if 'family=True', change the the entire color of sub-objects 
    
Returns: the object itself that the color is changed

Among the color constants defined in constants.py file, values such as YELLOW, BLUE, and RED are not visible, and only values such as YELLOW_E, BLUE_D, and RED_A are shown.

 

However in practice, YELLOW, BLUE, and RED can be used. This is because there is the following code when looking at the bottom of the constants.

for name in [s for s in list(COLOR_MAP.keys()) if s.endswith("_C")]:
    locals()[name.replace("_C", "")] = locals()[name]

 

Refer to the figure below for the actual color for the entire color value.


set_color_by_gradient

manimlib.mobject.mobject.Mobject.set_color_by_gradient(self, *colors)

Gradiently (gradually) changes the color of an object.

 

Parameters: *colors
    Color values to sepcify. 
    If there is no value in this method, an Exception will be occured.
    
    If color is only one, it is same with set_color.
    
    If there is more than one, use these colors to gradually change the color of the object
   
Returns: The object itself has changed color

Changes the color gradually for group objects where multiple objects are gathered.

 

You can't have a shape change color gradually but only for the goup of object is possible.

In the case of text, since a sentence is composed of several objects, the color can be gradually changed for the entire sentence.

 

The usage example below shows the result of applying a gradient to a text object and a group object consisting of multiple dots.

 

 def gradient_test(self):
        #dots
        dots = VGroup(*[Dot(radius=0.15) for i in range(20)])
        dots.arrange(RIGHT)
        dots.set_color_by_gradient(PINK, BLUE, YELLOW)

        #text
        text = TextMobject("Gradient Color")
        text.set_color_by_gradient(RED,YELLOW)
        text.next_to(dots, UP)

        #play
        self.play(
            FadeIn(dots),
            Write(text),
            run_time=2,
        )
        self.wait()

        self.remove(dots, text)

Next: [05-1-E]Shape changing methods: scale, rotate

 

[05-1-E]Shape changing methods: scale, rotate

On this page, you'll learn about the 'scale' method to rescale an object and the 'rotate' method to move around it. scale manimlib.mobject.mobject.Mobject.scale(self, scale_factor, **kwargs) Resize..

infograph.tistory.com

Go To: [99] Table of Contents

반응형