blob: 3286047a5fc8b087f963f543f2c1db5aab04b63c [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
Barry Warsaw698c1492001-07-10 21:45:27 +00008
9
Barry Warsawca07ba01998-10-22 03:25:59 +000010class Chooser:
11 """Ask for a color"""
12 def __init__(self,
13 master = None,
Barry Warsawca07ba01998-10-22 03:25:59 +000014 databasefile = None,
15 initfile = None,
Barry Warsawa20f6af1998-10-22 03:44:52 +000016 ignore = None,
17 wantspec = None):
Barry Warsawca07ba01998-10-22 03:25:59 +000018 self.__master = master
Barry Warsawca07ba01998-10-22 03:25:59 +000019 self.__databasefile = databasefile
20 self.__initfile = initfile or os.path.expanduser('~/.pynche')
21 self.__ignore = ignore
22 self.__pw = None
Barry Warsawa20f6af1998-10-22 03:44:52 +000023 self.__wantspec = wantspec
Barry Warsawca07ba01998-10-22 03:25:59 +000024
Barry Warsaw80d83e31999-04-27 18:56:35 +000025 def show(self, color, options):
26 # scan for options that can override the ctor options
27 self.__wantspec = options.get('wantspec', self.__wantspec)
28 dbfile = options.get('databasefile', self.__databasefile)
29 # load the database file
30 colordb = None
Georg Brandlbf82e372008-05-16 17:02:34 +000031 if dbfile != self.__databasefile:
Barry Warsaw80d83e31999-04-27 18:56:35 +000032 colordb = ColorDB.get_colordb(dbfile)
Barry Warsaw2be85821998-10-22 18:51:22 +000033 if not self.__master:
Georg Brandl91b47c62010-10-26 20:12:37 +000034 from tkinter import Tk
Barry Warsaw2be85821998-10-22 18:51:22 +000035 self.__master = Tk()
Barry Warsawca07ba01998-10-22 03:25:59 +000036 if not self.__pw:
37 self.__pw, self.__sb = \
38 Main.build(master = self.__master,
Barry Warsawca07ba01998-10-22 03:25:59 +000039 initfile = self.__initfile,
40 ignore = self.__ignore)
Barry Warsaw2be85821998-10-22 18:51:22 +000041 else:
42 self.__pw.deiconify()
43 # convert color
Barry Warsaw80d83e31999-04-27 18:56:35 +000044 if colordb:
45 self.__sb.set_colordb(colordb)
46 else:
47 colordb = self.__sb.colordb()
Barry Warsaw2be85821998-10-22 18:51:22 +000048 if color:
49 r, g, b = Main.initial_color(color, colordb)
50 self.__sb.update_views(r, g, b)
51 # reset the canceled flag and run it
52 self.__sb.canceled(0)
Barry Warsawca07ba01998-10-22 03:25:59 +000053 Main.run(self.__pw, self.__sb)
54 rgbtuple = self.__sb.current_rgb()
55 self.__pw.withdraw()
56 # check to see if the cancel button was pushed
57 if self.__sb.canceled_p():
58 return None, None
Barry Warsaw698c1492001-07-10 21:45:27 +000059 # Try to return the color name from the database if there is an exact
60 # match, otherwise use the "#rrggbb" spec. BAW: Forget about color
Barry Warsawca07ba01998-10-22 03:25:59 +000061 # aliases for now, maybe later we should return these too.
Barry Warsawa20f6af1998-10-22 03:44:52 +000062 name = None
63 if not self.__wantspec:
64 try:
65 name = colordb.find_byrgb(rgbtuple)[0]
66 except ColorDB.BadColor:
67 pass
68 if name is None:
Barry Warsawca07ba01998-10-22 03:25:59 +000069 name = ColorDB.triplet_to_rrggbb(rgbtuple)
70 return rgbtuple, name
71
Barry Warsaw2be85821998-10-22 18:51:22 +000072 def save(self):
73 if self.__sb:
74 self.__sb.save_views()
Barry Warsawca07ba01998-10-22 03:25:59 +000075
76
77# convenience stuff
Barry Warsaw2be85821998-10-22 18:51:22 +000078_chooser = None
79
Barry Warsawca07ba01998-10-22 03:25:59 +000080def askcolor(color = None, **options):
81 """Ask for a color"""
Barry Warsaw2be85821998-10-22 18:51:22 +000082 global _chooser
83 if not _chooser:
Neal Norwitzd9108552006-03-17 08:00:19 +000084 _chooser = Chooser(**options)
Barry Warsaw80d83e31999-04-27 18:56:35 +000085 return _chooser.show(color, options)
Barry Warsawca07ba01998-10-22 03:25:59 +000086
Barry Warsaw2be85821998-10-22 18:51:22 +000087def save():
88 global _chooser
89 if _chooser:
90 _chooser.save()
Barry Warsawca07ba01998-10-22 03:25:59 +000091
92
93# test stuff
94if __name__ == '__main__':
Georg Brandl91b47c62010-10-26 20:12:37 +000095 from tkinter import *
Barry Warsaw74a7ece2001-02-01 20:52:08 +000096
Barry Warsawca07ba01998-10-22 03:25:59 +000097 class Tester:
98 def __init__(self):
Barry Warsawca07ba01998-10-22 03:25:59 +000099 self.__root = tk = Tk()
100 b = Button(tk, text='Choose Color...', command=self.__choose)
101 b.pack()
102 self.__l = Label(tk)
103 self.__l.pack()
104 q = Button(tk, text='Quit', command=self.__quit)
105 q.pack()
106
107 def __choose(self, event=None):
108 rgb, name = askcolor(master=self.__root)
109 if rgb is None:
110 text = 'You hit CANCEL!'
111 else:
112 r, g, b = rgb
113 text = 'You picked %s (%3d/%3d/%3d)' % (name, r, g, b)
114 self.__l.configure(text=text)
115
116 def __quit(self, event=None):
117 self.__root.quit()
118
119 def run(self):
120 self.__root.mainloop()
121 t = Tester()
122 t.run()
123 # simpler
124## print 'color:', askcolor()
125## print 'color:', askcolor()