1
#define __SP_DROPPER_CONTEXT_C__
4
* Tool for picking colors from drawing
7
* Lauris Kaplinski <lauris@kaplinski.com>
8
* bulia byak <buliabyak@users.sf.net>
10
* Copyright (C) 1999-2005 Authors
12
* Released under GNU GPL, read the file 'COPYING' for more information
19
#include <glibmm/i18n.h>
20
#include <glibmm/ustring.h>
21
#include <glibmm/refptr.h>
22
#include <gtkmm/clipboard.h>
23
#include <gdk/gdkkeysyms.h>
25
#include "libnr/n-art-bpath.h"
27
#include "display/canvas-bpath.h"
28
#include "display/canvas-arena.h"
29
#include "display/curve.h"
32
#include "color-rgba.h"
33
#include "desktop-style.h"
34
#include "prefs-utils.h"
35
#include "sp-namedview.h"
37
#include "desktop-handles.h"
38
#include "selection.h"
41
#include "pixmaps/cursor-dropper.xpm"
43
#include "dropper-context.h"
44
#include "message-context.h"
45
#include "libnr/nr-scale-translate-ops.h"
48
static NArtBpath const spdc_circle[] = {
49
{ NR_MOVETO, 0, 0, 0, 0, -1, 0 },
50
{ NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
51
{ NR_CURVETO, C1, 1, 1, C1, 1, 0 },
52
{ NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
53
{ NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
54
{ NR_END, 0, 0, 0, 0, 0, 0 }
58
static void sp_dropper_context_class_init(SPDropperContextClass *klass);
59
static void sp_dropper_context_init(SPDropperContext *dc);
61
static void sp_dropper_context_setup(SPEventContext *ec);
62
static void sp_dropper_context_finish(SPEventContext *ec);
64
static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent * event);
66
static SPEventContextClass *parent_class;
68
GType sp_dropper_context_get_type()
70
static GType type = 0;
73
sizeof(SPDropperContextClass),
75
(GClassInitFunc) sp_dropper_context_class_init,
77
sizeof(SPDropperContext),
79
(GInstanceInitFunc) sp_dropper_context_init,
80
NULL, /* value_table */
82
type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDropperContext", &info, (GTypeFlags) 0);
87
static void sp_dropper_context_class_init(SPDropperContextClass *klass)
89
SPEventContextClass *ec_class = (SPEventContextClass *) klass;
91
parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
93
ec_class->setup = sp_dropper_context_setup;
94
ec_class->finish = sp_dropper_context_finish;
95
ec_class->root_handler = sp_dropper_context_root_handler;
98
static void sp_dropper_context_init(SPDropperContext *dc)
100
SPEventContext *event_context = SP_EVENT_CONTEXT(dc);
101
event_context->cursor_shape = cursor_dropper_xpm;
102
event_context->hot_x = 7;
103
event_context->hot_y = 7;
106
static void sp_dropper_context_setup(SPEventContext *ec)
108
SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
110
if (((SPEventContextClass *) parent_class)->setup) {
111
((SPEventContextClass *) parent_class)->setup(ec);
114
SPCurve *c = sp_curve_new_from_static_bpath(spdc_circle);
115
dc->area = sp_canvas_bpath_new(SP_DT_CONTROLS(ec->desktop), c);
117
sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(dc->area), 0x00000000,(SPWindRule)0);
118
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
119
sp_canvas_item_hide(dc->area);
121
if (prefs_get_int_attribute("tools.dropper", "selcue", 0) != 0) {
122
ec->enableSelectionCue();
125
if (prefs_get_int_attribute("tools.dropper", "gradientdrag", 0) != 0) {
130
static void sp_dropper_context_finish(SPEventContext *ec)
132
SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
134
ec->enableGrDrag(false);
137
gtk_object_destroy(GTK_OBJECT(dc->area));
144
* Copies the current context color to the clipboard.
146
void sp_dropper_context_copy(SPEventContext *ec)
148
SPDropperContext *dc = SP_DROPPER_CONTEXT(ec);
150
guint32 const c32 = SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B, dc->alpha);
152
int pick = prefs_get_int_attribute("tools.dropper", "pick",
153
SP_DROPPER_PICK_VISIBLE);
156
g_snprintf(c, 64, "%06x%02x", c32 >> 8,
157
pick == SP_DROPPER_PICK_ACTUAL? SP_COLOR_F_TO_U(dc->alpha) : 255);
163
Glib::RefPtr<Gtk::Clipboard> refClipboard =
164
Gtk::Clipboard::get();
165
refClipboard->set_text(text);
170
* Makes a copy of the current desktop color to the clipboard.
172
void sp_dropper_c32_color_copy(guint32 c32)
174
int const pick = prefs_get_int_attribute("tools.dropper", "pick",
175
SP_DROPPER_PICK_VISIBLE);
178
g_snprintf(c, 64, "%06x%02x", c32 >> 8,
179
pick == SP_DROPPER_PICK_ACTUAL? SP_RGBA32_A_U(c32) : 255);
184
Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
185
refClipboard->set_text(text);
191
* Makes a copy of the current color as a hex value. This should always compute
192
* the current color without alpha, but the on-screen representation.
194
void sp_dropper_c32_color_copy_hex(guint32 c32)
197
int pick = prefs_get_int_attribute ("tools.dropper", "pick",
198
SP_DROPPER_PICK_VISIBLE);
200
if ( pick == SP_DROPPER_PICK_ACTUAL )
201
; // process c32 so that it computes against page
202
// else just can cut off that last 2 hex digits....
207
g_snprintf(c, 48, "%06x", c32 >> 8);
212
Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
213
refClipboard->set_text(text);
218
static gint sp_dropper_context_root_handler(SPEventContext *ec, GdkEvent *event)
220
SPDropperContext *dc = (SPDropperContext *) ec;
223
int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE);
225
switch (event->type) {
226
case GDK_BUTTON_PRESS:
227
if (event->button.button == 1) {
228
dc->centre = NR::Point(event->button.x, event->button.y);
233
case GDK_MOTION_NOTIFY:
234
if (event->motion.state & GDK_BUTTON2_MASK) {
235
// pass on middle-drag
239
// otherwise, constantly calculate color no matter is any button pressed or not
242
double W(0), R(0), G(0), B(0), A(0);
248
rw = std::min(NR::L2(NR::Point(event->button.x, event->button.y) - dc->centre), 400.0);
250
if (rw == 0) { // happens sometimes, little idea why...
254
NR::Point const cd = ec->desktop->w2d(dc->centre);
255
NR::Matrix const w2dt = ec->desktop->w2d();
256
const double scale = rw * NR_MATRIX_DF_EXPANSION(&w2dt);
257
NR::Matrix const sm( NR::scale(scale, scale) * NR::translate(cd) );
258
sp_canvas_item_affine_absolute(dc->area, sm);
259
sp_canvas_item_show(dc->area);
262
const int x0 = (int) floor(dc->centre[NR::X] - rw);
263
const int y0 = (int) floor(dc->centre[NR::Y] - rw);
264
const int x1 = (int) ceil(dc->centre[NR::X] + rw);
265
const int y1 = (int) ceil(dc->centre[NR::Y] + rw);
267
if ((x1 > x0) && (y1 > y0)) {
269
nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x0, y0, x1, y1, TRUE);
270
/* fixme: (Lauris) */
271
sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(SP_DT_DRAWING(ec->desktop)), &pb);
272
for (int y = y0; y < y1; y++) {
273
const unsigned char *s = NR_PIXBLOCK_PX(&pb) + (y - y0) * pb.rs;
274
for (int x = x0; x < x1; x++) {
275
const double dx = x - dc->centre[NR::X];
276
const double dy = y - dc->centre[NR::Y];
277
const double w = exp(-((dx * dx) + (dy * dy)) / (rw * rw));
286
nr_pixblock_release(&pb);
288
R = (R + 0.001) / (255.0 * W);
289
G = (G + 0.001) / (255.0 * W);
290
B = (B + 0.001) / (255.0 * W);
291
A = (A + 0.001) / (255.0 * W);
293
R = CLAMP(R, 0.0, 1.0);
294
G = CLAMP(G, 0.0, 1.0);
295
B = CLAMP(B, 0.0, 1.0);
296
A = CLAMP(A, 0.0, 1.0);
302
int x = (int) floor(event->button.x);
303
int y = (int) floor(event->button.y);
304
nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
305
sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(SP_DT_DRAWING(ec->desktop)), &pb);
306
const unsigned char *s = NR_PIXBLOCK_PX(&pb);
314
if (pick == SP_DROPPER_PICK_VISIBLE) {
315
// compose with page color
316
guint32 bg = SP_DT_NAMEDVIEW(ec->desktop)->pagecolor;
317
R = R + (SP_RGBA32_R_F(bg)) * (1 - A);
318
G = G + (SP_RGBA32_G_F(bg)) * (1 - A);
319
B = B + (SP_RGBA32_B_F(bg)) * (1 - A);
322
// un-premultiply color channels
330
if (fabs(A) < 1e-4) {
331
A = 0; // suppress exponentials, CSS does not allow that
341
guint32 c32 = SP_RGBA32_F_COMPOSE(R, G, B, A);
344
sp_svg_write_color(c, 64, c32);
346
// alpha of color under cursor, to show in the statusbar
347
// locale-sensitive printf is OK, since this goes to the UI, not into SVG
348
gchar *alpha = g_strdup_printf(_(" alpha %.3g"), A);
349
// where the color is picked, to show in the statusbar
350
gchar *where = dc->dragging ? g_strdup_printf(_(", averaged with radius %d"), (int) rw) : g_strdup_printf(_(" under cursor"));
351
// message, to show in the statusbar
352
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");
353
ec->defaultMessageContext()->setF(
354
Inkscape::NORMAL_MESSAGE,
355
"<b>%s%s</b>%s. %s", c,
356
(pick == SP_DROPPER_PICK_VISIBLE)? "" : alpha,
366
case GDK_BUTTON_RELEASE:
367
if (event->button.button == 1)
369
sp_canvas_item_hide(dc->area);
370
dc->dragging = FALSE;
372
// do the actual color setting
373
sp_desktop_set_color(ec->desktop,
374
(event->button.state & GDK_MOD1_MASK)?
375
ColorRGBA(1 - dc->R, 1 - dc->G, 1 - dc->B, dc->alpha) : ColorRGBA(dc->R, dc->G, dc->B, dc->alpha),
376
false, !(event->button.state & GDK_SHIFT_MASK));
378
// REJON: set aux. toolbar input to hex color!
381
if (!(SP_DT_SELECTION(ec->desktop)->isEmpty())) {
382
sp_document_done(SP_DT_DOCUMENT(ec->desktop));
389
switch (get_group0_keyval(&event->key)) {
394
// prevent the zoom field from activation
395
if (!MOD__CTRL_ONLY) {
400
SP_DT_SELECTION(ec->desktop)->clear();
410
if (((SPEventContextClass *) parent_class)->root_handler) {
411
ret = ((SPEventContextClass *) parent_class)->root_handler(ec, event);
422
c-file-style:"stroustrup"
423
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
428
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :