You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a list of RGB tuples. I want to make it Oklab and make a linear gradient with it, then turn it into an RGB Pillow Image. Regular code for RGB looks like this. Please critique also, it's supposed to be as fast as it can be.
defcreate_linear_gradient(width, height, stops, direction):
# stops is a list { percent, (r, g, b) }# Normalize the direction vectordx, dy=directionmagnitude= (dx**2+dy**2) **0.5dx/=magnitudedy/=magnitude# create coordinate gridsx=np.linspace(0, 1, width)
y=np.linspace(0, 1, height)
xv, yv=np.meshgrid(x, y)
# Compute the distance map and center itdistance_map= (xv-0.5) *dx+ (yv-0.5) *dydistance_map=distance_map-distance_map.min()
distance_map=distance_map/distance_map.max() # Normalize to [0, 1]# Sort and interpolate stopsstops=sorted(stops, key=lambdastop: stop[1])
stop_colors, stop_positions=zip(*stops)
# Convert to arrays for interpolationstop_positions=np.array(stop_positions)
stop_colors=np.array(stop_colors, dtype=np.float32)
# Interpolate each color channelr_channel=np.interp(distance_map, stop_positions, stop_colors[:, 0])
g_channel=np.interp(distance_map, stop_positions, stop_colors[:, 1])
b_channel=np.interp(distance_map, stop_positions, stop_colors[:, 2])
gradient=np.stack([r_channel, g_channel, b_channel], axis=-1).astype(np.uint8)
returnImage.fromarray(gradient)
The text was updated successfully, but these errors were encountered:
Typically, if you want a perceptually linear gradient, you would construct it within Oklab, idea would be to have your input values as RGB, e.g., start and end RGB triplets, convert them to Oklab and linear interpolate the Oklab correlates.
Question
I have a list of RGB tuples. I want to make it Oklab and make a linear gradient with it, then turn it into an RGB Pillow Image. Regular code for RGB looks like this. Please critique also, it's supposed to be as fast as it can be.
The text was updated successfully, but these errors were encountered: