Reputation: 22827
I want to create a gradient of N distinct colors ranging from a dark color to a light color, all visually equidistant apart.
Similar like in this answer but for Python instead of R (can only use standard lib though, not eg. numpy).
I'd like a function where I can give as arguments the dark color, e.g. RGB(103,0,13)
, and the light color, e.g. RGB(255,245,240)
, and the number of shades. Similar like the output from ColorBrewer (but there I can't define the two end colors and I am limited to maximum 9 shades).
edit: What I tried so far: convert the RGB values to HLS using colorsys.rgb_to_hls(r, g, b)
and interpolating the Hue between the dark and the light colors.
Upvotes: 1
Views: 949
Reputation: 14098
Here's one way. Note, however, that the example gradient you posted above is a non-linear gradient of some kind. The function below generates linear gradients.
def generateColorGradient(RGB1, RGB2, n):
dRGB = [float(x2-x1)/(n-1) for x1, x2 in zip(RGB1, RGB2)]
gradient = [tuple([int(x+k*dx) for x, dx in zip(RGB1, dRGB)]) for k in range(n)]
return gradient
print generateColorGradient((103,0,13), (255,245,240), 9)
# OUTPUT
# [(103, 0, 13), (122, 30, 41), (141, 61, 69), (160, 91, 98), (179, 122, 126), (198, 153, 154), (217, 183, 183), (236, 214, 211), (255, 245, 240)]
Upvotes: 2