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 | |
---|
12 | assert sys.version_info[:2] >= ( 2, 4 ) |
---|
13 | |
---|
14 | def parse_css_color( color ): |
---|
15 | if color.startswith( '#' ): |
---|
16 | color = color[1:] |
---|
17 | if len( color ) == 3: |
---|
18 | r = int( color[0]*2, 16 ) |
---|
19 | g = int( color[1]*2, 16 ) |
---|
20 | b = int( color[2]*2, 16 ) |
---|
21 | elif len( color ) == 6: |
---|
22 | r = int( color[0:2], 16 ) |
---|
23 | g = int( color[2:4], 16 ) |
---|
24 | b = int( color[4:6], 16 ) |
---|
25 | else: |
---|
26 | raise Exception( "Color should be 3 hex numbers" ) |
---|
27 | return r/256, g/256, b/256 |
---|
28 | |
---|
29 | width = int( sys.argv[1] ) |
---|
30 | height = int( sys.argv[2] ) |
---|
31 | |
---|
32 | surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, width, height ) |
---|
33 | c = cairo.Context( surface ) |
---|
34 | |
---|
35 | height -= 1 |
---|
36 | width -= 1 |
---|
37 | |
---|
38 | hw = width / 2 |
---|
39 | |
---|
40 | c.set_line_width( 1 ) |
---|
41 | |
---|
42 | def t( x ): return x + 0.5 |
---|
43 | |
---|
44 | c.move_to( t( 0 ), t( height+2 ) ) |
---|
45 | c.line_to( t( hw ), t( 0 ) ) |
---|
46 | c.line_to( t( width ), t( height+2 ) ) |
---|
47 | c.close_path() |
---|
48 | |
---|
49 | c.set_source_rgb( *parse_css_color( sys.argv[3] ) ) |
---|
50 | c.fill_preserve() |
---|
51 | |
---|
52 | c.set_source_rgb( *parse_css_color( sys.argv[4] ) ) |
---|
53 | c.stroke() |
---|
54 | |
---|
55 | surface.write_to_png( "/dev/stdout" ) |
---|