blob: 788ad5a0d65fc04848344c3d0f12d019b78bb5b2 [file] [log] [blame]
Barry Warsawca07ba01998-10-22 03:25:59 +00001"""Color chooser implementing (almost) the tkColorColor interface
2"""
3
4import os
Barry Warsawca07ba01998-10-22 03:25:59 +00005import Main
6import ColorDB
7
8class Chooser:
9 """Ask for a color"""
10 def __init__(self,
11 master = None,
Barry Warsawca07ba01998-10-22 03:25:59 +000012 databasefile = None,
13 initfile = None,
Barry Warsawa20f6af1998-10-22 03:44:52 +000014 ignore = None,
15 wantspec = None):
Barry Warsawca07ba01998-10-22 03:25:59 +000016 self.__master = master
Barry Warsawca07ba01998-10-22 03:25:59 +000017 self.__databasefile = databasefile
18 self.__initfile = initfile or os.path.expanduser('~/.pynche')
19 self.__ignore = ignore
20 self.__pw = None
Barry Warsawa20f6af1998-10-22 03:44:52 +000021 self.__wantspec = wantspec
Barry Warsawca07ba01998-10-22 03:25:59 +000022
Barry Warsaw80d83e31999-04-27 18:56:35 +000023 def show(self, color, options):
24 # scan for options that can override the ctor options
25 self.__wantspec = options.get('wantspec', self.__wantspec)
26 dbfile = options.get('databasefile', self.__databasefile)
27 # load the database file
28 colordb = None
29 if dbfile <> self.__databasefile:
30 colordb = ColorDB.get_colordb(dbfile)
Barry Warsaw2be85821998-10-22 18:51:22 +000031 if not self.__master:
32 from Tkinter import Tk
33 self.__master = Tk()
Barry Warsawca07ba01998-10-22 03:25:59 +000034 if not self.__pw:
35 self.__pw, self.__sb = \
36 Main.build(master = self.__master,
Barry Warsawca07ba01998-10-22 03:25:59 +000037 initfile = self.__initfile,
38 ignore = self.__ignore)
Barry Warsaw2be85821998-10-22 18:51:22 +000039 else:
40 self.__pw.deiconify()
41 # convert color
Barry Warsaw80d83e31999-04-27 18:56:35 +000042 if colordb:
43 self.__sb.set_colordb(colordb)
44 else:
45 colordb = self.__sb.colordb()
Barry Warsaw2be85821998-10-22 18:51:22 +000046 if color:
47 r, g, b = Main.initial_color(color, colordb)
48 self.__sb.update_views(r, g, b)
49 # reset the canceled flag and run it
50 self.__sb.canceled(0)
Barry Warsawca07ba01998-10-22 03:25:59 +000051 Main.run(self.__pw, self.__sb)
52 rgbtuple = self.__sb.current_rgb()
53 self.__pw.withdraw()
54 # check to see if the cancel button was pushed
55 if self.__sb.canceled_p():
56 return None, None
Barry Warsawca07ba01998-10-22 03:25:59 +000057 # try to return the color name from the database if there is an exact
58 # match, otherwise use the "#rrggbb" spec. TBD: Forget about color
59 # aliases for now, maybe later we should return these too.
Barry Warsawa20f6af1998-10-22 03:44:52 +000060 name = None
61 if not self.__wantspec:
62 try:
63 name = colordb.find_byrgb(rgbtuple)[0]
64 except ColorDB.BadColor:
65 pass
66 if name is None:
Barry Warsawca07ba01998-10-22 03:25:59 +000067 name = ColorDB.triplet_to_rrggbb(rgbtuple)
68 return rgbtuple, name
69
Barry Warsaw2be85821998-10-22 18:51:22 +000070 def save(self):
71 if self.__sb:
72 self.__sb.save_views()
Barry Warsawca07ba01998-10-22 03:25:59 +000073
74
75# convenience stuff
Barry Warsaw2be85821998-10-22 18:51:22 +000076_chooser = None
77
Barry Warsawca07ba01998-10-22 03:25:59 +000078def askcolor(color = None, **options):
79 """Ask for a color"""
Barry Warsaw2be85821998-10-22 18:51:22 +000080 global _chooser
81 if not _chooser:
82 _chooser = apply(Chooser, (), options)
Barry Warsaw80d83e31999-04-27 18:56:35 +000083 return _chooser.show(color, options)
Barry Warsawca07ba01998-10-22 03:25:59 +000084
Barry Warsaw2be85821998-10-22 18:51:22 +000085def save():
86 global _chooser
87 if _chooser:
88 _chooser.save()
Barry Warsawca07ba01998-10-22 03:25:59 +000089
90
91# test stuff
92if __name__ == '__main__':
93 class Tester:
94 def __init__(self):
95 from Tkinter import *
96 self.__root = tk = Tk()
97 b = Button(tk, text='Choose Color...', command=self.__choose)
98 b.pack()
99 self.__l = Label(tk)
100 self.__l.pack()
101 q = Button(tk, text='Quit', command=self.__quit)
102 q.pack()
103
104 def __choose(self, event=None):
105 rgb, name = askcolor(master=self.__root)
106 if rgb is None:
107 text = 'You hit CANCEL!'
108 else:
109 r, g, b = rgb
110 text = 'You picked %s (%3d/%3d/%3d)' % (name, r, g, b)
111 self.__l.configure(text=text)
112
113 def __quit(self, event=None):
114 self.__root.quit()
115
116 def run(self):
117 self.__root.mainloop()
118 t = Tester()
119 t.run()
120 # simpler
121## print 'color:', askcolor()
122## print 'color:', askcolor()