【问题标题】:Is there a way to have rgb colors in Python3.8?有没有办法在 Python3.8 中使用 rgb 颜色?
【发布时间】:2020-07-24 13:12:20
【问题描述】:

我试图指定 RGB 颜色以使程序生成随机颜色。我正在尝试如何处理 RGB 颜色,但是它会显示一个错误,即颜色输入错误,更具体地说,“raise TurtleGraphicsError("bad color sequence: %s" % str(color)) turtle.TurtleGraphicsError: 错误的颜色序列: (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)

【问题讨论】:

标签: python python-3.x python-turtle


【解决方案1】:

我发现您需要一个window(或任何变量)=turtle.Screen()。然后你可以做window.colormode(255) 然后你可以改变配色方案

【讨论】:

    【解决方案2】:

    您的问题和自己的回答暗示了一些不正确的事情。 Python 乌龟默认为 RGB。它不是 integer 颜色值。默认为 RGB 浮动 值:

    from turtle import Screen
    from random import random
    
    color = random(), random(), random()
    
    screen = Screen()
    screen.bgcolor(color)
    screen.exitonclick()
    

    使用colormode(),您可以在浮点和整数 RGB 值之间切换。

    您还可以通过加载额外的 Python 库并将这些颜色模型转换为龟的 RGB 来切换到其他颜色模型,例如 HSB。

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多