/Tools/pynche/StripViewer.py

http://unladen-swallow.googlecode.com/ · Python · 433 lines · 277 code · 56 blank · 100 comment · 18 complexity · b08cd9dfdf05d79e3484d5ed568c54fb MD5 · raw file

  1. """Strip viewer and related widgets.
  2. The classes in this file implement the StripViewer shown in the top two thirds
  3. of the main Pynche window. It consists of three StripWidgets which display
  4. the variations in red, green, and blue respectively of the currently selected
  5. r/g/b color value.
  6. Each StripWidget shows the color variations that are reachable by varying an
  7. axis of the currently selected color. So for example, if the color is
  8. (R,G,B)=(127,163,196)
  9. then the Red variations show colors from (0,163,196) to (255,163,196), the
  10. Green variations show colors from (127,0,196) to (127,255,196), and the Blue
  11. variations show colors from (127,163,0) to (127,163,255).
  12. The selected color is always visible in all three StripWidgets, and in fact
  13. each StripWidget highlights the selected color, and has an arrow pointing to
  14. the selected chip, which includes the value along that particular axis.
  15. Clicking on any chip in any StripWidget selects that color, and updates all
  16. arrows and other windows. By toggling on Update while dragging, Pynche will
  17. select the color under the cursor while you drag it, but be forewarned that
  18. this can be slow.
  19. """
  20. from Tkinter import *
  21. import ColorDB
  22. # Load this script into the Tcl interpreter and call it in
  23. # StripWidget.set_color(). This is about as fast as it can be with the
  24. # current _tkinter.c interface, which doesn't support Tcl Objects.
  25. TCLPROC = '''\
  26. proc setcolor {canv colors} {
  27. set i 1
  28. foreach c $colors {
  29. $canv itemconfigure $i -fill $c -outline $c
  30. incr i
  31. }
  32. }
  33. '''
  34. # Tcl event types
  35. BTNDOWN = 4
  36. BTNUP = 5
  37. BTNDRAG = 6
  38. SPACE = ' '
  39. def constant(numchips):
  40. step = 255.0 / (numchips - 1)
  41. start = 0.0
  42. seq = []
  43. while numchips > 0:
  44. seq.append(int(start))
  45. start = start + step
  46. numchips = numchips - 1
  47. return seq
  48. # red variations, green+blue = cyan constant
  49. def constant_red_generator(numchips, red, green, blue):
  50. seq = constant(numchips)
  51. return map(None, [red] * numchips, seq, seq)
  52. # green variations, red+blue = magenta constant
  53. def constant_green_generator(numchips, red, green, blue):
  54. seq = constant(numchips)
  55. return map(None, seq, [green] * numchips, seq)
  56. # blue variations, red+green = yellow constant
  57. def constant_blue_generator(numchips, red, green, blue):
  58. seq = constant(numchips)
  59. return map(None, seq, seq, [blue] * numchips)
  60. # red variations, green+blue = cyan constant
  61. def constant_cyan_generator(numchips, red, green, blue):
  62. seq = constant(numchips)
  63. return map(None, seq, [green] * numchips, [blue] * numchips)
  64. # green variations, red+blue = magenta constant
  65. def constant_magenta_generator(numchips, red, green, blue):
  66. seq = constant(numchips)
  67. return map(None, [red] * numchips, seq, [blue] * numchips)
  68. # blue variations, red+green = yellow constant
  69. def constant_yellow_generator(numchips, red, green, blue):
  70. seq = constant(numchips)
  71. return map(None, [red] * numchips, [green] * numchips, seq)
  72. class LeftArrow:
  73. _ARROWWIDTH = 30
  74. _ARROWHEIGHT = 15
  75. _YOFFSET = 13
  76. _TEXTYOFFSET = 1
  77. _TAG = ('leftarrow',)
  78. def __init__(self, canvas, x):
  79. self._canvas = canvas
  80. self.__arrow, self.__text = self._create(x)
  81. self.move_to(x)
  82. def _create(self, x):
  83. arrow = self._canvas.create_line(
  84. x, self._ARROWHEIGHT + self._YOFFSET,
  85. x, self._YOFFSET,
  86. x + self._ARROWWIDTH, self._YOFFSET,
  87. arrow='first',
  88. width=3.0,
  89. tags=self._TAG)
  90. text = self._canvas.create_text(
  91. x + self._ARROWWIDTH + 13,
  92. self._ARROWHEIGHT - self._TEXTYOFFSET,
  93. tags=self._TAG,
  94. text='128')
  95. return arrow, text
  96. def _x(self):
  97. coords = self._canvas.coords(self._TAG)
  98. assert coords
  99. return coords[0]
  100. def move_to(self, x):
  101. deltax = x - self._x()
  102. self._canvas.move(self._TAG, deltax, 0)
  103. def set_text(self, text):
  104. self._canvas.itemconfigure(self.__text, text=text)
  105. class RightArrow(LeftArrow):
  106. _TAG = ('rightarrow',)
  107. def _create(self, x):
  108. arrow = self._canvas.create_line(
  109. x, self._YOFFSET,
  110. x + self._ARROWWIDTH, self._YOFFSET,
  111. x + self._ARROWWIDTH, self._ARROWHEIGHT + self._YOFFSET,
  112. arrow='last',
  113. width=3.0,
  114. tags=self._TAG)
  115. text = self._canvas.create_text(
  116. x - self._ARROWWIDTH + 15, # BAW: kludge
  117. self._ARROWHEIGHT - self._TEXTYOFFSET,
  118. justify=RIGHT,
  119. text='128',
  120. tags=self._TAG)
  121. return arrow, text
  122. def _x(self):
  123. coords = self._canvas.coords(self._TAG)
  124. assert coords
  125. return coords[0] + self._ARROWWIDTH
  126. class StripWidget:
  127. _CHIPHEIGHT = 50
  128. _CHIPWIDTH = 10
  129. _NUMCHIPS = 40
  130. def __init__(self, switchboard,
  131. master = None,
  132. chipwidth = _CHIPWIDTH,
  133. chipheight = _CHIPHEIGHT,
  134. numchips = _NUMCHIPS,
  135. generator = None,
  136. axis = None,
  137. label = '',
  138. uwdvar = None,
  139. hexvar = None):
  140. # instance variables
  141. self.__generator = generator
  142. self.__axis = axis
  143. self.__numchips = numchips
  144. assert self.__axis in (0, 1, 2)
  145. self.__uwd = uwdvar
  146. self.__hexp = hexvar
  147. # the last chip selected
  148. self.__lastchip = None
  149. self.__sb = switchboard
  150. canvaswidth = numchips * (chipwidth + 1)
  151. canvasheight = chipheight + 43 # BAW: Kludge
  152. # create the canvas and pack it
  153. canvas = self.__canvas = Canvas(master,
  154. width=canvaswidth,
  155. height=canvasheight,
  156. ## borderwidth=2,
  157. ## relief=GROOVE
  158. )
  159. canvas.pack()
  160. canvas.bind('<ButtonPress-1>', self.__select_chip)
  161. canvas.bind('<ButtonRelease-1>', self.__select_chip)
  162. canvas.bind('<B1-Motion>', self.__select_chip)
  163. # Load a proc into the Tcl interpreter. This is used in the
  164. # set_color() method to speed up setting the chip colors.
  165. canvas.tk.eval(TCLPROC)
  166. # create the color strip
  167. chips = self.__chips = []
  168. x = 1
  169. y = 30
  170. tags = ('chip',)
  171. for c in range(self.__numchips):
  172. color = 'grey'
  173. canvas.create_rectangle(
  174. x, y, x+chipwidth, y+chipheight,
  175. fill=color, outline=color,
  176. tags=tags)
  177. x = x + chipwidth + 1 # for outline
  178. chips.append(color)
  179. # create the strip label
  180. self.__label = canvas.create_text(
  181. 3, y + chipheight + 8,
  182. text=label,
  183. anchor=W)
  184. # create the arrow and text item
  185. chipx = self.__arrow_x(0)
  186. self.__leftarrow = LeftArrow(canvas, chipx)
  187. chipx = self.__arrow_x(len(chips) - 1)
  188. self.__rightarrow = RightArrow(canvas, chipx)
  189. def __arrow_x(self, chipnum):
  190. coords = self.__canvas.coords(chipnum+1)
  191. assert coords
  192. x0, y0, x1, y1 = coords
  193. return (x1 + x0) / 2.0
  194. # Invoked when one of the chips is clicked. This should just tell the
  195. # switchboard to set the color on all the output components
  196. def __select_chip(self, event=None):
  197. x = event.x
  198. y = event.y
  199. canvas = self.__canvas
  200. chip = canvas.find_overlapping(x, y, x, y)
  201. if chip and (1 <= chip[0] <= self.__numchips):
  202. color = self.__chips[chip[0]-1]
  203. red, green, blue = ColorDB.rrggbb_to_triplet(color)
  204. etype = int(event.type)
  205. if (etype == BTNUP or self.__uwd.get()):
  206. # update everyone
  207. self.__sb.update_views(red, green, blue)
  208. else:
  209. # just track the arrows
  210. self.__trackarrow(chip[0], (red, green, blue))
  211. def __trackarrow(self, chip, rgbtuple):
  212. # invert the last chip
  213. if self.__lastchip is not None:
  214. color = self.__canvas.itemcget(self.__lastchip, 'fill')
  215. self.__canvas.itemconfigure(self.__lastchip, outline=color)
  216. self.__lastchip = chip
  217. # get the arrow's text
  218. coloraxis = rgbtuple[self.__axis]
  219. if self.__hexp.get():
  220. # hex
  221. text = hex(coloraxis)
  222. else:
  223. # decimal
  224. text = repr(coloraxis)
  225. # move the arrow, and set its text
  226. if coloraxis <= 128:
  227. # use the left arrow
  228. self.__leftarrow.set_text(text)
  229. self.__leftarrow.move_to(self.__arrow_x(chip-1))
  230. self.__rightarrow.move_to(-100)
  231. else:
  232. # use the right arrow
  233. self.__rightarrow.set_text(text)
  234. self.__rightarrow.move_to(self.__arrow_x(chip-1))
  235. self.__leftarrow.move_to(-100)
  236. # and set the chip's outline
  237. brightness = ColorDB.triplet_to_brightness(rgbtuple)
  238. if brightness <= 128:
  239. outline = 'white'
  240. else:
  241. outline = 'black'
  242. self.__canvas.itemconfigure(chip, outline=outline)
  243. def update_yourself(self, red, green, blue):
  244. assert self.__generator
  245. i = 1
  246. chip = 0
  247. chips = self.__chips = []
  248. tk = self.__canvas.tk
  249. # get the red, green, and blue components for all chips
  250. for t in self.__generator(self.__numchips, red, green, blue):
  251. rrggbb = ColorDB.triplet_to_rrggbb(t)
  252. chips.append(rrggbb)
  253. tred, tgreen, tblue = t
  254. if tred <= red and tgreen <= green and tblue <= blue:
  255. chip = i
  256. i = i + 1
  257. # call the raw tcl script
  258. colors = SPACE.join(chips)
  259. tk.eval('setcolor %s {%s}' % (self.__canvas._w, colors))
  260. # move the arrows around
  261. self.__trackarrow(chip, (red, green, blue))
  262. def set(self, label, generator):
  263. self.__canvas.itemconfigure(self.__label, text=label)
  264. self.__generator = generator
  265. class StripViewer:
  266. def __init__(self, switchboard, master=None):
  267. self.__sb = switchboard
  268. optiondb = switchboard.optiondb()
  269. # create a frame inside the master.
  270. frame = Frame(master, relief=RAISED, borderwidth=1)
  271. frame.grid(row=1, column=0, columnspan=2, sticky='NSEW')
  272. # create the options to be used later
  273. uwd = self.__uwdvar = BooleanVar()
  274. uwd.set(optiondb.get('UPWHILEDRAG', 0))
  275. hexp = self.__hexpvar = BooleanVar()
  276. hexp.set(optiondb.get('HEXSTRIP', 0))
  277. # create the red, green, blue strips inside their own frame
  278. frame1 = Frame(frame)
  279. frame1.pack(expand=YES, fill=BOTH)
  280. self.__reds = StripWidget(switchboard, frame1,
  281. generator=constant_cyan_generator,
  282. axis=0,
  283. label='Red Variations',
  284. uwdvar=uwd, hexvar=hexp)
  285. self.__greens = StripWidget(switchboard, frame1,
  286. generator=constant_magenta_generator,
  287. axis=1,
  288. label='Green Variations',
  289. uwdvar=uwd, hexvar=hexp)
  290. self.__blues = StripWidget(switchboard, frame1,
  291. generator=constant_yellow_generator,
  292. axis=2,
  293. label='Blue Variations',
  294. uwdvar=uwd, hexvar=hexp)
  295. # create a frame to contain the controls
  296. frame2 = Frame(frame)
  297. frame2.pack(expand=YES, fill=BOTH)
  298. frame2.columnconfigure(0, weight=20)
  299. frame2.columnconfigure(2, weight=20)
  300. padx = 8
  301. # create the black button
  302. blackbtn = Button(frame2,
  303. text='Black',
  304. command=self.__toblack)
  305. blackbtn.grid(row=0, column=0, rowspan=2, sticky=W, padx=padx)
  306. # create the controls
  307. uwdbtn = Checkbutton(frame2,
  308. text='Update while dragging',
  309. variable=uwd)
  310. uwdbtn.grid(row=0, column=1, sticky=W)
  311. hexbtn = Checkbutton(frame2,
  312. text='Hexadecimal',
  313. variable=hexp,
  314. command=self.__togglehex)
  315. hexbtn.grid(row=1, column=1, sticky=W)
  316. # XXX: ignore this feature for now; it doesn't work quite right yet
  317. ## gentypevar = self.__gentypevar = IntVar()
  318. ## self.__variations = Radiobutton(frame,
  319. ## text='Variations',
  320. ## variable=gentypevar,
  321. ## value=0,
  322. ## command=self.__togglegentype)
  323. ## self.__variations.grid(row=0, column=1, sticky=W)
  324. ## self.__constants = Radiobutton(frame,
  325. ## text='Constants',
  326. ## variable=gentypevar,
  327. ## value=1,
  328. ## command=self.__togglegentype)
  329. ## self.__constants.grid(row=1, column=1, sticky=W)
  330. # create the white button
  331. whitebtn = Button(frame2,
  332. text='White',
  333. command=self.__towhite)
  334. whitebtn.grid(row=0, column=2, rowspan=2, sticky=E, padx=padx)
  335. def update_yourself(self, red, green, blue):
  336. self.__reds.update_yourself(red, green, blue)
  337. self.__greens.update_yourself(red, green, blue)
  338. self.__blues.update_yourself(red, green, blue)
  339. def __togglehex(self, event=None):
  340. red, green, blue = self.__sb.current_rgb()
  341. self.update_yourself(red, green, blue)
  342. ## def __togglegentype(self, event=None):
  343. ## which = self.__gentypevar.get()
  344. ## if which == 0:
  345. ## self.__reds.set(label='Red Variations',
  346. ## generator=constant_cyan_generator)
  347. ## self.__greens.set(label='Green Variations',
  348. ## generator=constant_magenta_generator)
  349. ## self.__blues.set(label='Blue Variations',
  350. ## generator=constant_yellow_generator)
  351. ## elif which == 1:
  352. ## self.__reds.set(label='Red Constant',
  353. ## generator=constant_red_generator)
  354. ## self.__greens.set(label='Green Constant',
  355. ## generator=constant_green_generator)
  356. ## self.__blues.set(label='Blue Constant',
  357. ## generator=constant_blue_generator)
  358. ## else:
  359. ## assert 0
  360. ## self.__sb.update_views_current()
  361. def __toblack(self, event=None):
  362. self.__sb.update_views(0, 0, 0)
  363. def __towhite(self, event=None):
  364. self.__sb.update_views(255, 255, 255)
  365. def save_options(self, optiondb):
  366. optiondb['UPWHILEDRAG'] = self.__uwdvar.get()
  367. optiondb['HEXSTRIP'] = self.__hexpvar.get()