Blender V5.0
interface_template_color_picker.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_listbase.h"
10#include "BLI_math_vector.h"
11#include "BLI_string_ref.hh"
12
13#include "BLT_translation.hh"
14
15#include "DNA_brush_types.h"
16
17#include "RNA_access.hh"
18#include "RNA_prototypes.hh"
19
21#include "interface_intern.hh"
22
24
25#define WHEEL_SIZE (5 * U.widget_unit)
26
29 const StringRefNull propname,
30 bool value_slider,
31 bool lock,
32 bool lock_luminosity,
33 bool cubic)
34{
35 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
36 uiBlock *block = layout->block();
38
39 if (!prop) {
40 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
41 return;
42 }
43
44 float softmin, softmax, step, precision;
45 RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
46
47 uiLayout *col = &layout->column(true);
48 uiLayout *row = &col->row(true);
49
50 uiBut *but = nullptr;
51 uiButHSVCube *hsv_but;
52 switch (U.color_picker_type) {
56 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
58 0,
59 "",
60 0,
61 0,
64 ptr,
65 prop,
66 -1,
67 0.0,
68 0.0,
69 "");
70 switch (U.color_picker_type) {
72 hsv_but->gradient_type = UI_GRAD_SV;
73 break;
75 hsv_but->gradient_type = UI_GRAD_HS;
76 break;
78 hsv_but->gradient_type = UI_GRAD_HV;
79 break;
80 }
81 but = hsv_but;
82 break;
83
84 /* user default */
87 default:
88 but = uiDefButR_prop(block,
90 0,
91 "",
92 0,
93 0,
96 ptr,
97 prop,
98 -1,
99 0.0,
100 0.0,
101 "");
102 break;
103 }
104
105 but->custom_data = cpicker;
106
107 cpicker->use_color_lock = lock;
108 cpicker->use_color_cubic = cubic;
109 cpicker->use_luminosity_lock = lock_luminosity;
110
111 if (lock_luminosity) {
112 float color[4]; /* in case of alpha */
113 RNA_property_float_get_array(ptr, prop, color);
114 cpicker->luminosity_lock_value = len_v3(color);
115 }
116
117 if (value_slider) {
118 switch (U.color_picker_type) {
120 row->separator();
121 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
123 0,
124 "",
125 WHEEL_SIZE + 6,
126 0,
127 14 * UI_SCALE_FAC,
129 ptr,
130 prop,
131 -1,
132 softmin,
133 softmax,
134 "");
135 hsv_but->gradient_type = UI_GRAD_L_ALT;
136 break;
138 col->separator();
139 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
141 0,
142 "",
143 0,
144 4,
146 18 * UI_SCALE_FAC,
147 ptr,
148 prop,
149 -1,
150 softmin,
151 softmax,
152 "");
154 break;
156 col->separator();
157 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
159 0,
160 "",
161 0,
162 4,
164 18 * UI_SCALE_FAC,
165 ptr,
166 prop,
167 -1,
168 softmin,
169 softmax,
170 "");
172 break;
174 col->separator();
175 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
177 0,
178 "",
179 0,
180 4,
182 18 * UI_SCALE_FAC,
183 ptr,
184 prop,
185 -1,
186 softmin,
187 softmax,
188 "");
190 break;
191
192 /* user default */
194 default:
195 row->separator();
196 hsv_but = (uiButHSVCube *)uiDefButR_prop(block,
198 0,
199 "",
200 WHEEL_SIZE + 6,
201 0,
202 14 * UI_SCALE_FAC,
204 ptr,
205 prop,
206 -1,
207 softmin,
208 softmax,
209 "");
210 hsv_but->gradient_type = UI_GRAD_V_ALT;
211 break;
212 }
213
214 hsv_but->custom_data = cpicker;
215 }
216}
217
218static void ui_template_palette_menu(bContext * /*C*/, uiLayout *layout, void * /*but_p*/)
219{
220 uiLayout *row;
221
222 layout->label(IFACE_("Sort By:"), ICON_NONE);
223 row = &layout->row(false);
224 PointerRNA op_ptr = row->op("PALETTE_OT_sort", IFACE_("Hue"), ICON_NONE);
225 RNA_enum_set(&op_ptr, "type", 1);
226 row = &layout->row(false);
227 op_ptr = row->op("PALETTE_OT_sort", IFACE_("Saturation"), ICON_NONE);
228 RNA_enum_set(&op_ptr, "type", 2);
229 row = &layout->row(false);
230 op_ptr = row->op("PALETTE_OT_sort", CTX_IFACE_(BLT_I18NCONTEXT_COLOR, "Value"), ICON_NONE);
231 RNA_enum_set(&op_ptr, "type", 3);
232 row = &layout->row(false);
233 op_ptr = row->op("PALETTE_OT_sort", IFACE_("Luminance"), ICON_NONE);
234 RNA_enum_set(&op_ptr, "type", 4);
235}
236
239 const StringRefNull propname,
240 bool /*colors*/)
241{
242 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
243 uiBut *but = nullptr;
244
245 const int cols_per_row = std::max(layout->width() / UI_UNIT_X, 1);
246
247 if (!prop) {
248 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
249 return;
250 }
251
252 const PointerRNA cptr = RNA_property_pointer_get(ptr, prop);
253 if (!cptr.data || !RNA_struct_is_a(cptr.type, &RNA_Palette)) {
254 return;
255 }
256
257 uiBlock *block = layout->block();
258
259 Palette *palette = static_cast<Palette *>(cptr.data);
260
261 uiLayout *col = &layout->column(true);
262 col->row(true);
263 uiDefIconButO(block,
265 "PALETTE_OT_color_add",
267 ICON_ADD,
268 0,
269 0,
270 UI_UNIT_X,
271 UI_UNIT_Y,
272 std::nullopt);
273 uiDefIconButO(block,
275 "PALETTE_OT_color_delete",
277 ICON_REMOVE,
278 0,
279 0,
280 UI_UNIT_X,
281 UI_UNIT_Y,
282 std::nullopt);
283 if (palette->colors.first != nullptr) {
284 but = uiDefIconButO(block,
286 "PALETTE_OT_color_move",
288 ICON_TRIA_UP,
289 0,
290 0,
291 UI_UNIT_X,
292 UI_UNIT_Y,
293 std::nullopt);
295 RNA_enum_set(but->opptr, "type", -1);
296
297 but = uiDefIconButO(block,
299 "PALETTE_OT_color_move",
301 ICON_TRIA_DOWN,
302 0,
303 0,
304 UI_UNIT_X,
305 UI_UNIT_Y,
306 std::nullopt);
308 RNA_enum_set(but->opptr, "type", 1);
309
310 /* Menu. */
312 block, ui_template_palette_menu, nullptr, ICON_SORTSIZE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "");
313 }
314
315 col = &layout->column(true);
316 col->row(true);
317
318 int row_cols = 0, col_id = 0;
319 LISTBASE_FOREACH (PaletteColor *, color, &palette->colors) {
320 if (row_cols >= cols_per_row) {
321 col->row(true);
322 row_cols = 0;
323 }
324
325 PointerRNA color_ptr = RNA_pointer_create_discrete(&palette->id, &RNA_PaletteColor, color);
326 uiButColor *color_but = (uiButColor *)uiDefButR(block,
328 0,
329 "",
330 0,
331 0,
332 UI_UNIT_X,
333 UI_UNIT_Y,
334 &color_ptr,
335 "color",
336 -1,
337 0.0,
338 1.0,
339 "");
340 color_but->is_pallete_color = true;
341 color_but->palette_color_index = col_id;
342 row_cols++;
343 col_id++;
344 }
345}
346
349 const StringRefNull propname,
350 int icon)
351{
352 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
353
354 if (!prop) {
355 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
356 return;
357 }
358
359 uiBlock *block = layout->block();
360
361 uiBut *but = uiDefIconButO(block,
363 "UI_OT_eyedropper_color",
365 icon,
366 0,
367 0,
368 UI_UNIT_X,
369 UI_UNIT_Y,
371 but->rnapoin = *ptr;
372 but->rnaprop = prop;
373 but->rnaindex = -1;
374}
#define LISTBASE_FOREACH(type, var, list)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_COLOR
#define UI_SCALE_FAC
@ USER_CP_SQUARE_SV
@ USER_CP_CIRCLE_HSL
@ USER_CP_SQUARE_HS
@ USER_CP_SQUARE_HV
@ USER_CP_CIRCLE_HSV
#define RNA_warning(format,...)
#define UI_UNIT_Y
uiBut * uiDefButR(uiBlock *block, ButType type, int retval, std::optional< blender::StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, blender::StringRefNull propname, int index, float min, float max, std::optional< blender::StringRef > tip)
uiBut * uiDefIconButO(uiBlock *block, ButType type, blender::StringRefNull opname, blender::wm::OpCallContext opcontext, int icon, int x, int y, short width, short height, std::optional< blender::StringRef > tip)
uiBut * uiDefButR_prop(uiBlock *block, ButType type, int retval, std::optional< blender::StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< blender::StringRef > tip)
PointerRNA * UI_but_operator_ptr_ensure(uiBut *but)
uiBut * uiDefIconMenuBut(uiBlock *block, uiMenuCreateFunc func, void *arg, int icon, int x, int y, short width, short height, std::optional< blender::StringRef > tip)
eButGradientType
@ UI_GRAD_L_ALT
@ UI_GRAD_SV
@ UI_GRAD_V_ALT
@ UI_GRAD_HV
@ UI_GRAD_HS
#define UI_UNIT_X
volatile int lock
#define U
constexpr const char * c_str() const
uint col
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ColorPicker * ui_block_colorpicker_create(uiBlock *block)
void uiTemplatePalette(uiLayout *layout, PointerRNA *ptr, const StringRefNull propname, bool)
void uiTemplateCryptoPicker(uiLayout *layout, PointerRNA *ptr, const StringRefNull propname, int icon)
static void ui_template_palette_menu(bContext *, uiLayout *layout, void *)
void uiTemplateColorPicker(uiLayout *layout, PointerRNA *ptr, const StringRefNull propname, bool value_slider, bool lock, bool lock_luminosity, bool cubic)
const char * RNA_property_ui_description(const PropertyRNA *prop)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
const char * RNA_struct_identifier(const StructRNA *type)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
float luminosity_lock_value
void * first
ListBase colors
StructRNA * type
Definition RNA_types.hh:52
void * data
Definition RNA_types.hh:53
eButGradientType gradient_type
void * custom_data
PropertyRNA * rnaprop
PointerRNA * opptr
PointerRNA rnapoin
uiBlock * block() const
void label(blender::StringRef name, int icon)
uiLayout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
int width() const
uiLayout & row(bool align)
PointerRNA op(wmOperatorType *ot, std::optional< blender::StringRef > name, int icon, blender::wm::OpCallContext context, eUI_Item_Flag flag)
PointerRNA * ptr
Definition wm_files.cc:4238