Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created October 1, 2012 20:52
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 nolanlawson/3814330 to your computer and use it in GitHub Desktop.
Save nolanlawson/3814330 to your computer and use it in GitHub Desktop.
generate some random colors for CatLog
#!/bin/sh/env python
#
# Quick script to generate eye-pleasing colors based on a single Saturation and Value (Brightness).
# Print out in HTML hex format
#
# usage: generate_random_colors.py numColors saturation value multiMode htmlMode
#
import sys, colorsys, random
try:
num_colors = int(sys.argv[1])
saturation = float(sys.argv[2])
brightness = float(sys.argv[3])
multiMode = int(sys.argv[4]) == 1
htmlMode = int(sys.argv[5]) == 1
except:
sys.exit("usage: generate_random_colors.py numColors saturation value multiMode htmlMode")
PHI = 1.61803398875;
bgColor = 'aaaaaa';
if (htmlMode):
pre_text = '<span whatever=\'%s\'><font color=\'#'
post_text = '\'><br/>FOOBAR</font></span>'
print '<body bgcolor="#%s">' % (bgColor)
else:
pre_text = '<color name="chart_line_%s">#FF'
post_text = '</color>'
hue = random.random();
def toHex(num):
return (hex(int(round(255 * num)))[2:]).zfill(2)
for j in range(1000):
if (multiMode):
saturation = float(random.random())
brightness = float(random.random())
print "<p/><span>saturation: %g, brightness: %g</span><br/>" % (saturation,brightness)
for i in range(num_colors):
rgb = colorsys.hsv_to_rgb(hue,saturation,brightness)
print (pre_text + '%s%s%s' + post_text) % tuple([str(i + 1).zfill(2)] + map(toHex,rgb))
# use the golden mean to get a nice distribution of colors
hue = (hue + (1.0 / PHI)) % 1
if (not multiMode):
break
if (htmlMode):
print '</body>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment