70
60
#include "BKE_main.h"
71
61
#include "BLI_ghash.h"
73
#include "ED_image.h" /* for HDR color sampling */
74
#include "ED_node.h" /* for HDR color sampling */
75
#include "ED_clip.h" /* for HDR color sampling */
77
/* ********************************************************** */
79
typedef struct Eyedropper {
80
struct ColorManagedDisplay *display;
86
int accum_start; /* has mouse been presed */
91
static int eyedropper_init(bContext *C, wmOperator *op)
93
Scene *scene = CTX_data_scene(C);
96
op->customdata = eye = MEM_callocN(sizeof(Eyedropper), "Eyedropper");
98
uiContextActiveProperty(C, &eye->ptr, &eye->prop, &eye->index);
100
if ((eye->ptr.data == NULL) ||
101
(eye->prop == NULL) ||
102
(RNA_property_editable(&eye->ptr, eye->prop) == FALSE) ||
103
(RNA_property_array_length(&eye->ptr, eye->prop) < 3) ||
104
(RNA_property_type(eye->prop) != PROP_FLOAT))
109
if (RNA_property_subtype(eye->prop) == PROP_COLOR) {
110
const char *display_device;
112
display_device = scene->display_settings.display_device;
113
eye->display = IMB_colormanagement_display_get_named(display_device);
119
static void eyedropper_exit(bContext *C, wmOperator *op)
121
WM_cursor_modal_restore(CTX_wm_window(C));
124
MEM_freeN(op->customdata);
125
op->customdata = NULL;
128
static int eyedropper_cancel(bContext *C, wmOperator *op)
130
eyedropper_exit(C, op);
131
return OPERATOR_CANCELLED;
134
/* *** eyedropper_color_ helper functions *** */
137
* \brief get the color from the screen.
139
* Special check for image or nodes where we MAY have HDR pixels which don't display.
141
static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int mx, int my, float r_col[3])
144
/* we could use some clever */
145
wmWindow *win = CTX_wm_window(C);
147
for (sa = win->screen->areabase.first; sa; sa = sa->next) {
148
if (BLI_rcti_isect_pt(&sa->totrct, mx, my)) {
149
if (sa->spacetype == SPACE_IMAGE) {
150
ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
151
if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
152
SpaceImage *sima = sa->spacedata.first;
153
int mval[2] = {mx - ar->winrct.xmin,
154
my - ar->winrct.ymin};
156
if (ED_space_image_color_sample(sima, ar, mval, r_col)) {
161
else if (sa->spacetype == SPACE_NODE) {
162
ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
163
if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
164
SpaceNode *snode = sa->spacedata.first;
165
int mval[2] = {mx - ar->winrct.xmin,
166
my - ar->winrct.ymin};
168
if (ED_space_node_color_sample(snode, ar, mval, r_col)) {
173
else if (sa->spacetype == SPACE_CLIP) {
174
ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
175
if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
176
SpaceClip *sc = sa->spacedata.first;
177
int mval[2] = {mx - ar->winrct.xmin,
178
my - ar->winrct.ymin};
180
if (ED_space_clip_color_sample(sc, ar, mval, r_col)) {
188
/* fallback to simple opengl picker */
189
glReadBuffer(GL_FRONT);
190
glReadPixels(mx, my, 1, 1, GL_RGB, GL_FLOAT, r_col);
191
glReadBuffer(GL_BACK);
194
/* sets the sample color RGB, maintaining A */
195
static void eyedropper_color_set(bContext *C, Eyedropper *eye, const float col[3])
199
/* to maintain alpha */
200
RNA_property_float_get_array(&eye->ptr, eye->prop, col_conv);
202
/* convert from display space to linear rgb space */
204
copy_v3_v3(col_conv, col);
205
IMB_colormanagement_display_to_scene_linear_v3(col_conv, eye->display);
208
copy_v3_v3(col_conv, col);
211
RNA_property_float_set_array(&eye->ptr, eye->prop, col_conv);
213
RNA_property_update(C, &eye->ptr, eye->prop);
216
/* set sample from accumulated values */
217
static void eyedropper_color_set_accum(bContext *C, Eyedropper *eye)
220
mul_v3_v3fl(col, eye->accum_col, 1.0f / (float)eye->accum_tot);
221
eyedropper_color_set(C, eye, col);
224
/* single point sample & set */
225
static void eyedropper_color_sample(bContext *C, Eyedropper *eye, int mx, int my)
228
eyedropper_color_sample_fl(C, eye, mx, my, col);
229
eyedropper_color_set(C, eye, col);
232
static void eyedropper_color_sample_accum(bContext *C, Eyedropper *eye, int mx, int my)
235
eyedropper_color_sample_fl(C, eye, mx, my, col);
236
/* delay linear conversion */
237
add_v3_v3(eye->accum_col, col);
241
/* main modal status check */
242
static int eyedropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
244
Eyedropper *eye = (Eyedropper *)op->customdata;
246
switch (event->type) {
249
return eyedropper_cancel(C, op);
251
if (event->val == KM_RELEASE) {
252
if (eye->accum_tot == 0) {
253
eyedropper_color_sample(C, eye, event->x, event->y);
256
eyedropper_color_set_accum(C, eye);
258
eyedropper_exit(C, op);
259
return OPERATOR_FINISHED;
261
else if (event->val == KM_PRESS) {
262
/* enable accum and make first sample */
263
eye->accum_start = TRUE;
264
eyedropper_color_sample_accum(C, eye, event->x, event->y);
268
if (eye->accum_start) {
269
/* button is pressed so keep sampling */
270
eyedropper_color_sample_accum(C, eye, event->x, event->y);
271
eyedropper_color_set_accum(C, eye);
275
if (event->val == KM_RELEASE) {
277
zero_v3(eye->accum_col);
278
eyedropper_color_sample_accum(C, eye, event->x, event->y);
279
eyedropper_color_set_accum(C, eye);
284
return OPERATOR_RUNNING_MODAL;
287
/* Modal Operator init */
288
static int eyedropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
291
if (eyedropper_init(C, op)) {
292
WM_cursor_modal_set(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
294
/* add temp handler */
295
WM_event_add_modal_handler(C, op);
297
return OPERATOR_RUNNING_MODAL;
300
eyedropper_exit(C, op);
301
return OPERATOR_CANCELLED;
305
/* Repeat operator */
306
static int eyedropper_exec(bContext *C, wmOperator *op)
309
if (eyedropper_init(C, op)) {
314
eyedropper_exit(C, op);
316
return OPERATOR_FINISHED;
319
return OPERATOR_CANCELLED;
323
static int eyedropper_poll(bContext *C)
325
if (!CTX_wm_window(C)) return 0;
329
static void UI_OT_eyedropper(wmOperatorType *ot)
332
ot->name = "Eyedropper";
333
ot->idname = "UI_OT_eyedropper";
334
ot->description = "Sample a color from the Blender Window to store in a property";
337
ot->invoke = eyedropper_invoke;
338
ot->modal = eyedropper_modal;
339
ot->cancel = eyedropper_cancel;
340
ot->exec = eyedropper_exec;
341
ot->poll = eyedropper_poll;
344
ot->flag = OPTYPE_BLOCKING;
349
63
/* Reset Default Theme ------------------------ */
351
65
static int reset_default_theme_exec(bContext *C, wmOperator *UNUSED(op))