How to add color in turtle drawings?



Introduction

You can fill color in turtle drawings and It accepts different types of color modes. You can use RGB mode or HEX color code. To fill color in shapes and images, you need to know five color filling methods. We will demonstrate each method with an example. Moreover, you get access to five activities with a source code to give you an idea to fill the color in different ways.

Before you learn about how to fill a color in turtle drawings, make sure that you know how to draw shapes and pictures. Click here to learn about turtle drawings.

Five methods to add color in turtle drawings

Now, let us see all five methods one by one:

1. color()

The color() method accepts one or two arguments as a color string or color values(r,g,b). If we pass two arguments, the first one is set to pen color and the second to fill color. However, if we pass only one argument, the same color is set for both pen color and fill color.

Syntax (color() method with one argument):

color (color_string) | color(r,g,b)

Example:

from turtle import *
color("green") # pen color and fill color are set to green.

Syntax(color() method with two arguments):

color(color_string1,color_string2),color(r,b,g)

Example:

from turtle import *
color("green", "red") # Red is fill color and green is pen color



2. pencolor(*args)

The pencolor() method accepts one argument to set the color of the pen.

Syntax:

pencolor(color_string) | pencolor(r,g,b)

Example:

from turtle import *  
pencolor("blue")	# Set pen color to blue
3. fillcolor()

The fillcolor() method accepts one argument to set the fill color of the shape.

Syntax:

fillcolor(color_string) | fillcolor(r,g,b)

Example:

from turtle import * 	
fillcolor("yellow")	#set fill color to yellow
4. begin_fill()

The begin_fill() method is used to indicate that the program will be drawing a closed shape that needs to be filled in. This method is called before drawing the shape.

Syntax:

begin_fill()


5. end_fill()

The end_fill() method is called after drawing the closed shape and fill the shape with the specified color.

Syntax:

end_fill()

Note: The begin_fill() and end_fill() method has to use together. First, to fill color into a shape, we have to call begin_fill(). Then the filling of color should be closed by the end_fill() method.

Example:

from turtle import *    
pencolor('blue')      
pensize(10)		       
fillcolor('yellow')     
begin_fill()              
circle(70)         
end_fill()                 

Output:

begin_fill() and end_fill() in turtle
Activity
1. Bhutan National Flag

Question: write a program to draw the National flag of Bhutan in the turtle graphics.

Source code:

from turtle import *
pensize(3)
shape("turtle")
penup()
goto(-200,0)
pendown()
fillcolor('yellow')
begin_fill()
left(90)
forward(200)
right(90)
forward(300)
right(146)
forward(360)
end_fill()
fillcolor('orange')
begin_fill()
left(180)
forward(360)
right(124)
forward(200)
right(90)
forward(300)
end_fill()
hideturtle()


Output:

Bhutan flag in turtle
2. Colorful Pentagon

Source code:

from turtle import *

hideturtle()
pensize(3)
color("yellow", "orange")
begin_fill()
for i in range(5):
    forward(100)
    left(72)
end_fill()

Output:

colorful pentagon
3. A Beautiful Rainbow

Question: Write a turtle program to draw a colorful rainbow.

Source code:

from turtle import *

hideturtle()
radius = 60 # Set initial radius to 60

# Declare colors of rainbow in list
rainbow_colors = ['violet','indigo','blue','green','yellow', 'orange','red']
pensize(20) 

for r in range(len(rainbow_colors)):
    color(rainbow_colors[r])  # Select first color from rainbow color list
    left(90) 
    circle(radius,180) # Draw half circle
    penup() 
    circle(radius,180) 
    right(90) 
    forward(20) 
    pendown() 
    radius += 20 # Increase radius with 20


Output:

Rainbow in turtle
4. Archery Target

Source code:

from turtle import *
hideturtle()
penup()
goto(-200,-200)
pensize(5)
pendown()

#drawing target background
x=0
while x<2:
    forward(300)
    left(90)
    forward(450)
    left(90)
    x+=1

#drawing target base
goto(-50,-350)
goto(100,-200)

#drawing bull's eye and other target circle
colors=['green','blue','red','yellow','purple']
for i in range(len(colors)):
    penup()
    goto(-50,-(i*10))
    pensize(10)
    pencolor(colors[i])
    pendown()
    circle(50+(10*i))

    #drawing lines at buttom of target
    if i%2==0:
        penup()
        goto(-195,-(160+7.5*i))
        pendown()
        pensize(15)
        forward(290)

Output:

target in turtle


5. Endless Knot or Pelbaeu (དཔལ་བེའུ)

Source code:

from turtle import *
bgcolor('green')
penup()
goto(175,0)
pendown()
pensize(10)
pencolor('blue')
right(45)
forward(75)
right(90)
forward(75)
right(90)
pencolor('yellow')
forward(300)
left(90)
forward(75)
left(90)
pencolor('blue')
forward(300)
right(90)
forward(75)
right(90)
pencolor('yellow')
forward(300)
left(90)
forward(75)
left(90)
forward(75)
left(90)
pencolor('blue')
forward(300)
right(90)
forward(75)
right(90)
pencolor('yellow')
forward(300)
left(90)
forward(75)
left(90)
pencolor('blue')
forward(300)
hideturtle()

Output:

endless knot in turtle