home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / MAIN.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  6.2 KB  |  223 lines

  1. """Pynche -- The PYthon Natural Color and Hue Editor.
  2.  
  3. Contact: Barry Warsaw
  4. Email:   bwarsaw@python.org
  5. Version: %(__version__)s
  6.  
  7. Pynche is based largely on a similar color editor I wrote years ago for the
  8. Sunview window system.  That editor was called ICE: the Interactive Color
  9. Editor.  I'd always wanted to port the editor to X but didn't feel like
  10. hacking X and C code to do it.  Fast forward many years, to where Python +
  11. Tkinter provides such a nice programming environment, with enough power, that
  12. I finally buckled down and implemented it.  I changed the name because these
  13. days, too many other systems have the acronym `ICE'.
  14.  
  15. This program currently requires Python 1.5 with Tkinter.  It has only been
  16. tested on Solaris 2.6.  Feedback is greatly appreciated.  Send email to
  17. bwarsaw@python.org
  18.  
  19. Usage: %(PROGRAM)s [-d file] [-i file] [-X] [-v] [-h] [initialcolor]
  20.  
  21. Where:
  22.     --database file
  23.     -d file
  24.         Alternate location of a color database file
  25.  
  26.     --initfile file
  27.     -i file
  28.         Alternate location of the initialization file.  This file contains a
  29.         persistent database of the current Pynche options and color.  This
  30.         means that Pynche restores its option settings and current color when
  31.         it restarts, using this file (unless the -X option is used).  The
  32.         default is ~/.pynche
  33.  
  34.     --ignore
  35.     -X
  36.         Ignore the initialization file when starting up.  Pynche will still
  37.         write the current option settings to this file when it quits.
  38.  
  39.     --version
  40.     -v
  41.         print the version number
  42.  
  43.     --help
  44.     -h
  45.         print this message
  46.  
  47.     initialcolor
  48.         initial color, as a color name or #RRGGBB format
  49.  
  50. """
  51.  
  52. __version__ = '1.0'
  53.  
  54. import sys
  55. import os
  56. import string
  57. import getopt
  58. import ColorDB
  59. from PyncheWidget import PyncheWidget
  60. from Switchboard import Switchboard
  61. from StripViewer import StripViewer
  62. from ChipViewer import ChipViewer
  63. from TypeinViewer import TypeinViewer
  64.  
  65.  
  66.  
  67. PROGRAM = sys.argv[0]
  68.  
  69. # Default locations of rgb.txt or other textual color database
  70. RGB_TXT = [
  71.     # Solaris OpenWindows
  72.     '/usr/openwin/lib/rgb.txt',
  73.     # Linux
  74.     '/usr/lib/X11/rgb.txt',
  75.     # The X11R6.4 rgb.txt file
  76.     os.path.join(sys.path[0], 'X/rgb.txt'),
  77.     # add more here
  78.     ]
  79.  
  80.  
  81.  
  82. def docstring():
  83.     return string.rstrip(__doc__ % globals())
  84.  
  85.  
  86. def usage(status, msg=''):
  87.     print docstring()
  88.     if msg:
  89.         print msg
  90.     sys.exit(status)
  91.  
  92.  
  93.  
  94. def initial_color(s, colordb):
  95.     # function called on every color
  96.     def scan_color(s, colordb=colordb):
  97.         try:
  98.             r, g, b = colordb.find_byname(s)
  99.         except ColorDB.BadColor:
  100.             try:
  101.                 r, g, b = ColorDB.rrggbb_to_triplet(s)
  102.             except ColorDB.BadColor:
  103.                 return None, None, None
  104.         return r, g, b
  105.     #
  106.     # First try the passed in color
  107.     r, g, b = scan_color(s)
  108.     if r is None:
  109.         # try the same color with '#' prepended, since some shells require
  110.         # this to be escaped, which is a pain
  111.         r, g, b = scan_color('#' + s)
  112.     if r is None:
  113.         print 'Bad initial color, using gray50:', s
  114.         r, g, b = scan_color('gray50')
  115.     if r is None:
  116.         usage(1, 'Cannot find an initial color to use')
  117.         # does not return
  118.     return r, g, b
  119.  
  120.  
  121.  
  122. def build(master=None, initialcolor=None, initfile=None, ignore=None):
  123.     # create all output widgets
  124.     s = Switchboard(not ignore and initfile)
  125.  
  126.     # load the color database
  127.     colordb = None
  128.     try:
  129.         dbfile = s.optiondb()['DBFILE']
  130.         colordb = ColorDB.get_colordb(dbfile)
  131.     except (KeyError, IOError):
  132.         # scoot through the files listed above to try to find a usable color
  133.         # database file
  134.         for f in RGB_TXT:
  135.             try:
  136.                 colordb = ColorDB.get_colordb(f)
  137.                 if colordb:
  138.                     break
  139.             except IOError:
  140.                 pass
  141.     if not colordb:
  142.         usage(1, 'No color database file found, see the -d option.')
  143.     s.set_colordb(colordb)
  144.  
  145.     # create the application window decorations
  146.     app = PyncheWidget(__version__, s, master=master)
  147.     w = app.window()
  148.  
  149.     # these built-in viewers live inside the main Pynche window
  150.     s.add_view(StripViewer(s, w))
  151.     s.add_view(ChipViewer(s, w))
  152.     s.add_view(TypeinViewer(s, w))
  153.  
  154.     # get the initial color as components and set the color on all views.  if
  155.     # there was no initial color given on the command line, use the one that's 
  156.     # stored in the option database
  157.     if initialcolor is None:
  158.         optiondb = s.optiondb()
  159.         red = optiondb.get('RED')
  160.         green = optiondb.get('GREEN')
  161.         blue = optiondb.get('BLUE')
  162.         # but if there wasn't any stored in the database, use grey50
  163.         if red is None or blue is None or green is None:
  164.             red, green, blue = initial_color('grey50', colordb)
  165.     else:
  166.         red, green, blue = initial_color(initialcolor, colordb)
  167.     s.update_views(red, green, blue)
  168.     return app, s
  169.  
  170.  
  171. def run(app, s):
  172.     try:
  173.     app.start()
  174.     except KeyboardInterrupt:
  175.     pass
  176.  
  177.  
  178.  
  179. def main():
  180.     try:
  181.     opts, args = getopt.getopt(
  182.             sys.argv[1:],
  183.             'hd:i:Xv',
  184.             ['database=', 'initfile=', 'ignore', 'help', 'version'])
  185.     except getopt.error, msg:
  186.     usage(1, msg)
  187.  
  188.     if len(args) == 0:
  189.         initialcolor = None
  190.     elif len(args) == 1:
  191.         initialcolor = args[0]
  192.     else:
  193.     usage(1)
  194.  
  195.     ignore = 0
  196.     initfile = os.path.expanduser('~/.pynche')
  197.     for opt, arg in opts:
  198.     if opt in ('-h', '--help'):
  199.         usage(0)
  200.         elif opt in ('-v', '--version'):
  201.             print '''\
  202. Pynche -- The PYthon Natural Color and Hue Editor.
  203. Contact: Barry Warsaw
  204. Email:   bwarsaw@python.org
  205. Version: %s''' % __version__
  206.             sys.exit(0)
  207.     elif opt in ('-d', '--database'):
  208.         RGB_TXT.insert(0, arg)
  209.         elif opt in ('-X', '--ignore'):
  210.             ignore = 1
  211.         elif opt in ('-i', '--initfile'):
  212.             initfile = arg
  213.  
  214.     app, sb = build(initialcolor=initialcolor,
  215.                     initfile=initfile,
  216.                     ignore=ignore)
  217.     run(app, sb)
  218.     sb.save_views()
  219.  
  220.  
  221. if __name__ == '__main__':
  222.     main()
  223.