PageRenderTime 33ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/static/june_2007_style/callout_top.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 55 lines | 36 code | 15 blank | 4 comment | 4 complexity | d0058600ce22f43f4fa21c3a7a3839b8 MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. usage: %prog width height bg_color hatch_color [color alpha stop_pos] +
  4. """
  5. from __future__ import division
  6. import sys
  7. import cairo
  8. assert sys.version_info[:2] >= ( 2, 4 )
  9. def parse_css_color( color ):
  10. if color.startswith( '#' ):
  11. color = color[1:]
  12. if len( color ) == 3:
  13. r = int( color[0]*2, 16 )
  14. g = int( color[1]*2, 16 )
  15. b = int( color[2]*2, 16 )
  16. elif len( color ) == 6:
  17. r = int( color[0:2], 16 )
  18. g = int( color[2:4], 16 )
  19. b = int( color[4:6], 16 )
  20. else:
  21. raise Exception( "Color should be 3 hex numbers" )
  22. return r/256, g/256, b/256
  23. width = int( sys.argv[1] )
  24. height = int( sys.argv[2] )
  25. surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, width, height )
  26. c = cairo.Context( surface )
  27. height -= 1
  28. width -= 1
  29. hw = width / 2
  30. c.set_line_width( 1 )
  31. def t( x ): return x + 0.5
  32. c.move_to( t( 0 ), t( height+2 ) )
  33. c.line_to( t( hw ), t( 0 ) )
  34. c.line_to( t( width ), t( height+2 ) )
  35. c.close_path()
  36. c.set_source_rgb( *parse_css_color( sys.argv[3] ) )
  37. c.fill_preserve()
  38. c.set_source_rgb( *parse_css_color( sys.argv[4] ) )
  39. c.stroke()
  40. surface.write_to_png( "/dev/stdout" )