1 /*
2 * Tool for picking colors from drawing
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * bulia byak <buliabyak@users.sf.net>
7 * Abhishek Sharma
8 *
9 * Copyright (C) 1999-2005 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include <glibmm/i18n.h>
19 #include <glibmm/ustring.h>
20 #include <glibmm/refptr.h>
21 #include <gtkmm/clipboard.h>
22 #include <gdk/gdkkeysyms.h>
24 #include "macros.h"
25 #include "display/canvas-bpath.h"
26 #include "display/canvas-arena.h"
27 #include "display/curve.h"
28 #include "svg/svg-color.h"
29 #include "color.h"
30 #include "color-rgba.h"
31 #include "desktop-style.h"
32 #include "preferences.h"
33 #include "sp-namedview.h"
34 #include "desktop.h"
35 #include "desktop-handles.h"
36 #include "selection.h"
37 #include "document.h"
39 #include "pixmaps/cursor-dropper.xpm"
41 #include "dropper-context.h"
42 #include "message-context.h"
43 //#include "libnr/nr-scale-translate-ops.h"
45 using Inkscape::DocumentUndo;
47 static void sp_dropper_context_class_init(SPDropperContextClass *klass);
48 static void sp_dropper_context_init(SPDropperContext *dc);
50 static void sp_dropper_context_setup(SPEventContext *ec);
51 static void sp_dropper_context_finish(SPEventContext *ec);
53 static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent * event);
55 static SPEventContextClass *parent_class;
57 GType sp_dropper_context_get_type()
58 {
59 static GType type = 0;
60 if (!type) {
61 GTypeInfo info = {
62 sizeof(SPDropperContextClass),
63 NULL, NULL,
64 (GClassInitFunc) sp_dropper_context_class_init,
65 NULL, NULL,
66 sizeof(SPDropperContext),
67 4,
68 (GInstanceInitFunc) sp_dropper_context_init,
69 NULL, /* value_table */
70 };
71 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDropperContext", &info, (GTypeFlags) 0);
72 }
73 return type;
74 }
76 static void sp_dropper_context_class_init(SPDropperContextClass *klass)
77 {
78 SPEventContextClass *ec_class = (SPEventContextClass *) klass;
80 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
82 ec_class->setup = sp_dropper_context_setup;
83 ec_class->finish = sp_dropper_context_finish;
84 ec_class->root_handler = sp_dropper_context_root_handler;
85 }
87 static void sp_dropper_context_init(SPDropperContext *dc)
88 {
89 SPEventContext *event_context = SP_EVENT_CONTEXT(dc);
90 event_context->cursor_shape = cursor_dropper_xpm;
91 event_context->hot_x = 7;
92 event_context->hot_y = 7;
93 }
95 static void sp_dropper_context_setup(SPEventContext *ec)
96 {
97 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
99 if (((SPEventContextClass *) parent_class)->setup) {
100 ((SPEventContextClass *) parent_class)->setup(ec);
101 }
103 /* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
104 SPCurve *c = new SPCurve();
105 const double C1 = 0.552;
106 c->moveto(-1,0);
107 c->curveto(-1, C1, -C1, 1, 0, 1 );
108 c->curveto(C1, 1, 1, C1, 1, 0 );
109 c->curveto(1, -C1, C1, -1, 0, -1 );
110 c->curveto(-C1, -1, -1, -C1, -1, 0 );
111 c->closepath();
112 dc->area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
113 c->unref();
114 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(dc->area), 0x00000000,(SPWindRule)0);
115 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
116 sp_canvas_item_hide(dc->area);
118 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
119 if (prefs->getBool("/tools/dropper/selcue")) {
120 ec->enableSelectionCue();
121 }
123 if (prefs->getBool("/tools/dropper/gradientdrag")) {
124 ec->enableGrDrag();
125 }
126 }
128 static void sp_dropper_context_finish(SPEventContext *ec)
129 {
130 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
132 ec->enableGrDrag(false);
134 if (dc->grabbed) {
135 sp_canvas_item_ungrab(dc->grabbed, GDK_CURRENT_TIME);
136 dc->grabbed = NULL;
137 }
139 if (dc->area) {
140 gtk_object_destroy(GTK_OBJECT(dc->area));
141 dc->area = NULL;
142 }
143 }
146 /**
147 * Returns the current dropper context icc-color.
148 */
149 SPColor* sp_dropper_context_get_icc_color(SPEventContext */*ec*/)
150 {
151 //TODO: implement-me!
153 return 0; // At least we will cause a clean crash, instead of random corruption.
154 }
156 /**
157 * Returns the current dropper context color.
158 */
159 guint32 sp_dropper_context_get_color(SPEventContext *ec)
160 {
161 SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
162 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
164 int pick = prefs->getInt("/tools/dropper/pick",
165 SP_DROPPER_PICK_VISIBLE);
166 bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
168 return SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B,
169 (pick == SP_DROPPER_PICK_ACTUAL && setalpha) ? dc->alpha : 1.0);
170 }
173 static gint sp_dropper_context_root_handler(SPEventContext *event_context, GdkEvent *event)
174 {
175 SPDropperContext *dc = (SPDropperContext *) event_context;
176 int ret = FALSE;
177 SPDesktop *desktop = event_context->desktop;
178 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
180 int pick = prefs->getInt("/tools/dropper/pick", SP_DROPPER_PICK_VISIBLE);
181 bool setalpha = prefs->getBool("/tools/dropper/setalpha", true);
183 switch (event->type) {
184 case GDK_BUTTON_PRESS:
185 if (event->button.button == 1 && !event_context->space_panning) {
186 dc->centre = Geom::Point(event->button.x, event->button.y);
187 dc->dragging = TRUE;
188 ret = TRUE;
189 }
191 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
192 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
193 NULL, event->button.time);
194 dc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
196 break;
197 case GDK_MOTION_NOTIFY:
198 if (event->motion.state & GDK_BUTTON2_MASK) {
199 // pass on middle-drag
200 ret = FALSE;
201 break;
202 } else if (!event_context->space_panning) {
203 // otherwise, constantly calculate color no matter is any button pressed or not
205 double rw = 0.0;
206 double W(0), R(0), G(0), B(0), A(0);
208 if (dc->dragging) {
209 // calculate average
211 // radius
212 rw = std::min(Geom::L2(Geom::Point(event->button.x, event->button.y) - dc->centre), 400.0);
214 if (rw == 0) { // happens sometimes, little idea why...
215 break;
216 }
218 Geom::Point const cd = desktop->w2d(dc->centre);
219 Geom::Matrix const w2dt = desktop->w2d();
220 const double scale = rw * w2dt.descrim();
221 Geom::Matrix const sm( Geom::Scale(scale, scale) * Geom::Translate(cd) );
222 sp_canvas_item_affine_absolute(dc->area, sm);
223 sp_canvas_item_show(dc->area);
225 /* Get buffer */
226 const int x0 = (int) floor(dc->centre[Geom::X] - rw);
227 const int y0 = (int) floor(dc->centre[Geom::Y] - rw);
228 const int x1 = (int) ceil(dc->centre[Geom::X] + rw);
229 const int y1 = (int) ceil(dc->centre[Geom::Y] + rw);
231 if ((x1 > x0) && (y1 > y0)) {
232 NRPixBlock pb;
233 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
234 /* fixme: (Lauris) */
235 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
236 for (int y = y0; y < y1; y++) {
237 const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
238 for (int x = x0; x < x1; x++) {
239 const double dx = x - dc->centre[Geom::X];
240 const double dy = y - dc->centre[Geom::Y];
241 const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
242 W += w;
243 R += w * s[0];
244 G += w * s[1];
245 B += w * s[2];
246 A += w * s[3];
247 s += 4;
248 }
249 }
250 nr_pixblock_release(&pb);
252 R = (R + 0.001) / (255.0 * W);
253 G = (G + 0.001) / (255.0 * W);
254 B = (B + 0.001) / (255.0 * W);
255 A = (A + 0.001) / (255.0 * W);
257 R = CLAMP(R, 0.0, 1.0);
258 G = CLAMP(G, 0.0, 1.0);
259 B = CLAMP(B, 0.0, 1.0);
260 A = CLAMP(A, 0.0, 1.0);
261 }
263 } else {
264 // pick single pixel
265 NRPixBlock pb;
266 int x = (int) floor(event->button.x);
267 int y = (int) floor(event->button.y);
268 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
269 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(desktop)), &pb);
270 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
272 R = s[0] / 255.0;
273 G = s[1] / 255.0;
274 B = s[2] / 255.0;
275 A = s[3] / 255.0;
276 }
278 if (pick == SP_DROPPER_PICK_VISIBLE) {
279 // compose with page color
280 guint32 bg = sp_desktop_namedview(desktop)->pagecolor;
281 R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
282 G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
283 B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
284 A = 1.0;
285 } else {
286 // un-premultiply color channels
287 if (A > 0) {
288 R /= A;
289 G /= A;
290 B /= A;
291 }
292 }
294 if (fabs(A) < 1e-4) {
295 A = 0; // suppress exponentials, CSS does not allow that
296 }
298 // remember color
299 dc->R = R;
300 dc->G = G;
301 dc->B = B;
302 dc->alpha = A;
304 // status message
305 double alpha_to_set = setalpha? dc->alpha : 1.0;
306 guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, alpha_to_set);
308 gchar c[64];
309 sp_svg_write_color(c, sizeof(c), c32);
311 // alpha of color under cursor, to show in the statusbar
312 // locale-sensitive printf is OK, since this goes to the UI, not into SVG
313 gchar *alpha = g_strdup_printf(_(" alpha %.3g"), alpha_to_set);
314 // where the color is picked, to show in the statusbar
315 gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
316 // message, to show in the statusbar
317 const gchar *message = dc->dragging ? _("<b>Release mouse</b> to set color.") : _("<b>Click</b> to set fill, <b>Shift+click</b> to set stroke; <b>drag</b> to average color in area; with <b>Alt</b> to pick inverse color; <b>Ctrl+C</b> to copy the color under mouse to clipboard");
318 event_context->defaultMessageContext()->setF(
319 Inkscape::NORMAL_MESSAGE,
320 "<b>%s%s</b>%s. %s", c,
321 (pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
322 where, message
323 );
325 g_free(where);
326 g_free(alpha);
328 ret = TRUE;
329 }
330 break;
331 case GDK_BUTTON_RELEASE:
332 if (event->button.button == 1 && !event_context->space_panning)
333 {
334 sp_canvas_item_hide(dc->area);
335 dc->dragging = FALSE;
337 if (dc->grabbed) {
338 sp_canvas_item_ungrab(dc->grabbed, event->button.time);
339 dc->grabbed = NULL;
340 }
342 double alpha_to_set = setalpha? dc->alpha : 1.0;
344 // do the actual color setting
345 sp_desktop_set_color(desktop,
346 (event->button.state & GDK_MOD1_MASK)?
347 ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, alpha_to_set) : ColorRGBA(dc->R, dc->G, dc->B, alpha_to_set),
348 false, !(event->button.state & GDK_SHIFT_MASK));
350 // REJON: set aux. toolbar input to hex color!
353 if (!(sp_desktop_selection(desktop)->isEmpty())) {
354 DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_DROPPER,
355 _("Set picked color"));
356 }
358 ret = TRUE;
359 }
360 break;
361 case GDK_KEY_PRESS:
362 switch (get_group0_keyval(&event->key)) {
363 case GDK_Up:
364 case GDK_Down:
365 case GDK_KP_Up:
366 case GDK_KP_Down:
367 // prevent the zoom field from activation
368 if (!MOD__CTRL_ONLY) {
369 ret = TRUE;
370 }
371 break;
372 case GDK_Escape:
373 sp_desktop_selection(desktop)->clear();
374 default:
375 break;
376 }
377 break;
378 default:
379 break;
380 }
382 if (!ret) {
383 if (((SPEventContextClass *) parent_class)->root_handler) {
384 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
385 }
386 }
388 return ret;
389 }
392 /*
393 Local Variables:
394 mode:c++
395 c-file-style:"stroustrup"
396 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
397 indent-tabs-mode:nil
398 fill-column:99
399 End:
400 */
401 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :