Skip to content

Instantly share code, notes, and snippets.

@rcarmo
Last active July 30, 2018 17:40
Show Gist options
  • Save rcarmo/e0a85f88ed40e180fdfce49557bbd2dd to your computer and use it in GitHub Desktop.
Save rcarmo/e0a85f88ed40e180fdfce49557bbd2dd to your computer and use it in GitHub Desktop.
Elegant Python RGB/RGBA multi-step linear gradients with generators
#!/usr/bin/env python3
from itertools import tee
# colors are floating-point 3-element tuples (doing RGBA is a matter of setting the component count)
def poly_gradient(colors, steps, components=3):
def linear_gradient(start, finish, substeps):
yield start
for i in range(1, substeps):
yield tuple([(start[j]+(float(i)/(substeps-1))*(finish[j]-start[j])) for j in range(components)])
def pairs(seq):
a, b = tee(seq)
next(b, None)
return zip(a, b)
substeps = int(float(steps)/(len(colors)-1))
for a, b in pairs(colors):
for c in linear_gradient(a, b, substeps):
yield c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment