简体   繁体   中英

Is there a way to have rgb colors in Python3.8?

I was trying to specify the RGB colors to make the program generate random colors. I was trying how to do RGB colors however, it would show an error that it had bad color input, more specifically, "raise TurtleGraphicsError("bad color sequence: %s" % str(color)) turtle.TurtleGraphicsError: bad color sequence: (0, 255, 0)"

from random import randint

myPen = turtle.Turtle()
myPen.ht()
myPen.speed(0)
myPen.pencolor(0,255,0)
myPen.begin_fill()
points = [[-500,-400],[0,500],[500,-400]] #size of triangle
def getMid(p1,p2):
    return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2) #find midpoint

def triangle(points,depth):

    myPen.up()
    myPen.goto(points[0][0],points[0][1])
    myPen.down()
    myPen.goto(points[1][0],points[1][1])
    myPen.goto(points[2][0],points[2][1])
    myPen.goto(points[0][0],points[0][1])

    if depth>0:
        z=(randint(0,255),randint(0,255),randint(0,255))
        myPen.fillcolor(z)
        triangle([points[0],
                        getMid(points[0], points[1]),
                        getMid(points[0], points[2])],
                   depth-1)
        triangle([points[1],
                        getMid(points[0], points[1]),
                        getMid(points[1], points[2])],
                   depth-1)
        triangle([points[2],
                         getMid(points[2], points[1]),
                         getMid(points[0], points[2])],
                   depth-1)



triangle(points,7)

I have found that you need a window (or whatever variable) =turtle.Screen() . Then you can do window.colormode(255) Then you can change the color scheme

Your question, and self answer, imply something that isn't true. Python turtle is RGB by default. What it isn't is integer color values. The default is RGB floating values:

from turtle import Screen
from random import random

color = random(), random(), random()

screen = Screen()
screen.bgcolor(color)
screen.exitonclick()

Using colormode() , you're switching between floating and integer RGB values.

You can also switch to other color models, like HSB, via loading additional Python libraries and converting those color models into RGB for turtle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM