Blender V5.3
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.hh"
10#include "BLI_math_vector_c.hh"
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
23namespace blender::ui {
24
25#define WHEEL_SIZE (5 * U.widget_unit)
26
28 PointerRNA *ptr,
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 Block *block = layout->block();
37 ColorPicker *cpicker = block_colorpicker_create(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 Layout &col = layout->column(true);
48 Layout &row = col.row(true);
49
50 Button *but = nullptr;
51 ButtonHSVCube *hsv_but;
52 switch (U.color_picker_type) {
56 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
58 "",
59 0,
60 0,
63 ptr,
64 prop,
65 -1,
66 0.0,
67 0.0,
68 ""));
69 switch (U.color_picker_type) {
71 hsv_but->gradient_type = GRAD_SV;
72 break;
74 hsv_but->gradient_type = GRAD_HS;
75 break;
77 hsv_but->gradient_type = GRAD_HV;
78 break;
81 break;
82 }
83 but = hsv_but;
84 break;
85
86 /* user default */
89 default:
90 but = uiDefButR_prop(block,
92 "",
93 0,
94 0,
97 ptr,
98 prop,
99 -1,
100 0.0,
101 0.0,
102 "");
103 break;
104 }
105
106 but->custom_data = cpicker;
107
108 cpicker->use_color_lock = lock;
109 cpicker->use_color_cubic = cubic;
110 cpicker->use_luminosity_lock = lock_luminosity;
111
112 if (lock_luminosity) {
113 float color[4]; /* in case of alpha */
116 }
117
118 if (value_slider) {
119 switch (U.color_picker_type) {
121 row.separator();
122 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
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 = GRAD_L_ALT;
136 break;
138 col.separator();
139 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
141 "",
142 0,
143 4,
145 18 * UI_SCALE_FAC,
146 ptr,
147 prop,
148 -1,
149 softmin,
150 softmax,
151 ""));
152 hsv_but->gradient_type = eButGradientType(GRAD_SV + 3);
153 break;
155 col.separator();
156 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
158 "",
159 0,
160 4,
162 18 * UI_SCALE_FAC,
163 ptr,
164 prop,
165 -1,
166 softmin,
167 softmax,
168 ""));
169 hsv_but->gradient_type = eButGradientType(GRAD_HS + 3);
170 break;
172 col.separator();
173 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
175 "",
176 0,
177 4,
179 18 * UI_SCALE_FAC,
180 ptr,
181 prop,
182 -1,
183 softmin,
184 softmax,
185 ""));
186 hsv_but->gradient_type = eButGradientType(GRAD_HV + 3);
187 break;
188
189 /* user default */
191 default:
192 row.separator();
193 hsv_but = static_cast<ButtonHSVCube *>(uiDefButR_prop(block,
195 "",
196 WHEEL_SIZE + 6,
197 0,
198 14 * UI_SCALE_FAC,
200 ptr,
201 prop,
202 -1,
203 softmin,
204 softmax,
205 ""));
206 hsv_but->gradient_type = GRAD_V_ALT;
207 break;
208 }
209
210 hsv_but->custom_data = cpicker;
211 }
212}
213
214static void template_palette_menu(bContext * /*C*/, Layout *layout, void * /*but_p*/)
215{
216
217 layout->label(IFACE_("Sort By:"), ICON_NONE);
218 Layout *row = &layout->row(false);
219 PointerRNA op_ptr = row->op("PALETTE_OT_sort", IFACE_("Hue"), ICON_NONE);
220 RNA_enum_set(&op_ptr, "type", 1);
221 row = &layout->row(false);
222 op_ptr = row->op("PALETTE_OT_sort", IFACE_("Saturation"), ICON_NONE);
223 RNA_enum_set(&op_ptr, "type", 2);
224 row = &layout->row(false);
225 op_ptr = row->op("PALETTE_OT_sort", CTX_IFACE_(BLT_I18NCONTEXT_COLOR, "Value"), ICON_NONE);
226 RNA_enum_set(&op_ptr, "type", 3);
227 row = &layout->row(false);
228 op_ptr = row->op("PALETTE_OT_sort", IFACE_("Luminance"), ICON_NONE);
229 RNA_enum_set(&op_ptr, "type", 4);
230}
231
232void template_palette(Layout *layout, PointerRNA *ptr, const StringRefNull propname)
233{
234 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
235 Button *but = nullptr;
236
237 const int cols_per_row = std::max(layout->width() / UI_UNIT_X, 1);
238
239 if (!prop) {
240 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
241 return;
242 }
243
244 const PointerRNA cptr = RNA_property_pointer_get(ptr, prop);
245 if (!cptr || !RNA_struct_is_a(cptr.type, RNA_Palette)) {
246 return;
247 }
248
249 Block *block = layout->block();
250
251 Palette *palette = static_cast<Palette *>(cptr.data);
252
253 Layout *col = &layout->column(true);
254 col->row(true);
255 uiDefIconButO(block,
257 "PALETTE_OT_color_add",
259 ICON_ADD,
260 0,
261 0,
262 UI_UNIT_X,
263 UI_UNIT_Y,
264 std::nullopt);
265 uiDefIconButO(block,
267 "PALETTE_OT_color_delete",
269 ICON_REMOVE,
270 0,
271 0,
272 UI_UNIT_X,
273 UI_UNIT_Y,
274 std::nullopt);
275 if (palette->colors.first() != nullptr) {
276 but = uiDefIconButO(block,
278 "PALETTE_OT_color_move",
280 ICON_TRIA_UP,
281 0,
282 0,
283 UI_UNIT_X,
284 UI_UNIT_Y,
285 std::nullopt);
287 RNA_enum_set(but->opptr, "type", -1);
288
289 but = uiDefIconButO(block,
291 "PALETTE_OT_color_move",
293 ICON_TRIA_DOWN,
294 0,
295 0,
296 UI_UNIT_X,
297 UI_UNIT_Y,
298 std::nullopt);
300 RNA_enum_set(but->opptr, "type", 1);
301
302 /* Menu. */
304 block, template_palette_menu, nullptr, ICON_SORTSIZE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "");
305 }
306
307 col = &layout->column(true);
308 col->row(true);
309
310 int row_cols = 0, col_id = 0;
311 for (PaletteColor &color : palette->colors) {
312 if (row_cols >= cols_per_row) {
313 col->row(true);
314 row_cols = 0;
315 }
316
317 PointerRNA color_ptr = RNA_pointer_create_discrete(&palette->id, RNA_PaletteColor, &color);
318 ButtonColor *color_but = static_cast<ButtonColor *>(uiDefButR(block,
320 "",
321 0,
322 0,
323 UI_UNIT_X,
324 UI_UNIT_Y,
325 &color_ptr,
326 "color",
327 -1,
328 0.0,
329 1.0,
330 ""));
331 color_but->is_palette_color = true;
332 color_but->palette_color_index = col_id;
333 row_cols++;
334 col_id++;
335 }
336}
337
339 PointerRNA *ptr,
340 const StringRefNull propname,
341 int icon)
342{
343 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
344
345 if (!prop) {
346 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
347 return;
348 }
349
350 Block *block = layout->block();
351
352 Button *but = uiDefIconButO(block,
354 "UI_OT_eyedropper_color",
356 icon,
357 0,
358 0,
359 UI_UNIT_X,
360 UI_UNIT_Y,
362 but->rnapoin = *ptr;
363 but->rnaprop = prop;
364 but->rnaindex = -1;
365}
366
367} // namespace blender::ui
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_COLOR
#define UI_SCALE_FAC
#define RNA_warning(format,...)
#define UI_UNIT_Y
#define UI_UNIT_X
volatile int lock
constexpr const char * c_str() const
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
Button * uiDefButR(Block *block, ButtonType type, std::optional< StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, StringRefNull propname, int index, float min, float max, std::optional< StringRef > tip)
void template_color_picker(Layout *layout, PointerRNA *ptr, StringRefNull propname, bool value_slider, bool lock, bool lock_luminosity, bool cubic)
void template_palette(Layout *layout, PointerRNA *ptr, StringRefNull propname)
PointerRNA * button_operator_ptr_ensure(Button *but)
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 template_crypto_picker(Layout *layout, PointerRNA *ptr, StringRefNull propname, int icon)
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)
Button * uiDefIconMenuBut(Block *block, MenuCreateFunc func, void *arg, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
static void template_palette_menu(bContext *, Layout *layout, void *)
UserDef U
Definition blender.cc:58
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
const char * RNA_struct_identifier(const StructRNA *type)
const char * RNA_property_ui_description(const PropertyRNA *prop, const PointerRNA *ptr)
void RNA_property_float_ui_range(PointerRNA *ptr, PropertyRNA *prop, float *softmin, float *softmax, float *step, float *precision)
ListBaseT< PaletteColor > colors
StructRNA * type
Definition RNA_types.hh:63
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)