citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

mass_swatch.py (1653B)


      1 from __future__ import with_statement
      2 import re
      3 import sys
      4 
      5 
      6 def hex2dec(s):
      7     return int(s, 16)
      8 
      9 def render_file(filename, items):
     10     out_filename = filename.split('.')[0] + '.htm'
     11     out_file = open(out_filename, "w")
     12 
     13     out_file.write("""
     14         <html>
     15         <head>
     16         <style>
     17             .colorblock
     18             {
     19                 width: 24px;
     20                 height: 24px;
     21                 border: 1px solid #000;
     22                 padding: 5px;
     23                 margin-right: 5px;
     24             }
     25 
     26             td
     27             {
     28                 width: 400px;
     29                 padding: 10px;
     30             }
     31         </style>
     32         </head>
     33         <body>
     34         <table>""");
     35 
     36     for item in items:
     37 
     38         column = []
     39         column.append(item[0])
     40 
     41         out_file.write('<tr><td>' + item[0] + '</td><td>')
     42 
     43         for color in item[1]:
     44             out_file.write('<span class="colorblock" style="background-color: ' + color[0] + '">' + str(color[1]) + '</span>')
     45 
     46         out_file.write('</td></tr>')
     47 
     48     out_file.write("</table></body></html>")
     49 
     50 if __name__ == "__main__":
     51     
     52     filename = sys.argv[1]
     53 
     54     reg = r'#[a-zA-Z0-9]{3,6}'
     55     items = []
     56 
     57     with open(filename) as file:
     58         for line in file:
     59             colors = re.findall(reg, line)
     60             
     61             rgb = []
     62             combined = []
     63 
     64             for color in colors:
     65                 rgb = (hex2dec(color[1:3]), hex2dec(color[3:5]), hex2dec(color[5:7]))
     66                 combined.append( (color, rgb) )
     67             
     68             if len(colors) == 0:
     69                 continue
     70 
     71             items.append( (line, combined) )
     72 
     73     render_file(filename, items)
     74 
     75