#!/usr/bin/env python __author__ = "Hans Meine " import fig, sys, re, os.path from optparse import OptionParser op = OptionParser( usage = "%prog ", description= """Creates a dimmed / grayed-out version of an XFig file (e.g. for use with PGF/Beamer). The background color needs to be specified in the usual hex notation (e.g. #ffffff for white), and opacity is the opacity in percentage as specified in the colormixin-environment (e.g. 15 in the beamer themes I used). In LaTeX, you can use \colorcurrentmixin to get the values matching the effect applied to the surrounding text. The input filename may be '-' for stdin, and if no output filename is given, the output will be sent to stdout.""") op.add_option("-o", "--output", action="store", dest="filename", help="Output filename (default: output to stdout)") options, args = op.parse_args() if len(args) != 3: sys.stderr.write("ERROR: Wrong number or arguments.\n\n") op.print_help(sys.stderr) sys.exit(1) filename = args[0] if filename == "-": filename = sys.stdin # constructor parameter for fig.File backgroundColor = fig.CustomColor(None, args[1]) alpha = float(args[2])/100 def blend(rgb): def blendChannel(fg, bg): return int(round(alpha*fg + (1-alpha)*bg)) return map(blendChannel, rgb, backgroundColor) figFile = fig.File(filename) for c in figFile.colors: c.setRGB(*blend(c)) standardMapping = [] for c in fig.standardColors: standardMapping.append(None) standardMapping.append(-1) # map fig.colorDefault to itself def mapColor(c): if c >= fig.colorCustom0: return c result = standardMapping[c] if result == None: result = figFile.getColor(blend(fig.standardColors[c])) standardMapping[c] = result return result for o in figFile.allObjects(): o.penColor = mapColor(o.penColor) o.fillColor = mapColor(o.fillColor) if options.filename: figFile.save(options.filename) else: sys.stdout.write(str(figFile))