img/buildicons.py
author Tony Garnock-Jones <tonyg@lshift.net>
Thu Jun 05 01:36:08 2008 +0100 (2008-06-05)
changeset 33 b0d0c11fb746
child 40 7f06f380d4db
permissions -rw-r--r--
Images for DVCS demo.
     1 #!/usr/bin/env python
     2 
     3 import cairo
     4 import math
     5 
     6 pi = math.pi
     7 
     8 WIDTH = 20
     9 MID = WIDTH / 2.0
    10 BLOBRADIUS = WIDTH / 4.0
    11 LINEWIDTH = WIDTH / 12.0
    12 
    13 surface = None
    14 ctx = None
    15 
    16 def reset(n = None):
    17     global surface, ctx
    18     if n and surface:
    19         surface.write_to_png(n)
    20     surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, WIDTH)
    21     ctx = cairo.Context(surface)
    22     ctx.set_line_width(LINEWIDTH)
    23 
    24 #     ctx.set_source_rgb(1,1,1)
    25 #     ctx.paint()
    26 #     ctx.set_source_rgb(0,0,0)
    27 
    28 def m(n):
    29     ctx.move_to(MID, MID)
    30     if n & 1: ctx.line_to(MID, 0); ctx.stroke()
    31     ctx.move_to(MID, MID)
    32     if n & 2: ctx.line_to(0, MID); ctx.stroke()
    33     ctx.move_to(MID, MID)
    34     if n & 4: ctx.line_to(MID, WIDTH); ctx.stroke()
    35     ctx.move_to(MID, MID)
    36     if n & 8: ctx.line_to(WIDTH, MID); ctx.stroke()
    37 
    38     ctx.arc(MID, MID, BLOBRADIUS, 0, pi * 2)
    39     ctx.fill()
    40     ctx.set_source_rgb(1,1,1)
    41     ctx.arc(MID, MID, BLOBRADIUS - LINEWIDTH, 0, pi * 2)
    42     ctx.fill()
    43 
    44     reset("blob_%d.png" % n)
    45 
    46 def k(n):
    47     if n & 1:
    48         ctx.move_to(MID, 0)
    49         if n & 2:
    50             ctx.line_to(MID, MID - BLOBRADIUS - LINEWIDTH / 4)
    51             ctx.stroke()
    52             ctx.move_to(MID, MID - LINEWIDTH / 4)
    53         ctx.line_to(MID, WIDTH)
    54         ctx.stroke()
    55     if n & 2:
    56         ctx.move_to(0, MID)
    57         if n & 1:
    58             ctx.line_to(MID - BLOBRADIUS + LINEWIDTH, MID)
    59             ctx.curve_to(MID - BLOBRADIUS + LINEWIDTH, MID - BLOBRADIUS,
    60                          MID + BLOBRADIUS - LINEWIDTH, MID - BLOBRADIUS,
    61                          MID + BLOBRADIUS - LINEWIDTH, MID)
    62         ctx.line_to(WIDTH, MID)
    63         ctx.stroke()
    64     if n & 4:
    65         ctx.arc(0, WIDTH, WIDTH/2, pi / 2, 0)
    66         ctx.stroke()
    67     if n & 8:
    68         ctx.arc(WIDTH, WIDTH, WIDTH/2, pi, 3 * pi / 2)
    69         ctx.stroke()
    70     reset("stick_%d.png" % n)
    71 
    72 reset()
    73 for n in range(16):
    74     m(n)
    75     k(n)
    76 
    77 def topleft():
    78     ctx.move_to(MID, 0)
    79     ctx.line_to(0, MID)
    80     ctx.stroke()
    81 def bottomright():
    82     ctx.move_to(WIDTH, MID)
    83     ctx.line_to(MID, WIDTH)
    84     ctx.stroke()
    85 bottomright()
    86 reset("slide_l.png")
    87 topleft()
    88 bottomright()
    89 reset("slide_m.png")
    90 topleft()
    91 reset("slide_r.png")