Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7f4a47

Browse files
committed
Better initial color choosing
1 parent 45c8d34 commit e7f4a47

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

Tools/pynche/Main.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ def usage(status, msg=''):
6161
sys.exit(status)
6262

6363

64+
65+
def initial_color(s, colordb):
66+
# function called on every color
67+
def scan_color(s, colordb=colordb):
68+
try:
69+
r, g, b = colordb.find_byname(s)
70+
except ColorDB.BadColor:
71+
try:
72+
r, g, b = ColorDB.rrggbb_to_triplet(s)
73+
except ColorDB.BadColor:
74+
return None, None, None
75+
return r, g, b
76+
#
77+
# First try the passed in color
78+
r, g, b = scan_color(s)
79+
if r is None:
80+
# try the same color with '#' prepended, since some shells require
81+
# this to be escaped, which is a pain
82+
r, g, b = scan_color('#' + s)
83+
if r is None:
84+
print 'Bad initial color, using gray50:', s
85+
r, g, b = scan_color('gray50')
86+
if r is None:
87+
usage(1, 'Cannot find an initial color to use')
88+
# does not return
89+
return r, g, b
90+
91+
6492

6593
def main():
6694
try:
@@ -95,19 +123,8 @@ def main():
95123
else:
96124
usage(1, 'No color database file found, see the -d option.')
97125

98-
# get triplet for initial color
99-
try:
100-
red, green, blue = colordb.find_byname(initialcolor)
101-
except ColorDB.BadColor:
102-
# must be a #rrggbb style color
103-
try:
104-
red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
105-
except ColorDB.BadColor:
106-
try:
107-
red, green, blue = ColorDB.rrggbb_to_triplet('#7f7f7f')
108-
print 'Bad initial color, using gray50:', initialcolor
109-
except ColorDB.BadColor:
110-
usage(1, 'Cannot find an initial color to use')
126+
# get the initial color as components
127+
red, green, blue = initial_color(initialcolor, colordb)
111128

112129
# create all output widgets
113130
s = Switchboard(colordb)

Tools/pynche/PyncheWidget.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self, version, switchboard):
4040
#
4141
filemenu = self.__filemenu = Menu(menubar, tearoff=0)
4242
filemenu.add_command(label='Quit',
43-
command=tkroot.quit,
43+
command=self.__quit,
4444
accelerator='Alt-Q',
4545
underline=0)
4646
#
@@ -78,12 +78,15 @@ def __init__(self, version, switchboard):
7878

7979
# now create the top level window
8080
root = self.__root = Toplevel(tkroot, class_='Pynche', menu=menubar)
81-
root.protocol('WM_DELETE_WINDOW', tkroot.quit)
81+
root.protocol('WM_DELETE_WINDOW', self.__quit)
8282
root.title('Pynche %s' % version)
8383
root.iconname('Pynche')
8484
root.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
85-
root.bind('<Alt-q>', tkroot.quit)
86-
root.bind('<Alt-Q>', tkroot.quit)
85+
root.bind('<Alt-q>', self.__quit)
86+
root.bind('<Alt-Q>', self.__quit)
87+
88+
def __quit(self, event=None):
89+
self.__root.quit()
8790

8891
def __keepalive(self):
8992
# Exercise the Python interpreter regularly so keyboard interrupts get

0 commit comments

Comments
 (0)