What does bad color sequence mean in Python turtle?
Asked Answered
C

5

26

I am using python turtle for a project where I need turtle to draw characters. However, when I try to use the RGB value for a color, I keep getting an error message. The input is:

turtle.color((151,2,1))

followed by a series of movements. However, when I run the program I get this message:

File "C:/Users/Larry/Desktop/tests.py", line 5, in center
turtle.color((151,2,1))
File "<string>", line 1, in color
File "C:\Python33\lib\turtle.py", line 2208, in color
pcolor = self._colorstr(pcolor)
File "C:\Python33\lib\turtle.py", line 2688, in _colorstr
return self.screen._colorstr(args)
File "C:\Python33\lib\turtle.py", line 1158, in _colorstr
raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (151, 2, 1)

What does this mean, and how can I fix it?

Citizenship answered 27/5, 2013 at 17:58 Comment(1)
Run screen.colormode(255) and it should work.Teece
A
28

From the docs:

Each of r, g, and b must be in the range 0..colormode, where colormode is either 1.0 or 255 (see colormode()).

Your colormode is probably set to 1.0, so either the individual color coordinates need to be floats in the range 0 to 1, or you need to set the colormode to 255.

Annoy answered 27/5, 2013 at 18:4 Comment(0)
S
7

A very short and simplified answer is, it means the value passed to the pencolor() method has not been previously set via the Screen object method colormode().

A screen object must be created. And then, the color mode must be set. Thus, making it possible for the turtle pen to accept a tuple class object which contains a number ranging from 0 - 255. (255, 0, 20) for example. Why? Because there is more than one way of setting the color mode.

e.g.

from turtle import Turtle
from turtle import Screen

# Creating a turtle object
bert = Turtle()

# Creating the screen object
screen = Screen()

# Setting the screen color-mode
screen.colormode(255)

# Changing the color of the pen the turtle carries
bert.pencolor(255, 0, 0)

# 'Screen object loop to prevent the window from closing without command'
screen.exitonclick()
Superintend answered 19/12, 2020 at 1:30 Comment(0)
A
1

you get this error in case of like you are generating a random color using a function and you are using the random module and then returning a tuple of 3 random integers, and pass this tuple color function you can fix this issue by writing this turtle.colormode(255)

use this code

import random
import turtle

from turtle import Turtle
turtle.colormode(255)
tim = Turtle()
tim.speed("fastest")
direction = [90,  270]
#colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
tim.pensize(5)

def random_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    random_color = (r, g, b)
    return random_color


while True:
    move = random.choice(direction)
    tim.forward(40)
    tim.color(random_color())
    tim.left(move)
Avi answered 19/12, 2021 at 17:42 Comment(0)
S
-1

Try to import colormode function

from turtle import Turtle
from turtle import colormode
from turtle import Screen

tim = Turtle()
screen = Screen()
colormode(255)

tim.pencolor(151,2,1)
screen.exitonclick()
Silverware answered 24/9, 2021 at 15:1 Comment(1)
This doesn't add anything to existing answersCittern
A
-1

you will get this error in case you are generating a random color using a function and you are using the random module and then returning a tuple of 3 random integers, and pass this tuple color function you can fix this issue by writing this turtle.colormode(255)

use this code

import random
import turtle

from turtle import Turtle
turtle.colormode(255)
tim = Turtle()
tim.speed("fastest")
direction = [90,  270]
#colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
tim.pensize(5)

def random_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    random_color = (r, g, b)
    return random_color


while True:
    move = random.choice(direction)
    tim.forward(40)
    tim.color(random_color())
    tim.left(move)
Avi answered 19/12, 2021 at 17:46 Comment(1)
Posted twice and doesn't really add anything other answers don't already discuss--please try to avoid adding noise to the thread. Thanks.Cittern

© 2022 - 2024 — McMap. All rights reserved.