如何使用Python Turtle进行线性渐变? [英] How do I make a linear gradient with Python Turtle?
本文介绍了如何使用Python Turtle进行线性渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试复制此图像:
I'm currently trying to replicate this image: https://imgur.com/a/IZIPGkg I'm trying to make that gradient in the background but I have zero clue how to do it and there's basically nothing on the internet. Edit: I have the RGB colors for both ends if that helps. The top is rgb(154,0,254) and the bottom is rgb(221,122,80).
解决方案
Crude but resonably quick and effective:
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()
这篇关于如何使用Python Turtle进行线性渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文