I am looking for a way to color objects based on the colors of nearby objects, and from what I've read it looks like linear interpolation is the thing to do.
I have looked through a lot (all?) of the posts on SO about linear interpolation for color in Python but haven't been able to make any of it work with my code.
I want to try and keep this as simple as possible for now. If I had a gradient that went from purple to white and two objects, how could I tell python: if object 1 is purple, make object 2 a lighter purple ?
Looking at this post, a function lerp is defined by:
def lerp(color1, color2, frac):
return color1 * (1 - frac) + color2 * frac
But I don't understand how to apply it. This post has a ton of suggestions, but it doesn't seem resolved, although it does contain a lot of interesting information about additive and subtractive coloring methods. I understand coloring is complex, and I don't want to make a redundant post, so I am hoping to really emphasize that I am looking for the most simple, rudimentary method to do something very simple, like make an object a lighter shade of purple if it is next to an object that is a darker purple.
For this example, I've created 3 polygons and colored them all magenta:
import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString
# functions for plotting
def plot_coords(coords, color):
pts = list(coords)
x, y = zip(*pts)
# print(color)
plt.plot(x,y, color='k', linewidth=1)
plt.fill_between(x, y, facecolor=color)
def plot_polys(polys, color):
for poly, color in zip(polys, color):
plot_coords(poly.exterior.coords, color)
x = 0
y = 0
# make some points
points = [Point(x, y),
Point((x + 2), y),
Point((x - 2), y)]
#buffer points by 2 to make circles
circles = []
for point in points:
circles.append(point.buffer(2))
# plot 3 magenta circles
plot_polys(circles, "m"*3)
How could I use the above lerp function (or a different one) to color my circles on a gradient?
Note: I know I could use a pre-made palette from matplotlib, but my longer term goal is to understand the arithmetic that is controlling the color changes.
Thanks for your help!