Blender V5.3
interface_region_color_picker.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdarg>
12#include <cstdlib>
13#include <cstring>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_userdef_types.h"
18
19#include "BLI_listbase.hh"
20#include "BLI_rect.hh"
21#include "BLI_string_utf8.hh"
22#include "BLI_utildefines.hh"
23
24#include "BKE_context.hh"
25
26#include "UI_interface_c.hh"
27#include "WM_api.hh"
28#include "WM_types.hh"
29
30#include "RNA_access.hh"
31
32#include "BLT_translation.hh"
33
35
36#include "interface_intern.hh"
37
38namespace blender::ui {
39
44
49
52
53/* -------------------------------------------------------------------- */
56
57static void color_picker_rgb_round(float rgb[3])
58{
59 /* Handle small rounding errors in color space conversions. Doing these for
60 * all color space conversions would be expensive, but for the color picker
61 * we can do the extra work. */
62 for (int i = 0; i < 3; i++) {
63 if (fabsf(rgb[i]) < 5e-5f) {
64 rgb[i] = 0.0f;
65 }
66 else if (fabsf(1.0f - rgb[i]) < 5e-5f) {
67 rgb[i] = 1.0f;
68 }
69 }
70}
71
72void color_picker_rgb_to_hsv_compat(const float rgb[3], float r_cp[3])
73{
74 /* Convert RGB to HSV, remaining as compatible as possible with the existing
75 * r_hsv value (for example when value goes to zero, preserve the hue). */
76 switch (U.color_picker_type) {
78 rgb_to_hsl_compat_v(rgb, r_cp);
79 break;
80 default:
81 rgb_to_hsv_compat_v(rgb, r_cp);
82 break;
83 }
84}
85
86void color_picker_rgb_to_hsv(const float rgb[3], float r_cp[3])
87{
88 switch (U.color_picker_type) {
90 rgb_to_hsl_v(rgb, r_cp);
91 break;
92 default:
93 rgb_to_hsv_v(rgb, r_cp);
94 break;
95 }
96}
97
98void color_picker_hsv_to_rgb(const float r_cp[3], float rgb[3])
99{
100 switch (U.color_picker_type) {
102 hsl_to_rgb_v(r_cp, rgb);
103 break;
104 default:
105 hsv_to_rgb_v(r_cp, rgb);
106 break;
107 }
108}
109
111{
112 if (but->rnaprop) {
114 return true;
115 }
116 }
117
118 return but->block->is_color_gamma_picker;
119}
120
122{
123 if (but->rnaprop) {
124 const PropertySubType prop_subtype = RNA_property_subtype(but->rnaprop);
125 if (ELEM(prop_subtype, PROP_COLOR, PROP_COLOR_GAMMA)) {
126 const int color_components_count = RNA_property_array_length(&but->rnapoin, but->rnaprop);
127 if (color_components_count == 4) {
128 return true;
129 }
130 }
131 }
132
133 return false;
134}
135
136static void scene_linear_to_perceptual_space(const bool is_gamma, float rgb[3])
137{
138 /* Map to color picking space for HSV values and HSV cube/circle,
139 * assuming it is more perceptually linear than the scene linear
140 * space for intuitive color picking. */
141 if (!is_gamma) {
144 }
145}
146
147static void perceptual_to_scene_linear_space(const bool is_gamma, float rgb[3])
148{
149 if (!is_gamma) {
152 }
153}
154
159
164
166
167/* -------------------------------------------------------------------- */
170
172 const bool is_gamma,
173 const bool is_editing_sliders,
174 const float rgb_scene_linear[3])
175{
176 /* Note that we skip updating values if we are editing the same number sliders.
177 * This avoids numerical drift from precision errors converting between color
178 * space and between RGB and HSV. */
179
180 /* Convert from RGB linear to RGB perceptual for number editing. */
181 if (cpicker->is_init == false ||
182 !(is_editing_sliders && g_color_picker_type == PICKER_TYPE_RGB &&
184 {
185 copy_v3_v3(cpicker->rgb_perceptual_slider, rgb_scene_linear);
187 }
188
189 /* Convert from RGB perceptual to HSV perceptual. */
190 if (cpicker->is_init == false) {
192 }
193 else if (!(is_editing_sliders && g_color_picker_type == PICKER_TYPE_HSV &&
195 {
197 }
198
199 /* Convert from RGB linear to HSV linear. */
200 if (cpicker->is_init == false) {
201 color_picker_rgb_to_hsv(rgb_scene_linear, cpicker->hsv_linear_slider);
202 }
203 else if (!(is_editing_sliders && g_color_picker_type == PICKER_TYPE_HSV &&
205 {
206 color_picker_rgb_to_hsv_compat(rgb_scene_linear, cpicker->hsv_linear_slider);
207 }
208
212
213 /* Convert from RGB to HSV in perceptually linear space for picker widgets. */
214 float rgb_perceptual_slider[3];
215 copy_v3_v3(rgb_perceptual_slider, rgb_scene_linear);
216 scene_linear_to_perceptual_space(is_gamma, rgb_perceptual_slider);
217
218 if (cpicker->is_init == false) {
219 color_picker_rgb_to_hsv(rgb_perceptual_slider, cpicker->hsv_perceptual);
221 }
222 else {
223 color_picker_rgb_to_hsv_compat(rgb_perceptual_slider, cpicker->hsv_perceptual);
224 }
225
226 cpicker->is_init = true;
227}
228
230{
231 float rgb_perceptual_slider[3];
232 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
233 float *hsv_perceptual = cpicker->hsv_perceptual;
234
235 color_picker_hsv_to_rgb(hsv_perceptual, rgb_perceptual_slider);
236
237 button_v3_set(but, rgb_perceptual_slider);
238}
239
240/* Updates all buttons who share the same color picker as the one passed. */
242 ColorPicker *cpicker,
243 const bool is_editing_sliders,
244 const float rgba_scene_linear[4])
245{
247 cpicker, block->is_color_gamma_picker, is_editing_sliders, rgba_scene_linear);
248
249 for (Button &bt : block->buttons()) {
250 if (bt.custom_data != cpicker) {
251 continue;
252 }
253
254 if (bt.rnaprop) {
255 button_v4_set(&bt, rgba_scene_linear);
256 /* original button that created the color picker already does undo
257 * push, so disable it on RNA buttons in the color picker block */
259 }
260 else if (bt.type == ButtonType::Text) {
261 /* Hex text input field. */
262 float rgba_hex[4];
263 uchar rgba_hex_uchar[4];
264 char col[16];
265
266 /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc...). */
267 copy_v4_v4(rgba_hex, rgba_scene_linear);
268 if (!block->is_color_gamma_picker) {
270 color_picker_rgb_round(rgba_hex);
271 }
272
273 rgba_float_to_uchar(rgba_hex_uchar, rgba_hex);
274
275 int col_len;
276 if (cpicker->has_alpha) {
277 col_len = SNPRINTF_UTF8_RLEN(
278 col, "#%02X%02X%02X%02X", UNPACK4_EX((uint), rgba_hex_uchar, ));
279 }
280 else {
281 col_len = SNPRINTF_UTF8_RLEN(col, "#%02X%02X%02X", UNPACK3_EX((uint), rgba_hex_uchar, ));
282 }
283 memcpy(bt.poin, col, col_len + 1); /* +1 offset for the # symbol. */
284 }
285
286 button_update(&bt);
287 }
288}
289
290static void colorpicker_rgba_update_cb(bContext * /*C*/, void *picker_bt1, void *prop_bt1)
291{
292 Button *picker_but = static_cast<Button *>(picker_bt1);
293 Block *block = picker_but->block;
294 PopupBlockHandle *popup = block->handle;
295 ColorPicker *cpicker = static_cast<ColorPicker *>(picker_but->custom_data);
296
297 Button *prop_but = static_cast<Button *>(prop_bt1);
298 PointerRNA ptr = prop_but->rnapoin;
299 PropertyRNA *prop = prop_but->rnaprop;
300
301 if (prop) {
302 float rgba_scene_linear[4];
303
304 zero_v4(rgba_scene_linear);
306 &ptr, prop, rgba_scene_linear, ARRAY_SIZE(rgba_scene_linear));
307 update_color_picker_buts_rgba(block, cpicker, false, rgba_scene_linear);
308 }
309
310 if (popup) {
311 popup->menuretval = RETURN_UPDATE;
312 }
313}
314
315static void colorpicker_hsv_perceptual_slider_update_cb(bContext * /*C*/, void *bt1, void *bt2)
316{
317 Button *but = static_cast<Button *>(bt1);
318 PopupBlockHandle *popup = but->block->handle;
319 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
320
321 /* Get RNA ptr/prop from the original color datablock button (bt2) since the HSV buttons (bt1)
322 * do not directly point to it. */
323 Button *prop_but = static_cast<Button *>(bt2);
324 PointerRNA ptr = prop_but->rnapoin;
325 PropertyRNA *prop = prop_but->rnaprop;
326 float rgba_scene_linear[4];
327
328 if (prop) {
329 zero_v4(rgba_scene_linear);
330 /* Get the current RGBA color for its (optional) Alpha component,
331 * then update RGB components from the current HSV values. */
333 &ptr, prop, rgba_scene_linear, ARRAY_SIZE(rgba_scene_linear));
335 copy_v3_v3(rgba_scene_linear, cpicker->rgb_perceptual_slider);
337 update_color_picker_buts_rgba(but->block, cpicker, true, rgba_scene_linear);
338 }
339
340 if (popup) {
341 popup->menuretval = RETURN_UPDATE;
342 }
343}
344
345static void colorpicker_hsv_linear_slider_update_cb(bContext * /*C*/, void *bt1, void *bt2)
346{
347 Button *but = static_cast<Button *>(bt1);
348 PopupBlockHandle *popup = but->block->handle;
349 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
350
351 /* Get RNA ptr/prop from the original color datablock button (bt2) since the HSV buttons (bt1)
352 * do not directly point to it. */
353 Button *prop_but = static_cast<Button *>(bt2);
354 PointerRNA ptr = prop_but->rnapoin;
355 PropertyRNA *prop = prop_but->rnaprop;
356 float rgba_scene_linear[4];
357
358 if (prop) {
359 zero_v4(rgba_scene_linear);
360 /* Get the current RGBA color for its (optional) Alpha component,
361 * then update RGB components from the current HSV values. */
363 &ptr, prop, rgba_scene_linear, ARRAY_SIZE(rgba_scene_linear));
364 color_picker_hsv_to_rgb(cpicker->hsv_linear_slider, rgba_scene_linear);
365 update_color_picker_buts_rgba(but->block, cpicker, true, rgba_scene_linear);
366 }
367
368 if (popup) {
369 popup->menuretval = RETURN_UPDATE;
370 }
371}
372
373static void colorpicker_rgb_perceptual_slider_update_cb(bContext * /*C*/, void *bt1, void *bt2)
374{
375 Button *but = static_cast<Button *>(bt1);
376 PopupBlockHandle *popup = but->block->handle;
377 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
378
379 /* Get RNA ptr/prop from the original color datablock button (bt2) since the HSV buttons (bt1)
380 * do not directly point to it. */
381 Button *prop_but = static_cast<Button *>(bt2);
382 PointerRNA ptr = prop_but->rnapoin;
383 PropertyRNA *prop = prop_but->rnaprop;
384 float rgba_scene_linear[4];
385
386 if (prop) {
387 zero_v4(rgba_scene_linear);
388 /* Get the current RGBA color for its (optional) Alpha component,
389 * then update RGB components from the current HSV values. */
391 &ptr, prop, rgba_scene_linear, ARRAY_SIZE(rgba_scene_linear));
392 copy_v3_v3(rgba_scene_linear, cpicker->rgb_perceptual_slider);
395 update_color_picker_buts_rgba(but->block, cpicker, true, rgba_scene_linear);
396 }
397
398 if (popup) {
399 popup->menuretval = RETURN_UPDATE;
400 }
401}
402
403static void colorpicker_hex_rna_cb(bContext * /*C*/, void *bt1, void *bt2)
404{
405 Button *but = static_cast<Button *>(bt1);
406 PopupBlockHandle *popup = but->block->handle;
407 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
408 char hexcol[128];
409 button_string_get(but, hexcol, ARRAY_SIZE(hexcol));
410
411 /* In case the current color contains an Alpha component but the Hex string does not, get the
412 * current color to preserve the Alpha component.
413 * Like #colorpicker_hsv_perceptual_slider_update_cb, the original color datablock button
414 * (bt2) is used since Hex Text Field button (bt1) doesn't directly point to it. */
415 Button *prop_but = static_cast<Button *>(bt2);
416 PointerRNA ptr = prop_but->rnapoin;
417 PropertyRNA *prop = prop_but->rnaprop;
418
419 float rgba[4];
420 if (prop) {
421 zero_v4(rgba);
422 RNA_property_float_get_array_at_most(&ptr, prop, rgba, ARRAY_SIZE(rgba));
423 }
424 /* Override the current color with the parsed Hex string, preserving the original Alpha if the
425 * hex string doesn't contain it. */
426 const bool is_parsed = hex_to_rgba(hexcol, rgba, rgba + 1, rgba + 2, rgba + 3);
427
428 /* Hex code is assumed to be in sRGB space (coming from other applications, web, etc...).
429 * Only apply conversion if the hex string was successfully parsed. */
430 if (is_parsed && !button_is_color_gamma(but)) {
433 }
434
435 update_color_picker_buts_rgba(but->block, cpicker, false, rgba);
436
437 if (popup) {
438 popup->menuretval = RETURN_UPDATE;
439 }
440}
441
442static void popup_close_cb(bContext * /*C*/, void *bt1, void * /*arg*/)
443{
444 Button *but = static_cast<Button *>(bt1);
445 PopupBlockHandle *popup = but->block->handle;
446
447 if (popup) {
448 ColorPicker *cpicker = static_cast<ColorPicker *>(but->custom_data);
449 BLI_assert(cpicker->is_init);
450 popup->menuretval = (equals_v3v3(cpicker->hsv_perceptual, cpicker->hsv_perceptual_init) ?
452 RETURN_OK);
453 }
454}
455
457{
459 const ePickerSpace space = (block->is_color_gamma_picker) ? (type == PICKER_TYPE_RGB) ?
463
464 /* tag buttons */
465 for (Button &bt : block->buttons()) {
466 if ((bt.func == colorpicker_rgba_update_cb) && (bt.type == ButtonType::NumSlider) &&
467 (bt.rnaindex != 3))
468 {
469 /* RGB sliders (color circle and alpha are always shown) */
471 bt.flag, !(type == PICKER_TYPE_RGB && space == PICKER_SPACE_LINEAR), UI_HIDDEN);
472 }
474 /* HSV sliders */
476 bt.flag, !(type == PICKER_TYPE_RGB && space == PICKER_SPACE_PERCEPTUAL), UI_HIDDEN);
477 }
479 /* HSV sliders */
481 bt.flag, !(type == PICKER_TYPE_HSV && space == PICKER_SPACE_PERCEPTUAL), UI_HIDDEN);
482 }
483 else if (bt.func == colorpicker_hsv_linear_slider_update_cb) {
484 /* HSV sliders */
486 bt.flag, !(type == PICKER_TYPE_HSV && space == PICKER_SPACE_LINEAR), UI_HIDDEN);
487 }
488 }
489}
490
491static void colorpicker_update_type_space_cb(bContext * /*C*/, void *picker_bt1, void *prop_bt1)
492{
493 Button *picker_but = static_cast<Button *>(picker_bt1);
494 Block *block = picker_but->block;
495 ColorPicker *cpicker = static_cast<ColorPicker *>(picker_but->custom_data);
496
497 Button *prop_but = static_cast<Button *>(prop_bt1);
498 PointerRNA ptr = prop_but->rnapoin;
499 PropertyRNA *prop = prop_but->rnaprop;
500
501 float rgba_scene_linear[4];
502
503 zero_v4(rgba_scene_linear);
505 &ptr, prop, rgba_scene_linear, ARRAY_SIZE(rgba_scene_linear));
506 update_color_picker_buts_rgba(block, cpicker, false, rgba_scene_linear);
507
508 colorpicker_hide_reveal(picker_but->block);
509}
510
511#define PICKER_TOTAL_W (180.0f * UI_SCALE_FAC)
512#define PICKER_BAR ((8.0f * UI_SCALE_FAC) + (6 * U.pixelsize))
513#define PICKER_SPACE (8.0f * UI_SCALE_FAC)
514#define PICKER_W (PICKER_TOTAL_W - PICKER_BAR - PICKER_SPACE)
515#define PICKER_H PICKER_W
516
523 const char **r_area_tooltip,
524 const char **r_slider_tooltip)
525{
526 const char *name_hue = CTX_TIP_(BLT_I18NCONTEXT_COLOR, "Hue");
527 const char *name_sat = CTX_TIP_(BLT_I18NCONTEXT_COLOR, "Saturation");
528 const char *name_val = CTX_TIP_(BLT_I18NCONTEXT_COLOR, "Value");
529 const char *name_light = CTX_TIP_(BLT_I18NCONTEXT_COLOR, "Lightness");
530 const char *name_color = CTX_TIP_(BLT_I18NCONTEXT_COLOR, "Color");
531
532 const char *axis_x = nullptr;
533 const char *axis_y = nullptr;
534 const char *slider = nullptr;
535
536 switch (U.color_picker_type) {
538 axis_x = name_hue;
539 axis_y = name_sat;
540 slider = name_light;
541 break;
543 axis_x = name_hue;
544 axis_y = name_sat;
545 slider = name_val;
546 break;
548 axis_x = name_sat;
549 axis_y = name_val;
550 slider = name_hue;
551 break;
553 axis_x = name_hue;
554 axis_y = name_sat;
555 slider = name_val;
556 break;
558 axis_x = name_hue;
559 axis_y = name_val;
560 slider = name_sat;
561 break;
562 default:
563 *r_area_tooltip = name_color;
564 *r_slider_tooltip = name_val;
565 return;
566 }
567
568 SNPRINTF_UTF8(cpicker->tooltip_area, "%s/%s", axis_x, axis_y);
569
570 *r_area_tooltip = cpicker->tooltip_area;
571 *r_slider_tooltip = slider;
572}
573
574static void colorpicker_circle(Block *block,
575 PointerRNA *ptr,
576 PropertyRNA *prop,
577 ColorPicker *cpicker)
578{
579 Button *bt;
580 ButtonHSVCube *hsv_but;
581 const char *circle_tooltip;
582 const char *slider_tooltip;
583
584 colorpicker_tooltips(cpicker, &circle_tooltip, &slider_tooltip);
585
586 /* Color circle (Hue/Saturation) */
587 bt = uiDefButR_prop(block,
589 "",
590 0,
591 0,
592 PICKER_H,
593 PICKER_W,
594 ptr,
595 prop,
596 -1,
597 0.0,
598 0.0,
599 circle_tooltip);
601 bt->custom_data = cpicker;
602
603 /* Slider (Lightness or Value, depending on color picker type) */
604 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
606 "",
608 0,
610 PICKER_H,
611 ptr,
612 prop,
613 -1,
614 0.0,
615 0.0,
616 slider_tooltip));
617 hsv_but->gradient_type = (U.color_picker_type == USER_CP_CIRCLE_HSL) ? GRAD_L_ALT : GRAD_V_ALT;
618 button_func_set(hsv_but, colorpicker_rgba_update_cb, hsv_but, hsv_but);
619 hsv_but->custom_data = cpicker;
620}
621
623 Block *block, PointerRNA *ptr, PropertyRNA *prop, eButGradientType type, ColorPicker *cpicker)
624{
625 ButtonHSVCube *hsv_but;
626 const char *square_tooltip;
627 const char *slider_tooltip;
628
629 colorpicker_tooltips(cpicker, &square_tooltip, &slider_tooltip);
630
631 BLI_assert(type <= GRAD_HS);
632
633 /* Color square (SV, HS or HV, depending on color picker type) */
634 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
636 "",
637 0,
640 PICKER_H,
641 ptr,
642 prop,
643 -1,
644 0.0,
645 0.0,
646 square_tooltip));
647 hsv_but->gradient_type = type;
648 button_func_set(hsv_but, colorpicker_rgba_update_cb, hsv_but, hsv_but);
649 hsv_but->custom_data = cpicker;
650
651 /* Slider (Hue, Saturation or Value, depending on color picker type) */
652 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
654 "",
655 0,
656 0,
659 ptr,
660 prop,
661 -1,
662 0.0,
663 0.0,
664 slider_tooltip));
665 hsv_but->gradient_type = eButGradientType(type + 3);
666 button_func_set(hsv_but, colorpicker_rgba_update_cb, hsv_but, hsv_but);
667 hsv_but->custom_data = cpicker;
668}
669
670/* a HS circle, V slider, rgb/hsv/hex sliders */
671static void block_colorpicker(const bContext * /*C*/,
672 Block *block,
673 Button *from_but,
674 float rgba_scene_linear[4],
675 bool show_picker)
676{
677 /* ePickerType */
678 Button *bt;
679 int picker_width;
680 float softmin, softmax, hardmin, hardmax, step, precision;
681 ColorPicker *cpicker = block_colorpicker_create(block);
682 PointerRNA *ptr = &from_but->rnapoin;
683 PropertyRNA *prop = from_but->rnaprop;
684
685 picker_width = PICKER_TOTAL_W;
686
687 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
688 RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
689 RNA_property_float_get_array_at_most(ptr, prop, rgba_scene_linear, 4);
690
692 cpicker, block->is_color_gamma_picker, false, rgba_scene_linear);
693 cpicker->has_alpha = button_color_has_alpha(from_but);
694
695 /* when the softmax isn't defined in the RNA,
696 * using very large numbers causes sRGB/linear round trip to fail. */
697 if (softmax == FLT_MAX) {
698 softmax = 1.0f;
699 }
700
701 switch (U.color_picker_type) {
703 colorpicker_square(block, ptr, prop, GRAD_SV, cpicker);
704 break;
706 colorpicker_square(block, ptr, prop, GRAD_HS, cpicker);
707 break;
709 colorpicker_square(block, ptr, prop, GRAD_HV, cpicker);
710 break;
711
712 /* user default */
715 default:
716 colorpicker_circle(block, ptr, prop, cpicker);
717 break;
718 }
719
720 /* mode */
721 int yco = -0.5f * UI_UNIT_Y;
722
723 if (!block->is_color_gamma_picker) {
724 auto colorspace_tip_func = [](bContext & /*C*/, TooltipData &tip, Button *but, void *space) {
725 tooltip_text_field_add(tip, but->tip, {}, TIP_STYLE_HEADER, TIP_LC_NORMAL, false);
727 TIP_("Color Space: ") + std::string(static_cast<const char *>(space)),
728 {},
731 false);
732 };
733
734 block_align_begin(block);
735
736 bt = uiDefButV(block,
738 IFACE_("Linear"),
739 0,
740 yco -= UI_UNIT_Y,
741 picker_width * 0.5,
742 UI_UNIT_Y,
744 0.0,
745 float(PICKER_TYPE_RGB),
746 TIP_("Scene linear values in the working color space"));
751 bt,
752 colorspace_tip_func,
754 nullptr);
755 bt->custom_data = cpicker;
756
757 bt = uiDefButV(block,
759 IFACE_("Perceptual"),
760 picker_width * 0.5,
761 yco,
762 picker_width * 0.5,
763 UI_UNIT_Y,
765 0.0,
766 float(PICKER_TYPE_HSV),
767 TIP_("Perceptually uniform values, matching the color picker"));
772 bt,
773 colorspace_tip_func,
775 nullptr);
776
777 bt->custom_data = cpicker;
778
779 block_align_end(block);
780
781 yco -= 0.5f * UI_UNIT_X;
782 }
783
784 block_align_begin(block);
785
786 bt = uiDefButV(block,
788 IFACE_("RGB"),
789 0,
790 yco -= UI_UNIT_Y,
791 picker_width * 0.5,
792 UI_UNIT_Y,
794 0.0,
795 float(PICKER_TYPE_RGB),
796 TIP_("RGB values"));
800 bt->custom_data = cpicker;
801
802 bt = uiDefButV(block,
804 (U.color_picker_type == USER_CP_CIRCLE_HSL) ? IFACE_("HSL") : IFACE_("HSV"),
805 picker_width * 0.5,
806 yco,
807 picker_width * 0.5,
808 UI_UNIT_Y,
810 0.0,
811 float(PICKER_TYPE_HSV),
812 (U.color_picker_type == USER_CP_CIRCLE_HSL) ? TIP_("Hue, Saturation, Lightness") :
813 TIP_("Hue, Saturation, Value"));
817 bt->custom_data = cpicker;
818
819 block_align_end(block);
820
821 const int slider_yco = yco - 1.1f * UI_UNIT_Y;
822
823 /* NOTE: don't disable BUT_UNDO for RGBA values, since these don't add undo steps. */
824
825 /* RGB values */
826 block_align_begin(block);
827 const auto add_rgb_perceptual_slider =
828 [&](const char *str, const char *tip, const int index, const int y) {
829 bt = uiDefButR_prop(block,
831 str,
832 0,
833 y,
834 picker_width,
835 UI_UNIT_Y,
836 ptr,
837 prop,
838 index,
839 0.0,
840 0.0,
841 tip);
845 bt->custom_data = cpicker;
846 };
847
848 yco = slider_yco;
849 add_rgb_perceptual_slider(IFACE_("Red:"), TIP_("Red"), 0, yco);
850 add_rgb_perceptual_slider(IFACE_("Green:"), TIP_("Green"), 1, yco -= UI_UNIT_Y);
851 add_rgb_perceptual_slider(IFACE_("Blue:"), TIP_("Blue"), 2, yco -= UI_UNIT_Y);
852
853 /* HSV values */
854 const auto add_hsv_perceptual_slider =
855 [&](const char *str, const char *tip, const int index, const int y, const bool linear) {
856 float *hsv_values = linear ? cpicker->hsv_linear_slider : cpicker->hsv_perceptual_slider;
857 bt = uiDefButV(block,
859 str,
860 0,
861 y,
862 picker_width,
863 UI_UNIT_Y,
864 hsv_values + index,
865 0.0,
866 1.0,
867 tip);
868 if (index == 2) {
869 bt->hardmax = hardmax; /* Not common but RGB may be over 1.0. */
870 }
877 bt,
878 from_but);
879 bt->custom_data = cpicker;
880 };
881
882 yco = slider_yco;
883 add_hsv_perceptual_slider(IFACE_("Hue:"), TIP_("Hue"), 0, yco, !block->is_color_gamma_picker);
884 add_hsv_perceptual_slider(IFACE_("Saturation:"),
885 TIP_("Saturation"),
886 1,
887 yco -= UI_UNIT_Y,
888 !block->is_color_gamma_picker);
889 if (U.color_picker_type == USER_CP_CIRCLE_HSL) {
890 add_hsv_perceptual_slider(IFACE_("Lightness:"),
891 TIP_("Lightness"),
892 2,
893 yco -= UI_UNIT_Y,
894 !block->is_color_gamma_picker);
895 }
896 else {
897 add_hsv_perceptual_slider(CTX_IFACE_(BLT_I18NCONTEXT_COLOR, "Value:"),
899 2,
900 yco -= UI_UNIT_Y,
901 !block->is_color_gamma_picker);
902 }
903
904 /* Could use:
905 * col->prop(ptr, prop, -1, 0,ui::ITEM_R_EXPAND |ui::ITEM_R_SLIDER, "", ICON_NONE);
906 * but need to use button_func_set for updating other fake buttons */
907
908 if (!block->is_color_gamma_picker) {
909 yco = slider_yco;
910
911 /* Display RGB values */
912 const auto add_rgb_perceptual_slider =
913 [&](const char *str, const char *tip, const int index, const int y) {
914 bt = uiDefButV(block,
916 str,
917 0,
918 y,
919 picker_width,
920 UI_UNIT_Y,
921 cpicker->rgb_perceptual_slider + index,
922 hardmin,
923 hardmax,
924 tip);
927 bt->softmin = softmin;
928 bt->softmax = softmax;
931 bt->custom_data = cpicker;
932 };
933
934 add_rgb_perceptual_slider(IFACE_("Red:"), TIP_("Red"), 0, yco);
935 add_rgb_perceptual_slider(IFACE_("Green:"), TIP_("Green"), 1, yco -= UI_UNIT_Y);
936 add_rgb_perceptual_slider(IFACE_("Blue:"), TIP_("Blue"), 2, yco -= UI_UNIT_Y);
937
938 yco = slider_yco;
939 add_hsv_perceptual_slider(IFACE_("Hue:"), TIP_("Hue"), 0, yco, false);
940 add_hsv_perceptual_slider(
941 IFACE_("Saturation:"), TIP_("Saturation"), 1, yco -= UI_UNIT_Y, false);
942 if (U.color_picker_type == USER_CP_CIRCLE_HSL) {
943 add_hsv_perceptual_slider(
944 IFACE_("Lightness:"), TIP_("Lightness"), 2, yco -= UI_UNIT_Y, false);
945 }
946 else {
947 add_hsv_perceptual_slider(CTX_IFACE_(BLT_I18NCONTEXT_COLOR, "Value:"),
949 2,
950 yco -= UI_UNIT_Y,
951 false);
952 }
953 }
954
955 if (cpicker->has_alpha) {
956 bt = uiDefButR_prop(block,
958 IFACE_("Alpha:"),
959 0,
960 yco -= UI_UNIT_Y,
961 picker_width,
962 UI_UNIT_Y,
963 ptr,
964 prop,
965 3,
966 0.0,
967 0.0,
968 TIP_("Alpha"));
972 bt->custom_data = cpicker;
973 }
974 else {
975 rgba_scene_linear[3] = 1.0f;
976 }
977
978 block_align_end(block);
979
980 /* Hex color is in sRGB space. */
981 float rgba_hex[4];
982 uchar rgba_hex_uchar[4];
983
984 copy_v4_v4(rgba_hex, rgba_scene_linear);
985
986 if (!button_is_color_gamma(from_but)) {
988 color_picker_rgb_round(rgba_hex);
989 }
990
991 rgba_float_to_uchar(rgba_hex_uchar, rgba_hex);
992
993 if (cpicker->has_alpha) {
994 SNPRINTF_UTF8(cpicker->hexcol, "#%02X%02X%02X%02X", UNPACK4_EX((uint), rgba_hex_uchar, ));
995 }
996 else {
997 SNPRINTF_UTF8(cpicker->hexcol, "#%02X%02X%02X", UNPACK3_EX((uint), rgba_hex_uchar, ));
998 }
999
1000 yco -= UI_UNIT_Y * 1.5f;
1001
1002 const int label_width = picker_width * 0.15f;
1003 const int eyedropper_offset = show_picker ? UI_UNIT_X * 1.25f : 0;
1004 const int text_width = picker_width - label_width - eyedropper_offset;
1005
1006 uiDefBut(block,
1008 IFACE_("Hex"),
1009 0,
1010 yco,
1011 label_width,
1012 UI_UNIT_Y,
1013 nullptr,
1014 0.0,
1015 0.0,
1016 std::nullopt);
1017
1018 bt = uiDefBut(block,
1020 "",
1021 label_width,
1022 yco,
1023 text_width,
1024 UI_UNIT_Y,
1025 cpicker->hexcol,
1026 0,
1027 cpicker->has_alpha ? 10 : 8,
1028 std::nullopt);
1029 const auto bt_tooltip_func =
1030 [](bContext & /*C*/, TooltipData &tip, Button * /*but*/, void *has_alpha_ptr) {
1031 const bool *has_alpha = static_cast<bool *>(has_alpha_ptr);
1032 if (*has_alpha) {
1034 TIP_("Hex triplet for color with alpha (#RRGGBBAA)."),
1035 {},
1038 false);
1039 }
1040 else {
1042 TIP_("Hex triplet for color (#RRGGBB)."),
1043 {},
1046 false);
1047 }
1049 tip, TIP_("Gamma corrected"), {}, TIP_STYLE_NORMAL, TIP_LC_NORMAL, false);
1050 };
1052 bt, bt_tooltip_func, static_cast<void *>(&cpicker->has_alpha), nullptr);
1054 button_func_set(bt, colorpicker_hex_rna_cb, bt, from_but);
1055 bt->custom_data = cpicker;
1056
1057 if (show_picker) {
1058 bt = uiDefIconButO(block,
1060 "UI_OT_eyedropper_color",
1062 ICON_EYEDROPPER,
1063 picker_width - UI_UNIT_X,
1064 yco,
1065 UI_UNIT_X,
1066 UI_UNIT_Y,
1067 std::nullopt);
1070 button_func_set(bt, popup_close_cb, bt, nullptr);
1071 bt->custom_data = cpicker;
1072 }
1073
1075}
1076
1077static int colorpicker_wheel_cb(const bContext * /*C*/, Block *block, const wmEvent *event)
1078{
1079 PopupBlockHandle *popup = block->handle;
1080 bool mouse_in_region = popup && BLI_rcti_isect_pt(&popup->region->winrct,
1081 float(event->xy[0]),
1082 float(event->xy[1]));
1083
1084 if (popup && !mouse_in_region && (ISMOUSE_WHEEL(event->type) || event->type == MOUSEPAN)) {
1085 /* Exit and save color if moving mouse wheel or trackpad panning while outside the popup. */
1086 popup->menuretval = RETURN_OK;
1087 return 1;
1088 }
1089
1090 /* Increase/Decrease the Color HSV Value component using the mouse wheel. */
1091 float add = 0.0f;
1092
1093 switch (event->type) {
1094 case WHEELUPMOUSE:
1095 add = 0.05f;
1096 break;
1097 case WHEELDOWNMOUSE:
1098 add = -0.05f;
1099 break;
1100 case MOUSEPAN:
1101 add = 0.005f * WM_event_absolute_delta_y(event) / UI_SCALE_FAC;
1102 break;
1103 default:
1104 break;
1105 }
1106
1107 if (add != 0.0f) {
1108 for (Button &but : block->buttons()) {
1109 if (but.type == ButtonType::HsvCube && but.active == nullptr) {
1110 ColorPicker *cpicker = static_cast<ColorPicker *>(but.custom_data);
1111 float *hsv_perceptual = cpicker->hsv_perceptual;
1112
1113 /* Get the RGBA Color. */
1114 float rgba_perceptual[4];
1115 button_v4_get(&but, rgba_perceptual);
1117
1118 /* Convert it to HSV. */
1119 color_picker_rgb_to_hsv_compat(rgba_perceptual, hsv_perceptual);
1120
1121 /* Increment/Decrement its value from mouse wheel input. */
1122 hsv_perceptual[2] = clamp_f(hsv_perceptual[2] + add, 0.0f, 1.0f);
1123
1124 /* Convert it to linear space RGBA, and apply it back to the button. */
1125 float rgba_scene_linear[4];
1126 rgba_scene_linear[3] = rgba_perceptual[3]; /* Transfer Alpha component. */
1127 color_picker_hsv_to_rgb(hsv_perceptual, rgba_scene_linear);
1128 perceptual_to_scene_linear_space(&but, rgba_scene_linear);
1129 button_v4_set(&but, rgba_scene_linear);
1130
1131 /* Update all other Color Picker buttons to reflect the color change. */
1132 update_color_picker_buts_rgba(block, cpicker, false, rgba_scene_linear);
1133 if (popup) {
1134 popup->menuretval = RETURN_UPDATE;
1135 }
1136
1137 return 1;
1138 }
1139 }
1140 }
1141 return 0;
1142}
1143
1145{
1146 Button *but = static_cast<Button *>(arg_but);
1147 Block *block;
1148
1149 block = block_begin(C, handle->region, __func__, EmbossType::Emboss);
1150
1151 if (button_is_color_gamma(but)) {
1152 block->is_color_gamma_picker = true;
1153 }
1154
1155 copy_v3_v3(handle->retvec, but->editvec);
1156
1157 block_colorpicker(C, block, but, handle->retvec, true);
1158
1161 block_bounds_set_normal(block, 0.5 * UI_UNIT_X);
1162
1164 block->direction = UI_DIR_UP;
1165
1166 return block;
1167}
1168
1170{
1171 ColorPicker *cpicker = MEM_new_zeroed<ColorPicker>(__func__);
1172 BLI_addhead(&block->color_pickers.list, cpicker);
1173
1174 return cpicker;
1175}
1176
1178
1179} // namespace blender::ui
#define SNPRINTF_UTF8(dst, format,...)
#define SNPRINTF_UTF8_RLEN(dst, format,...)
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define CTX_TIP_(context, msgid)
#define BLT_I18NCONTEXT_COLOR
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define UNPACK3_EX(pre, a, post)
#define UNPACK4_EX(pre, a, post)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
#define str(s)
#define FLT_MAX
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
static void colorpicker_update_type_space_cb(bContext *, void *picker_bt1, void *prop_bt1)
static void block_colorpicker(const bContext *, Block *block, Button *from_but, float rgba_scene_linear[4], bool show_picker)
Block * block_func_COLOR(bContext *C, PopupBlockHandle *handle, void *arg_but)
static void colorpicker_hex_rna_cb(bContext *, void *bt1, void *bt2)
void perceptual_to_scene_linear_space(Button *but, float rgb[3])
void button_v4_set(Button *but, const float vec[4])
void button_string_get(Button *but, char *str, const size_t str_maxncpy)
void button_func_set(Button *but, std::function< void(bContext &)> func)
void button_v4_get(Button *but, float vec[4])
void block_bounds_set_normal(Block *block, int addval)
Definition interface.cc:651
static void colorpicker_square(Block *block, PointerRNA *ptr, PropertyRNA *prop, eButGradientType type, ColorPicker *cpicker)
static void colorpicker_hsv_perceptual_slider_update_cb(bContext *, void *bt1, void *bt2)
void block_align_begin(Block *block)
static void colorpicker_hide_reveal(Block *block)
static int colorpicker_wheel_cb(const bContext *, Block *block, const wmEvent *event)
static void colorpicker_circle(Block *block, PointerRNA *ptr, PropertyRNA *prop, ColorPicker *cpicker)
static void update_color_picker_buts_rgba(Block *block, ColorPicker *cpicker, const bool is_editing_sliders, const float rgba_scene_linear[4])
Button * uiDefIconButO(Block *block, ButtonType type, StringRefNull opname, wm::OpCallContext opcontext, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
ColorPicker * block_colorpicker_create(Block *block)
void button_func_tooltip_custom_set(Button *but, ButtonToolTipCustomFunc func, void *arg, FreeArgFunc free_arg)
void scene_linear_to_perceptual_space(Button *but, float rgb[3])
bool button_color_has_alpha(Button *but)
void color_picker_rgb_to_hsv(const float rgb[3], float r_cp[3])
static void popup_close_cb(bContext *, void *bt1, void *)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
static void color_picker_rgb_round(float rgb[3])
static void colorpicker_tooltips(ColorPicker *cpicker, const char **r_area_tooltip, const char **r_slider_tooltip)
Button * uiDefButR_prop(Block *block, ButtonType type, std::optional< StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< StringRef > tip)
void button_number_slider_precision_set(Button *but, float precision)
void block_theme_style_set(Block *block, char theme_style)
static void colorpicker_rgb_perceptual_slider_update_cb(bContext *, void *bt1, void *bt2)
void block_align_end(Block *block)
void tooltip_text_field_add(TooltipData &data, std::string text, std::string suffix, const TooltipStyle style, const TooltipColorID color_id, const bool is_pad=false)
void button_flag_disable(Button *but, int64_t flag)
void color_picker_hsv_to_rgb(const float r_cp[3], float rgb[3])
void button_drawflag_disable(Button *but, int flag)
bool button_is_color_gamma(Button &but)
void button_number_slider_step_size_set(Button *but, float step_size)
void button_v3_set(Button *but, const float vec[3])
Button * uiDefButV(Block *block, ButtonType type, StringRef str, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
static void colorpicker_rgba_update_cb(bContext *, void *picker_bt1, void *prop_bt1)
static void color_picker_update_from_rgb_linear(ColorPicker *cpicker, const bool is_gamma, const bool is_editing_sliders, const float rgb_scene_linear[3])
void button_hsv_set(Button *but)
void button_update(Button *but)
void color_picker_rgb_to_hsv_compat(const float rgb[3], float r_cp[3])
static void colorpicker_hsv_linear_slider_update_cb(bContext *, void *bt1, void *bt2)
UserDef U
Definition blender.cc:58
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_property_float_get_array_at_most(PointerRNA *ptr, PropertyRNA *prop, float *values, int values_num)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
void rgb_to_hsl_v(const float rgb[3], float r_hsl[3])
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
void rgb_to_hsv_compat_v(const float rgb[3], float r_hsv[3])
MINLINE void zero_v4(float r[4])
MINLINE bool equals_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
void IMB_colormanagement_scene_linear_to_color_picking_v3(float color_picking[3], const float scene_linear[3])
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned char uchar
Definition BKE_text.h:58
const char * IMB_colormanagement_role_colorspace_name_get(int role)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
BLI_INLINE void IMB_colormanagement_srgb_to_scene_linear_v3(float scene_linear[3], const float srgb[3])
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void rgb_to_hsl_compat_v(const float rgb[3], float r_hsl[3])
void hsl_to_rgb_v(const float hsl[3], float r_rgb[3])
Definition math_color.cc:66
PropertySubType RNA_property_subtype(PropertyRNA *prop)
bool hex_to_rgba(const char *hexcol, float *r_r, float *r_g, float *r_b, float *r_a)
void IMB_colormanagement_color_picking_to_scene_linear_v3(float scene_linear[3], const float color_picking[3])
int WM_event_absolute_delta_y(const wmEvent *event)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
bool BLI_rcti_isect_pt(const struct rcti *rect, int x, int y)
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
@ PROP_COLOR_GAMMA
Definition RNA_types.hh:329
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
void RNA_property_float_range(PointerRNA *ptr, PropertyRNA *prop, float *hardmin, float *hardmax)
MINLINE float clamp_f(float value, float min, float max)
#define fabsf
int(*) block_event_func(const bContext *C, Block *, const wmEvent *)
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
PopupBlockHandle * handle
ColorPickerData color_pickers
ListBaseT< ColorPicker > list
wmEventType type
Definition WM_types.hh:817
#define ISMOUSE_WHEEL(event_type)