Today we’re going to talk about two of the most exciting features in our beloved language: fillcolor and color. These functions are like the cherry on top of your sundae they add a pop of color to your code and make it look oh-so-pretty!
First things first, how to use these functions in Python. To set the fillcolor or pencolor (which is just another way of saying “line color”), you can call either `turtle.fillcolor()` or `turtle.pencolor()`. These functions take a variety of input formats, so let’s break them down:
1. No arguments this will return the current fill/line color as a string or tuple (depending on whether you called `fillcolor()` or `pencolor()`, respectively). For example:
# Import the turtle module
import turtle
# Create a turtle object
t = turtle.Turtle()
# Set the fill color to violet
t.fillcolor("violet")
# Set the pen color to a tuple of RGB values
t.pencolor((50, 193, 143))
# Use the fillcolor() function to return the current fill color as a string
t.fillcolor()
# Use the pencolor() function to return the current pen color as a tuple
t.pencolor()
# Print the results
print(t.fillcolor()) # Prints 'violet'
print(t.pencolor()) # Prints (50, 193, 143)
2. One argument this will set both the fill and line color to the given value. The input can be a string or tuple in either case:
# This script sets the fill and line color of a turtle object to the given value.
# Import the turtle module
import turtle
# Create a turtle object
t = turtle.Turtle()
# Set the fill color to red
t.fillcolor("red")
# Set the line color to a tuple of RGB values
t.pencolor((10, 50, 25))
# The fillcolor() method sets the color used for filling in shapes drawn by the turtle object.
# The pencolor() method sets the color used for drawing lines by the turtle object.
# Both methods take in a string or tuple as an argument, representing the desired color.
# In this case, the fill color is set to red and the line color is set to a dark green color represented by the RGB values (10, 50, 25).
3. Three arguments this will set both the fill and line color to an RGB value (i.e., a tuple of red, green, and blue values). Each argument must be in the range 0..colormode, where colormode is either 1.0 or 255:
# Set the fill color of the turtle to an RGB value of (0.3, 0.8, 0.6)
turtle.fillcolor(0.3, 0.8, 0.6) # The fillcolor() function takes in three arguments, representing the red, green, and blue values of the desired color. These values must be in the range of 0 to colormode, which is either 1.0 or 255.
# Set the pen color of the turtle to an RGB value of (74, 193, 12)
turtle.pencolor((74, 193, 12)) # The pencolor() function also takes in three arguments, but in this case, the arguments are passed in as a tuple. This function sets the color of the turtle's pen, which is used for drawing lines and shapes.
That’s it! You can use these functions to create beautiful and colorful graphics in Python. And if you want to get really fancy, you can even combine them with other features like `colormode()` (which lets you switch between RGB and CMYK modes) or `turtle.speed(0)` (which makes your turtle move faster).
But wait there’s more! Did you know that Python also has a built-in function called `color()`? This function can take up to three arguments, just like the fill and line color functions we talked about earlier. But instead of setting both colors at once, it lets you set either the fill or line color (or both) depending on how many arguments you provide:
# This script demonstrates the usage of the built-in function 'color()' in the turtle module.
# First, we import the turtle module
import turtle
# The 'color()' function can take up to three arguments, just like the fill and line color functions we talked about earlier.
# The first argument is for the fill color, the second is for the line color, and the third is for the line thickness.
# If only one argument is provided, it sets the fill color to that value and the line color remains unchanged.
# If two arguments are provided, it sets the fill color to the first value and the line color to the second value.
# If three arguments are provided, it sets the fill color to the first value, the line color to the second value, and the line thickness to the third value.
# Here, we set the fill color to "green" by providing only one argument.
turtle.color("green")
# Next, we set the fill color to a specific RGB value by providing a tuple of three values.
# In this case, the RGB value is (0, 128, 0), which corresponds to a dark shade of green.
turtle.color((0, 128, 0))
# Finally, we set the fill color to a specific RGB value by providing three separate arguments.
# In this case, the RGB value is (51, 204, 140), which corresponds to a light shade of green.
turtle.color(51, 204, 140)
And that’s it! With these functions and a little creativity, you can create all sorts of amazing graphics in Python. So go ahead let your inner artist loose and start experimenting with fillcolors and colors today!