[2] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | usage: %prog width height bg_color hatch_color [color alpha stop_pos] + |
---|
| 5 | """ |
---|
| 6 | |
---|
| 7 | from __future__ import division |
---|
| 8 | |
---|
| 9 | import sys |
---|
| 10 | import cairo |
---|
| 11 | from math import pi |
---|
| 12 | |
---|
| 13 | assert sys.version_info[:2] >= ( 2, 4 ) |
---|
| 14 | |
---|
| 15 | def parse_css_color( color ): |
---|
| 16 | if color.startswith( '#' ): |
---|
| 17 | color = color[1:] |
---|
| 18 | if len( color ) == 3: |
---|
| 19 | r = int( color[0], 16 ) |
---|
| 20 | g = int( color[1], 16 ) |
---|
| 21 | b = int( color[2], 16 ) |
---|
| 22 | elif len( color ) == 6: |
---|
| 23 | r = int( color[0:2], 16 ) |
---|
| 24 | g = int( color[2:4], 16 ) |
---|
| 25 | b = int( color[4:6], 16 ) |
---|
| 26 | else: |
---|
| 27 | raise Exception( "Color should be 3 hex numbers" ) |
---|
| 28 | return r/256, g/256, b/256 |
---|
| 29 | |
---|
| 30 | size = int( sys.argv[1] ) |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, size, size ) |
---|
| 34 | c = cairo.Context( surface ) |
---|
| 35 | |
---|
| 36 | c.set_line_width( 1 ) |
---|
| 37 | |
---|
| 38 | c.arc( size / 2.0, size / 2.0, ( size - 1 ) / 2.0, 0, 2 * pi ) |
---|
| 39 | |
---|
| 40 | c.set_source_rgb( *parse_css_color( sys.argv[2] ) ) |
---|
| 41 | c.fill_preserve() |
---|
| 42 | |
---|
| 43 | c.set_source_rgb( *parse_css_color( sys.argv[3] ) ) |
---|
| 44 | c.stroke() |
---|
| 45 | |
---|
| 46 | t = size / 4.0 |
---|
| 47 | |
---|
| 48 | arrow = sys.argv[4] |
---|
| 49 | if arrow == 'right': |
---|
| 50 | c.move_to( t + 1, t ) |
---|
| 51 | c.line_to( 3*t - 1, 2*t ) |
---|
| 52 | c.line_to( t + 1, 3*t ) |
---|
| 53 | c.stroke() |
---|
| 54 | |
---|
| 55 | surface.write_to_png( "/dev/stdout" ) |
---|