OSDN Git Service

colorize: add brushmodifier action & toolbar entry
[mypaint-anime/master.git] / gui / colorselectionwindow.py
1 # This file is part of MyPaint.
2 # Copyright (C) 2007 by Martin Renold <martinxyz@gmx.ch>
3 #
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.
8
9 "select color window (GTK and an own window)"
10 from gettext import gettext as _
11
12 import gtk
13 gdk = gtk.gdk
14
15 import windowing
16 import stock
17 from lib import mypaintlib
18 from lib.helpers import clamp,gdkpixbuf2numpy
19 import dialogs
20 from hsvcompat import ColorChangerHSV
21
22
23 class ToolWidget (gtk.VBox):
24     """Tool widget with the standard GTK color selector (triangle)."""
25
26     stock_id = stock.TOOL_COLOR_SELECTOR
27
28     def __init__(self, app):
29         gtk.VBox.__init__(self)
30         self.pack_start(ColorChangerHSV(app), True, True)
31
32
33 # own color selector
34 # see also colorchanger.hpp
35 class ColorSelectorPopup(windowing.PopupWindow):
36     backend_class = None
37     closes_on_picking = True
38     def __init__(self, app):
39         windowing.PopupWindow.__init__(self, app)
40
41         self.backend = self.backend_class()
42
43         self.image = image = gtk.Image()
44         self.add(image)
45
46         self.set_events(gdk.BUTTON_PRESS_MASK |
47                         gdk.BUTTON_RELEASE_MASK |
48                         gtk.gdk.POINTER_MOTION_MASK |
49                         gdk.ENTER_NOTIFY |
50                         gdk.LEAVE_NOTIFY
51                         )
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)
55
56         self.button_pressed = False
57
58     def enter(self):
59         self.update_image()
60         self.show_all()
61         self.window.set_cursor(gdk.Cursor(gdk.CROSSHAIR))
62         self.mouse_pos = None
63
64     def leave(self, reason):
65         # TODO: make a generic "popupmenu" class with this code?
66         if reason == 'keyup':
67             if self.mouse_pos:
68                 self.pick_color(*self.mouse_pos)
69         self.hide()
70
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)
80
81     def pick_color(self,x,y):
82         hsv = self.backend.pick_color_at(x, y)
83         if hsv:
84             self.app.brush.set_color_hsv(hsv)
85         else:
86             self.hide()
87
88     def motion_notify_cb(self, widget, event):
89         self.mouse_pos = event.x, event.y
90
91     def button_press_cb(self, widget, event):
92         if event.button == 1:
93             self.pick_color(event.x,event.y)
94         self.button_pressed = True
95
96     def button_release_cb(self, widget, event):
97         if self.button_pressed:
98             if event.button == 1:
99                 self.pick_color(event.x,event.y)
100                 if self.closes_on_picking:
101                     # FIXME: hacky?
102                     self.popup_state.leave()
103                 else:
104                     self.update_image()
105
106
107
108 class ColorChangerWashPopup(ColorSelectorPopup):
109     backend_class = mypaintlib.ColorChangerWash
110     outside_popup_timeout = 0.050
111
112 class ColorChangerCrossedBowlPopup(ColorSelectorPopup):
113     backend_class = mypaintlib.ColorChangerCrossedBowl
114     outside_popup_timeout = 0.050
115
116 class ColorRingPopup(ColorSelectorPopup):
117     backend_class = mypaintlib.SCWSColorSelector
118     closes_on_picking = False
119     outside_popup_timeout = 0.050
120