Skip to content

Instantly share code, notes, and snippets.

@dreness
Created April 3, 2014 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreness/9946163 to your computer and use it in GitHub Desktop.
Save dreness/9946163 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import colorsys
def ColorSpread(count, saturation, lightness):
'''
Compute a stepped spectrum sequence of color values that are evenly
distributed around the 360 degree hue wheel. Returns an array of rgb
values in hex.
Arguments:
count The desired number of colors
saturation Color saturation level, from 0 to 1
lightness Color lightness, from 0 to 1
IMPORTANT: all values must be expressed as floats, even if they are
round numbers
EXAMPLES:
>>> a = ColorSpread(6.0, .7, .8)
>>> a
['F07575', 'F0F075', '75F075', '75F0F0', '7575F0', 'F075F0']
>>> for x in a:
... print "#%s" % (x)
...
#F07575
#F0F075
#75F075
#75F0F0
#7575F0
#F075F0
'''
step = 1.0 / count
hue = 0
colors = []
for i in range(int(count)):
dec = colorsys.hls_to_rgb(hue, saturation, lightness)
hue = hue + step
hexvals = []
for c in dec:
x = c * 256
hexvals.append("%X" % x)
color = "%s%s%s" % (hexvals[0], hexvals[1], hexvals[2])
colors.append(color)
return colors
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment