img/buildicons.py
author Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
Mon Dec 21 23:52:38 2009 +0000 (2009-12-21)
changeset 123 bc4c90cbd778
parent 33 b0d0c11fb746
permissions -rw-r--r--
For OS X
     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 ENABLE_CROSSOVER = False
    14 
    15 surface = None
    16 ctx = None
    17 
    18 def reset(n = None):
    19     global surface, ctx
    20     if n and surface:
    21         surface.write_to_png(n)
    22     surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, WIDTH)
    23     ctx = cairo.Context(surface)
    24     ctx.set_line_width(LINEWIDTH)
    25 
    26 #     ctx.set_source_rgb(1,1,1)
    27 #     ctx.paint()
    28 #     ctx.set_source_rgb(0,0,0)
    29 
    30 def m(n):
    31     ctx.move_to(MID, MID)
    32     if n & 1: ctx.line_to(MID, 0); ctx.stroke()
    33     ctx.move_to(MID, MID)
    34     if n & 2: ctx.line_to(0, MID); ctx.stroke()
    35     ctx.move_to(MID, MID)
    36     if n & 4: ctx.line_to(MID, WIDTH); ctx.stroke()
    37     ctx.move_to(MID, MID)
    38     if n & 8: ctx.line_to(WIDTH, MID); ctx.stroke()
    39 
    40     ctx.arc(MID, MID, BLOBRADIUS, 0, pi * 2)
    41     ctx.fill()
    42     ctx.set_source_rgb(1,1,1)
    43     ctx.arc(MID, MID, BLOBRADIUS - LINEWIDTH, 0, pi * 2)
    44     ctx.fill()
    45 
    46     reset("blob_%d.png" % n)
    47 
    48 def k(n):
    49     if n & 1:
    50         ctx.move_to(MID, 0)
    51         if ENABLE_CROSSOVER and n & 2:
    52             ctx.line_to(MID, MID - BLOBRADIUS - LINEWIDTH / 4)
    53             ctx.stroke()
    54             ctx.move_to(MID, MID - LINEWIDTH / 4)
    55         ctx.line_to(MID, WIDTH)
    56         ctx.stroke()
    57     if n & 2:
    58         ctx.move_to(0, MID)
    59         if ENABLE_CROSSOVER and n & 1:
    60             ctx.line_to(MID - BLOBRADIUS + LINEWIDTH, MID)
    61             ctx.curve_to(MID - BLOBRADIUS + LINEWIDTH, MID - BLOBRADIUS,
    62                          MID + BLOBRADIUS - LINEWIDTH, MID - BLOBRADIUS,
    63                          MID + BLOBRADIUS - LINEWIDTH, MID)
    64         ctx.line_to(WIDTH, MID)
    65         ctx.stroke()
    66     if n & 4:
    67         ctx.arc(0, WIDTH, WIDTH/2, pi / 2, 0)
    68         ctx.stroke()
    69     if n & 8:
    70         ctx.arc(WIDTH, WIDTH, WIDTH/2, pi, 3 * pi / 2)
    71         ctx.stroke()
    72     reset("stick_%d.png" % n)
    73 
    74 reset()
    75 for n in range(16):
    76     m(n)
    77     k(n)
    78 
    79 def topleft():
    80     ctx.move_to(MID, 0)
    81     ctx.line_to(0, MID)
    82     ctx.stroke()
    83 def bottomright():
    84     ctx.move_to(WIDTH, MID)
    85     ctx.line_to(MID, WIDTH)
    86     ctx.stroke()
    87 bottomright()
    88 reset("slide_l.png")
    89 topleft()
    90 bottomright()
    91 reset("slide_m.png")
    92 topleft()
    93 reset("slide_r.png")