1 # This file is part of MyPaint.
2 # Copyright (C) 2007 by Martin Renold <martinxyz@gmx.ch>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 "select color window (GTK and an own window)"
10 from gettext import gettext as _
17 from lib import mypaintlib
18 from lib.helpers import clamp,gdkpixbuf2numpy
20 from hsvcompat import ColorChangerHSV
23 class ToolWidget (gtk.VBox):
24 """Tool widget with the standard GTK color selector (triangle)."""
26 stock_id = stock.TOOL_COLOR_SELECTOR
28 def __init__(self, app):
29 gtk.VBox.__init__(self)
30 self.pack_start(ColorChangerHSV(app), True, True)
34 # see also colorchanger.hpp
35 class ColorSelectorPopup(windowing.PopupWindow):
37 closes_on_picking = True
38 def __init__(self, app):
39 windowing.PopupWindow.__init__(self, app)
41 self.backend = self.backend_class()
43 self.image = image = gtk.Image()
46 self.set_events(gdk.BUTTON_PRESS_MASK |
47 gdk.BUTTON_RELEASE_MASK |
48 gtk.gdk.POINTER_MOTION_MASK |
52 self.connect("button-release-event", self.button_release_cb)
53 self.connect("button-press-event", self.button_press_cb)
54 self.connect("motion-notify-event", self.motion_notify_cb)
56 self.button_pressed = False
61 self.window.set_cursor(gdk.Cursor(gdk.CROSSHAIR))
64 def leave(self, reason):
65 # TODO: make a generic "popupmenu" class with this code?
68 self.pick_color(*self.mouse_pos)
71 def update_image(self):
72 size = self.backend.get_size()
73 pixbuf = gdk.Pixbuf(gdk.COLORSPACE_RGB, True, 8, size, size)
74 arr = gdkpixbuf2numpy(pixbuf)
75 self.backend.set_brush_color(*self.app.brush.get_color_hsv())
76 self.backend.render(arr)
77 pixmap, mask = pixbuf.render_pixmap_and_mask()
78 self.image.set_from_pixmap(pixmap, mask)
79 self.shape_combine_mask(mask,0,0)
81 def pick_color(self,x,y):
82 hsv = self.backend.pick_color_at(x, y)
84 self.app.brush.set_color_hsv(hsv)
88 def motion_notify_cb(self, widget, event):
89 self.mouse_pos = event.x, event.y
91 def button_press_cb(self, widget, event):
93 self.pick_color(event.x,event.y)
94 self.button_pressed = True
96 def button_release_cb(self, widget, event):
97 if self.button_pressed:
99 self.pick_color(event.x,event.y)
100 if self.closes_on_picking:
102 self.popup_state.leave()
108 class ColorChangerWashPopup(ColorSelectorPopup):
109 backend_class = mypaintlib.ColorChangerWash
110 outside_popup_timeout = 0.050
112 class ColorChangerCrossedBowlPopup(ColorSelectorPopup):
113 backend_class = mypaintlib.ColorChangerCrossedBowl
114 outside_popup_timeout = 0.050
116 class ColorRingPopup(ColorSelectorPopup):
117 backend_class = mypaintlib.SCWSColorSelector
118 closes_on_picking = False
119 outside_popup_timeout = 0.050