如何使用Python Turtle生成线性渐变?

2024-09-30 23:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我当前正在尝试复制此映像: https://imgur.com/a/IZIPGkg 我试着在背景中做梯度,但我没有任何线索如何做,互联网上基本上什么都没有。 编辑:如果有帮助的话,我有两端的RGB颜色。顶部为rgb(154,0254),底部为rgb(221122,80)


Tags: httpscom编辑颜色互联网rgb梯度背景
1条回答
网友
1楼 · 发布于 2024-09-30 23:29:21

原油,但速度快且有效:

from turtle import Screen, Turtle

COLOR = (0.60156, 0, 0.99218)  # (154, 0, 254)
TARGET = (0.86328, 0.47656, 0.31250)  # (221, 122, 80)

screen = Screen()
screen.tracer(False)

WIDTH, HEIGHT = screen.window_width(), screen.window_height()

deltas = [(hue - COLOR[index]) / HEIGHT for index, hue in enumerate(TARGET)]

turtle = Turtle()
turtle.color(COLOR)

turtle.penup()
turtle.goto(-WIDTH/2, HEIGHT/2)
turtle.pendown()

direction = 1

for distance, y in enumerate(range(HEIGHT//2, -HEIGHT//2, -1)):

    turtle.forward(WIDTH * direction)
    turtle.color([COLOR[i] + delta * distance for i, delta in enumerate(deltas)])
    turtle.sety(y)

    direction *= -1

screen.tracer(True)
screen.exitonclick()

enter image description here

相关问题 更多 >