How do I make a linear gradient with Python Turtle?

2024/11/18 21:27:26

I'm currently trying to replicate this image: https://i.sstatic.net/fymWE.jpg 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).

Answer

Crude but resonably quick and effective:

from turtle import Screen, TurtleCOLOR = (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 = 1for 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 *= -1screen.tracer(True)
screen.exitonclick()

enter image description here

https://en.xdnf.cn/q/118627.html

Related Q&A

Python - Converting an array to a list causes values to change

>>> import numpy as np >>> a=np.arange(0,2,0.2) >>> a array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8]) >>> a=a.tolist() >>> a [0.0, 0.2, …

Understand Python Function [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to download linkedin (save as pdf option) using python

Image what i want to download.Image is of LinkedIn profile page of my friend i want to click on that save-as-pdf option for many users.can that be downloaded using python code? for different users? o…

My tkinter entry box is printing .!entry instead of what is entered

from tkinter import * def _name_():businessname=entry_bnprint(businessname) edit_bar=Tk() name=Label(edit_bar,text="Name:").grid(row=0) entry_bn=Entry(edit_bar) entry_bn.grid(row=0,column=1) …

How to get an average from a row then make a list out of it [duplicate]

This question already has answers here:Reading a CSV file, calculating averages and printing said averages(2 answers)Closed 6 years ago.If I have a csv data that gives two row values of:years grades 20…

Beautiful soup: Extract everything between two tags when these tags have different ids

Beautiful soup: Extract everything between two tags I have seen a question through the above link where we are getting the information between two tags. Whereas I need to get the information between th…

exceptions.RuntimeError - Object has no attribute errno [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How can I translate this python function to c++?

I am trying to translate a python function to c++ without success. Can someone help me? The python function receives as input a string S and 2 integers (fragment_size and jump). The aim of this functi…

Reverse PDF imposition

I have an imposed document: there are 4 n A4 pages on the n sheets. I put them into a roller image scanner and receive one 2 n paged PDF document (A3).If, say, n = 3, then Ive got the following seque…

Python: How to run flask mysqldb on Windows machine?

Ive installed the flask-mysqldb module with pip package management system on my Windows machine and I dont know how to run it.I have tried to add the path to the MySQLdb in System properties and still …