如何用颜色进行线性插值?

问题描述

所以我有一个任务,我必须使用乌龟复制以下图像。我知道将笔设置为颜色,然后画一条线,向下移动1 y坐标并重复的基本概念。但是我需要知道的是如何知道每条线是什么颜色。我听说过线性插值,但是我不知道这意味着什么。

assignment

解决方法

好的,所以我能够回答我的问题。我所做的是将两种颜色的rgb代码之间的差值除以我希望渐变能够持续的数量,然后一次将其添加到rgb中一行。

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,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():

有点基本,但是可以用

,

这是您可以使用任何开始和结束颜色进行操作的方法,而无需对任何内容进行硬编码。注意,这是显示对每个颜色通道进行的操作的示例;下面,我将展示如何更简洁地做到这一点。

res = [[dict([tuple(d.values())]) for d in lst]for lst in data]
print(res)

输出:

[[{'text1.txt': 'class1'}],[{'text2.txt': 'class2'}]]

请注意,此操作将在最终颜色之前停止,您可以在最后一行中使用目标颜色,也可以将范围扩大到# the starting color initial_color = (0.60156,0.99218) # (154,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)

这是上面的代码,但经过高度简化-它提供了相同的输出:

(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)