如何对颜色进行线性插值?
How to do linear interpolation with colors?
所以我有一个任务,我必须使用 turtle 复制下面的图像。我知道将笔设置为一种颜色的基本概念,然后画一条线,向下移动 1 个 y 坐标并重复。但我需要知道的是我如何知道每条线的颜色。我听说过线性插值,但我不知道那是什么意思。
assignment
好的,我可以回答我的问题了。我所做的是取 2 种颜色的 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, 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():
它有点简陋,但很管用
以下是您可以使用任何起始颜色和结束颜色执行此操作的方法,而无需 hard-code 任何操作。请注意,这是一个示例,显示了对每个颜色通道所做的操作;下面我将以更简洁的方式展示如何做到这一点。
# 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)
输出:
(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)
请注意,这在最终颜色之前停止,您可以使用最后一行的目标颜色,或者只是将范围增加到 number_of_rows + 1
。
这是上面的代码,但经过高度简化 - 它给出了相同的输出:
# 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)
所以我有一个任务,我必须使用 turtle 复制下面的图像。我知道将笔设置为一种颜色的基本概念,然后画一条线,向下移动 1 个 y 坐标并重复。但我需要知道的是我如何知道每条线的颜色。我听说过线性插值,但我不知道那是什么意思。
assignment
好的,我可以回答我的问题了。我所做的是取 2 种颜色的 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, 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():
它有点简陋,但很管用
以下是您可以使用任何起始颜色和结束颜色执行此操作的方法,而无需 hard-code 任何操作。请注意,这是一个示例,显示了对每个颜色通道所做的操作;下面我将以更简洁的方式展示如何做到这一点。
# 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)
输出:
(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)
请注意,这在最终颜色之前停止,您可以使用最后一行的目标颜色,或者只是将范围增加到 number_of_rows + 1
。
这是上面的代码,但经过高度简化 - 它给出了相同的输出:
# 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)