Skip to content

Instantly share code, notes, and snippets.

@irees
Created March 2, 2016 19:35
Show Gist options
  • Save irees/f70babd3f6f9580d67bc to your computer and use it in GitHub Desktop.
Save irees/f70babd3f6f9580d67bc to your computer and use it in GitHub Desktop.
import json
import urllib2
import colorsys
import cooperhewitt.swatchbook.crayola
palette = cooperhewitt.swatchbook.crayola.palette()
url = "http://transit.land/api/v1/routes.json?vehicle_type=metro&per_page=1000"
###
def hex_to_rgb(value):
value = value[0:2], value[2:4], value[4:6]
return [int(i, 16)/255.0 for i in value]
### Process
data = json.load(urllib2.urlopen(url))
# print "Found %s routes"%(len(data['routes']))
route_colors = [str(i['tags'].get('route_color')).lower() for i in data['routes']]
route_colors_uniq = set(route_colors)
route_colors_uniq.remove('none')
colors = dict(
map(lambda x:[x,colorsys.rgb_to_hsv(*hex_to_rgb(x))], route_colors_uniq)
)
### Output
print """<html><body><table cellpadding="2" cellspacing="0">"""
print """<thead><tr><th>Hex color</th><th>Count</th><th>Crayola</th></tr></thead>"""
for h, rgb in sorted(colors.items(), key=lambda x:x[1]):
print """
<tr>
<td style="background-color:#%s">%s</td>
<td>%s</td>
<td>%s</td>
</tr>
"""%(
h,
h,
route_colors.count(h),
palette.closest('#%s'%h)[1]
)
print """</table></body></html>"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment