#!/usr/bin/env python #Convert color to name mapping from /usr/lib/X11/rgb.txt #to a HTML page #(C) 2004-2006 Thomas Guettler http://www.thomas-guettler.de #Feedback is Welcome! (Even hints to typos) #This script is in the public domain # Python Imports import os import re import cgi import sys rgbtxt="/usr/lib/X11/rgb.txt" def usage(): print """Usage: %s [path_to_X11/rgb.txt] outfile.html Convert color to name mapping from /usr/lib/X11/rgb.txt to a HTML page """ % (os.path.basename(sys.argv[0])) def main(): global rgbtxt if len(sys.argv)>3 or len(sys.argv)==1: usage() sys.exit(1) if len(sys.argv)==3: rgbtxt=sys.argv[1] outfile=sys.argv[2] else: outfile=sys.argv[1] if not os.path.isfile(rgbtxt): print "Cannot open %s" % rgbtxt usage() sys.exit(1) if outfile.endswith(".txt"): print "This is not a good file for the html output:", outfile usage() sys.exit(1) fd=open(rgbtxt) regex=re.compile(r'^(\d+)\s+(\d+)\s+(\d+)\s+(.*)$') colors={} head=1 headlines=[] for line in fd: match=regex.match(line) if not match: if head: headlines.append(line) continue head=0 r=int(match.group(1)) g=int(match.group(2)) b=int(match.group(3)) name=match.group(4).strip() if colors.has_key(name): print "Color %s is definied twice" % name continue colors[name]=(r, g, b) print "Found %s colors in %s" % ( len(colors), rgbtxt) fd.close() dec=[] for c, rgb in colors.items(): dec.append((c.lower(), c, rgb)) dec.sort() rows=[] for c_lower, color, (r, g, b) in dec: rows.append(''' %s %s %s %s #%02x%02x%02x   ''' % ( color, r, g, b, r, g, b, r, g, b )) rows=''.join(rows) headlines=''.join(headlines) html=''' Colors of %s
Colors of %s
%s
       
%s

Created by rgbtxt2html.py ''' % ( rgbtxt, rgbtxt, cgi.escape(headlines), rows) fd=open(outfile, "wt") fd.write(html) fd.close() print "Created %s" % outfile if __name__=="__main__": main()