How do i change colors in python?

How do i change colors in python?

Drawing shapes and learning how to move the pen in Python Turtle is fun, but it gets much more interesting when you add some color to your turtle drawings. In Python Turtle, you can do a lot with colors. To get started with colors, the first thing you can do is change the color of the pen in Turtle. You can set the pen to be any color you like or change it at any time in your program. In this tutorial, we’ll experiment with the color() and width() functions to spice up our turtle drawings.


color()

Let’s revisit the very first program we wrote with Python Turtle where we simply made the pen move using the forward() function.

How do i change colors in python?

Now let’s make that line a different color using the color() function.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

color('blue')
forward(75)

done()

How do i change colors in python?

You can make the turtle any color you like. Let’s change the turtle shape to a square, and make the color green now.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
color('green')
forward(75)

done()

How do i change colors in python?


width()

When you change the pen color in Python Turtle, the effect is more pronounced when making the lines thicker. This can be done with the width() function like so.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(10)
color('blue')
forward(75)

done()

How do i change colors in python?

You can pass the color() function two arguments and it will behave as if you are calling pencolor(arg1) and fillcolor(arg2). The following code shows this in action.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(10)
color('blue', 'red')
forward(75)

done() 

How do i change colors in python?

We can make use of the color() function multiple times in the same program. This technique is used here to draw a square that has a different color for each side of the square.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(10)
color('red')
left(90)
forward(150)
color('green')
left(90)
forward(150)
color('yellow')
left(90)
forward(150)
color('blue')
left(90)
forward(150)

done()

How do i change colors in python?


Random Colors

Let’s make use of a list of colors in combination with the random.choice() Python function to create some interesting drawings that use all the colors of the rainbow in a random fashion. Consider this abstract Python art if you will.

from turtle import *
import random

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(2)
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

for i in range(75):
    color(random.choice(colors))
    right(20 + i)
    forward(1 + (i * 5))
    right(40 + i)

done()

How do i change colors in python?


Using color() In Functions

One exercise we did earlier was to draw shapes using functions. We can update those functions to make use of color. That way we can draw a shape, and make it any color we like by passing the color to it when the function is called. The code below updates the function to draw a square, but not it accepts a parameter to specify the color you would like the square to be.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(2)


def draw_square(linecolor):
    color(linecolor)
    for i in range(4):
        forward(100)
        right(90)


right(-90)
penup()
forward(50)
right(-90)
forward(200)
pendown()
draw_square('red')

right(180)
penup()
forward(100)
right(-90)
pendown()
draw_square('green')

right(90)
penup()
forward(200)
right(-90)
pendown()
draw_square('blue')

done()

How do i change colors in python?


More Colors!

from turtle import *
import random

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(2)
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']


def draw_triangle(length=150):
    color(random.choice(colors))
    for i in range(3):
        forward(length)
        left(120)


for i in range(40):
    draw_triangle()
    right(10)

done()

How do i change colors in python?


from turtle import *
import random

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(2)
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']


def draw_triangle(length=150):
    color(random.choice(colors))
    for i in range(3):
        forward(length)
        left(120)


for i in range(20):
    draw_triangle()
    right(2)

done()

How do i change colors in python?


Thick Color Triangles

For the last example of using a colored pen in Python turtle, we’ll rewrite the draw_triangle() function so that it accepts a parameter to specify the color. The width of the line will also be thicker for a neat effect.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
width(20)


def draw_triangle(linecolor, length=150):
    color(linecolor)
    for i in range(3):
        forward(length)
        left(120)


draw_triangle('red')
right(180)
forward(100)
right(180)
draw_triangle('green', 200)
right(180)
forward(100)
right(180)
draw_triangle('blue', 250)

done()  

How do i change colors in python?

Is there a color function in Python?

Wand color() function in Python color() function draws a color on the image using current fill color, starting at specified position & method. Uses same arguments as color() method.

How do I change print color in python?

Example Code: from colorama import Fore from colorama import Style print(f"This is {Fore. GREEN}color{Style. RESET_ALL}!")

How do you color a number in Python?

Python Color Constants Module.
Contains constants for 551 named colors* (e.g, as named tuples: Color = namedtuple('RGB','red, green, blue').
Extends the Color class to include a method for getting the hex formatted color: class RGB(Color): def hex_format(self): return '#{:02X}{:02X}{:02X}'..