2

So I have an assignment where I have to replicate the image below using turtle. I know the basic concept of setting the pen to a color, then making a line, go 1 y coordinate down and repeat. But what I need to know is how do I know what color to make each line. I've heard about linear interpolation but I have no idea what that means.

assignment

KauDar123
  • 33
  • 3
  • 1
    Show what you've tried so far so others can have a starting point when helping you. – Ionut Ticus Sep 23 '20 at 18:44
  • I see you've asked this question before on another account (or someone with the same homework assignment did): https://stackoverflow.com/questions/63999474/how-do-i-make-a-linear-gradient-with-python-turtle What about the answer didn't make sense to you? – Random Davis Sep 23 '20 at 18:57

2 Answers2

2

Here's how you can do it with any starting and ending colors, without having to hard-code anything. Note that this is an example showing the operations done to each color channel; below I'll show how to do it in a more succinct way.

# the starting color
initial_color = (0.60156, 0, 0.99218)  # (154, 0, 254)

# the final, target color
target_color = (0.86328, 0.47656, 0.31250)  # (221, 122, 80)

number_of_rows=10  # how many rows we're painting

# get the total difference between each color channel
red_difference=target_color[0]-initial_color[0]
green_difference=target_color[1]-initial_color[1]
blue_difference=target_color[2]-initial_color[2]

# divide the difference by the number of rows, so each color changes by this amount per row
red_delta = red_difference/number_of_rows
green_delta = green_difference/number_of_rows
blue_delta = blue_difference/number_of_rows

# display the color for each row
for i in range(0, number_of_rows):
    # apply the delta to the red, green and blue channels
    interpolated_color=(initial_color[0] + (red_delta * i), 
                        initial_color[1] + (green_delta * i),
                        initial_color[2] + (blue_delta * i))
    print(interpolated_color)

Output:

(0.60156, 0.0, 0.99218)
(0.627732, 0.047656, 0.9242119999999999)
(0.653904, 0.095312, 0.856244)
(0.680076, 0.14296799999999998, 0.788276)
(0.706248, 0.190624, 0.720308)
(0.7324200000000001, 0.23828, 0.6523399999999999)
(0.758592, 0.28593599999999997, 0.5843719999999999)
(0.784764, 0.333592, 0.516404)
(0.8109360000000001, 0.381248, 0.44843599999999995)
(0.8371080000000001, 0.42890399999999995, 0.3804679999999999)

Note that this stops before the final color, you can either use the target color for your last row, or just increase the range to number_of_rows + 1.

Here's the above code but highly simplified - it gives the same output:

# simpler version - we can skip the diffs and just get the deltas, and store all 3 colors in a list
deltas=[(target_color[i] - initial_color[i])/number_of_rows for i in range(3)]
for j in range(0, number_of_rows):
    interpolated_color=tuple([initial_color[i] + (deltas[i] * j) for i in range(3)])
    print(interpolated_color)
Random Davis
  • 6,662
  • 4
  • 14
  • 24
0

alright so i was able to answer my question. What I did was take the difference between the 2 color's rgb code and then divide that by the amount I wanted the gradient to last, then added that to the rgb one line at a time.

import turtle
jeff = turtle.Turtle()
jeff.speed('fastest')
def gradient():
 jeff.penup()
 jeff.goto(-450, 200)
 jeff.pendown()
 r = 154.0
 g = 0.0
 b = 254.0
 for i in range(125):
  jeff.pencolor(r, g, b)
  jeff.fd(900)
  jeff.penup()
  jeff.seth(270)
  jeff.fd(1)
  jeff.seth(180)
  jeff.pendown()
  r += 0.268
  g += 0.488
  b -= 0.696
  jeff.pencolor(r, g, b)
  jeff.fd(900)
  jeff.penup()
  jeff.seth(270)
  jeff.fd(1)
  jeff.seth(0)
  jeff.pendown()
  r += 0.268
  g += 0.488
  b -= 0.696
gradient():

It's a bit rudimentary, but it works

KauDar123
  • 33
  • 3