Blender V5.3
grease_pencil_vertex_paint.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_math_color_c.hh"
10
11#include "BLT_translation.hh"
12
13#include "BKE_brush.hh"
14#include "BKE_context.hh"
15#include "BKE_curves.hh"
16#include "BKE_grease_pencil.hh"
17#include "BKE_paint.hh"
18
19#include "DEG_depsgraph.hh"
20
21#include "RNA_access.hh"
22#include "RNA_define.hh"
23
24#include "ED_curves.hh"
25#include "ED_grease_pencil.hh"
26
27namespace blender {
28
29namespace ed::greasepencil {
30
31enum class VertexColorMode : int8_t {
32 Stroke = 0,
33 Fill = 1,
34 Both = 2,
35};
36
38 {int(VertexColorMode::Stroke), "STROKE", 0, "Stroke", ""},
39 {int(VertexColorMode::Fill), "FILL", 0, "Fill", ""},
40 {int(VertexColorMode::Both), "BOTH", 0, "Stroke & Fill", ""},
41 {0, nullptr, 0, nullptr, nullptr},
42};
43
44template<typename Fn>
46 Object &object,
48 const bool use_selection_mask,
49 Fn &&fn)
50{
51 bool changed = false;
52 IndexMaskMemory memory;
53 using namespace ed::greasepencil;
55 if (info.drawing.strokes().attributes().contains("vertex_color")) {
56 const IndexMask points = use_selection_mask ?
58 object, info.drawing, info.layer_index, memory) :
60 object, info.drawing, info.layer_index, memory);
61 if (!points.is_empty()) {
63 points.foreach_index(
64 [&](const int64_t point_i) {
65 ColorGeometry4f &color = vertex_colors[point_i];
66 if (color.a > 0.0f) {
67 color = fn(color);
68 }
69 },
71 changed = true;
72 }
73 }
74 }
76 if (info.drawing.strokes().attributes().contains("fill_color")) {
77 const IndexMask strokes = use_selection_mask ?
79 object, info.drawing, info.layer_index, memory) :
81 object, info.drawing, info.layer_index, memory);
82 if (!strokes.is_empty()) {
84 strokes.foreach_index(
85 [&](const int64_t curve_i) {
86 ColorGeometry4f &color = fill_colors[curve_i];
87 if (color.a > 0.0f) {
88 color = fn(color);
89 }
90 },
92 changed = true;
93 }
94 }
95 }
96 return changed;
97}
98
100 wmOperator *op)
101{
102 const Scene &scene = *CTX_data_scene(C);
103 Object &object = *CTX_data_active_object(C);
104 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
105 const VertexColorMode mode = VertexColorMode(RNA_enum_get(op->ptr, "mode"));
106 const float brightness = RNA_float_get(op->ptr, "brightness");
107 const float contrast = RNA_float_get(op->ptr, "contrast");
108 float delta = contrast / 2.0f;
109 const bool use_selection_mask = ED_grease_pencil_any_vertex_mask_selection(scene.toolsettings);
110
111 /*
112 * The algorithm is by Werner D. Streidt
113 * (http://visca.com/ffactory/archives/5-99/msg00021.html)
114 * Extracted of OpenCV `demhist.c`.
115 */
116 float gain, offset;
117 if (contrast > 0.0f) {
118 gain = 1.0f - delta * 2.0f;
119 gain = 1.0f / math::max(gain, FLT_EPSILON);
120 offset = gain * (brightness - delta);
121 }
122 else {
123 delta *= -1.0f;
124 gain = math::max(1.0f - delta * 2.0f, 0.0f);
125 offset = gain * brightness + delta;
126 }
127
128 std::atomic<bool> any_changed;
132 if (curves.is_empty()) {
133 return;
134 }
135 const bool changed = apply_color_operation_for_mode(
136 mode,
137 object,
138 info,
139 use_selection_mask,
140 [&](const ColorGeometry4f &color) -> ColorGeometry4f {
141 const float3 result = float3(color) * gain + offset;
142 return ColorGeometry4f(result[0], result[1], result[2], color.a);
143 });
144 any_changed.store(any_changed | changed, std::memory_order_relaxed);
145 });
146
147 if (any_changed) {
150 }
151 return OPERATOR_FINISHED;
152}
153
155{
156 /* identifiers */
157 ot->name = "Vertex Paint Brightness/Contrast";
158 ot->idname = "GREASE_PENCIL_OT_vertex_color_brightness_contrast";
159 ot->description = "Adjust vertex color brightness/contrast";
160
161 /* API callbacks. */
164
165 /* flags */
167
168 /* params */
169 ot->prop = RNA_def_enum(
170 ot->srna, "mode", prop_grease_pencil_vertex_mode, int(VertexColorMode::Both), "Mode", "");
171
172 RNA_def_float(ot->srna, "brightness", 0.0f, -1.0f, 1.0f, "Brightness", "", -1.0f, 1.0f);
173 RNA_def_float(ot->srna, "contrast", 0.0f, -1.0f, 1.0f, "Contrast", "", -1.0f, 1.0f);
174}
175
177{
178 const Scene &scene = *CTX_data_scene(C);
179 Object &object = *CTX_data_active_object(C);
180 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
181 const VertexColorMode mode = VertexColorMode(RNA_enum_get(op->ptr, "mode"));
182 const float hue = RNA_float_get(op->ptr, "h");
183 const float sat = RNA_float_get(op->ptr, "s");
184 const float val = RNA_float_get(op->ptr, "v");
185 const bool use_selection_mask = ED_grease_pencil_any_vertex_mask_selection(scene.toolsettings);
186
187 std::atomic<bool> any_changed;
191 if (curves.is_empty()) {
192 return;
193 }
194 const bool changed = apply_color_operation_for_mode(
195 mode,
196 object,
197 info,
198 use_selection_mask,
199 [&](const ColorGeometry4f &color) -> ColorGeometry4f {
200 float3 hsv;
202
203 hsv[0] += (hue - 0.5f);
204 if (hsv[0] > 1.0f) {
205 hsv[0] -= 1.0f;
206 }
207 else if (hsv[0] < 0.0f) {
208 hsv[0] += 1.0f;
209 }
210 hsv[1] *= sat;
211 hsv[2] *= val;
212
213 float3 rgb_result;
214 hsv_to_rgb_v(hsv, rgb_result);
215 return ColorGeometry4f(rgb_result[0], rgb_result[1], rgb_result[2], color.a);
216 });
217 any_changed.store(any_changed | changed, std::memory_order_relaxed);
218 });
219
220 if (any_changed) {
223 }
224 return OPERATOR_FINISHED;
225}
226
228{
229 /* identifiers */
230 ot->name = "Vertex Paint Hue/Saturation/Value";
231 ot->idname = "GREASE_PENCIL_OT_vertex_color_hsv";
232 ot->description = "Adjust vertex color HSV values";
233
234 /* API callbacks. */
237
238 /* flags */
240
241 /* params */
242 ot->prop = RNA_def_enum(
243 ot->srna, "mode", prop_grease_pencil_vertex_mode, int(VertexColorMode::Both), "Mode", "");
244
245 PropertyRNA *prop = RNA_def_float(ot->srna, "h", 0.5f, 0.0f, 1.0f, "Hue", "", 0.0f, 1.0f);
247
248 prop = RNA_def_float(ot->srna, "s", 1.0f, 0.0f, 2.0f, "Saturation", "", 0.0f, 2.0f);
250
251 prop = RNA_def_float(ot->srna, "v", 1.0f, 0.0f, 2.0f, "Value", "", 0.0f, 2.0f);
254}
255
257{
258 const Scene &scene = *CTX_data_scene(C);
259 Object &object = *CTX_data_active_object(C);
260 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
261 const VertexColorMode mode = VertexColorMode(RNA_enum_get(op->ptr, "mode"));
262 const bool use_selection_mask = ED_grease_pencil_any_vertex_mask_selection(scene.toolsettings);
263
264 std::atomic<bool> any_changed;
268 if (curves.is_empty()) {
269 return;
270 }
271 const bool changed = apply_color_operation_for_mode(
272 mode,
273 object,
274 info,
275 use_selection_mask,
276 [&](const ColorGeometry4f &color) -> ColorGeometry4f {
277 /* Invert the color. */
278 return ColorGeometry4f(1.0f - color.r, 1.0f - color.g, 1.0f - color.b, color.a);
279 });
280 any_changed.store(any_changed | changed, std::memory_order_relaxed);
281 });
282
283 if (any_changed) {
286 }
287 return OPERATOR_FINISHED;
288}
289
291{
292 /* identifiers */
293 ot->name = "Vertex Paint Invert";
294 ot->idname = "GREASE_PENCIL_OT_vertex_color_invert";
295 ot->description = "Invert RGB values";
296
297 /* API callbacks. */
300
301 /* flags */
303
304 /* params */
305 ot->prop = RNA_def_enum(
306 ot->srna, "mode", prop_grease_pencil_vertex_mode, int(VertexColorMode::Both), "Mode", "");
307}
308
310{
311 const Scene &scene = *CTX_data_scene(C);
312 Object &object = *CTX_data_active_object(C);
313 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
314 const VertexColorMode mode = VertexColorMode(RNA_enum_get(op->ptr, "mode"));
315 const float gain = RNA_float_get(op->ptr, "gain");
316 const float offset = RNA_float_get(op->ptr, "offset");
317 const bool use_selection_mask = ED_grease_pencil_any_vertex_mask_selection(scene.toolsettings);
318
319 std::atomic<bool> any_changed;
323 if (curves.is_empty()) {
324 return;
325 }
326 const bool changed = apply_color_operation_for_mode(
327 mode,
328 object,
329 info,
330 use_selection_mask,
331 [&](const ColorGeometry4f &color) -> ColorGeometry4f {
332 const float3 result = float3(color) * gain + offset;
333 return ColorGeometry4f(result[0], result[1], result[2], color.a);
334 });
335 any_changed.store(any_changed | changed, std::memory_order_relaxed);
336 });
337
338 if (any_changed) {
341 }
342 return OPERATOR_FINISHED;
343}
344
346{
347 /* identifiers */
348 ot->name = "Vertex Paint Levels";
349 ot->idname = "GREASE_PENCIL_OT_vertex_color_levels";
350 ot->description = "Adjust levels of vertex colors";
351
352 /* API callbacks. */
355
356 /* flags */
358
359 /* params */
360 ot->prop = RNA_def_enum(
361 ot->srna, "mode", prop_grease_pencil_vertex_mode, int(VertexColorMode::Both), "Mode", "");
362
364 ot->srna, "offset", 0.0f, -1.0f, 1.0f, "Offset", "Value to add to colors", -1.0f, 1.0f);
366 ot->srna, "gain", 1.0f, 0.0f, FLT_MAX, "Gain", "Value to multiply colors by", 0.0f, 10.0f);
367}
368
370{
371 const Scene &scene = *CTX_data_scene(C);
372 Object &object = *CTX_data_active_object(C);
374 const Brush &brush = *BKE_paint_brush(&paint);
375 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
376 const VertexColorMode mode = VertexColorMode(RNA_enum_get(op->ptr, "mode"));
377 const float factor = RNA_float_get(op->ptr, "factor");
378 const bool use_selection_mask = ED_grease_pencil_any_vertex_mask_selection(scene.toolsettings);
379
380 float3 color_linear = BKE_brush_color_get(&paint, &brush);
381 const ColorGeometry4f target_color(color_linear[0], color_linear[1], color_linear[2], 1.0f);
382
383 std::atomic<bool> any_changed;
387 if (curves.is_empty()) {
388 return;
389 }
390 /* Create the color attributes if they don't exist. */
392 curves.attributes_for_write().add<ColorGeometry4f>(
394 }
396 curves.attributes_for_write().add<ColorGeometry4f>(
398 }
399 const bool changed = apply_color_operation_for_mode(
400 mode,
401 object,
402 info,
403 use_selection_mask,
404 [&](const ColorGeometry4f &color) -> ColorGeometry4f {
405 /* Mix in the target color based on the factor. */
406 return math::interpolate(color, target_color, factor);
407 });
408 any_changed.store(any_changed | changed, std::memory_order_relaxed);
409 });
410
411 if (any_changed) {
414 }
415 return OPERATOR_FINISHED;
416}
417
419{
420 /* identifiers */
421 ot->name = "Vertex Paint Set Color";
422 ot->idname = "GREASE_PENCIL_OT_vertex_color_set";
423 ot->description = "Set active color to all selected vertices";
424
425 /* API callbacks. */
428
429 /* flags */
431
432 /* params */
433 ot->prop = RNA_def_enum(
434 ot->srna, "mode", prop_grease_pencil_vertex_mode, int(VertexColorMode::Both), "Mode", "");
435 RNA_def_float(ot->srna, "factor", 1.0f, 0.0f, 1.0f, "Factor", "Mix Factor", 0.0f, 1.0f);
436}
437
439{
440 const Scene &scene = *CTX_data_scene(C);
441 Object &object = *CTX_data_active_object(C);
442 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
443 const VertexColorMode mode = VertexColorMode(RNA_enum_get(op->ptr, "mode"));
444 const bool use_selection_mask = ED_grease_pencil_any_vertex_mask_selection(scene.toolsettings);
445
446 std::atomic<bool> any_changed;
450 if (curves.is_empty()) {
451 return;
452 }
453
454 bool changed = false;
455 if (use_selection_mask) {
457 mode,
458 object,
459 info,
460 use_selection_mask,
461 [&](const ColorGeometry4f & /*color*/) -> ColorGeometry4f {
462 return ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f);
463 });
464 any_changed.store(any_changed | changed, std::memory_order_relaxed);
465 return;
466 }
467
468 /* Remove the color attributes. */
470 changed |= curves.attributes_for_write().remove("vertex_color");
471 }
473 changed |= curves.attributes_for_write().remove("fill_color");
474 }
475 any_changed.store(any_changed | changed, std::memory_order_relaxed);
476 });
477
478 if (any_changed) {
481 }
482 return OPERATOR_FINISHED;
483}
484
486{
487 /* identifiers */
488 ot->name = "Reset Vertex Color";
489 ot->idname = "GREASE_PENCIL_OT_stroke_reset_vertex_color";
490 ot->description = "Reset vertex color for all or selected strokes";
491
492 /* callbacks */
495
496 /* flags */
498
499 /* properties */
500 ot->prop = RNA_def_enum(
501 ot->srna, "mode", prop_grease_pencil_vertex_mode, int(VertexColorMode::Both), "Mode", "");
502}
503
504} // namespace ed::greasepencil
505
516
517} // namespace blender
Low-level operations for curves.
Low-level operations for grease pencil.
#define BLT_I18NCONTEXT_COLOR
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define ELEM(...)
bool contains(StringRef name) const
AttributeAccessor attributes() const
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
MutableSpan< ColorGeometry4f > fill_colors_for_write()
MutableSpan< ColorGeometry4f > vertex_colors_for_write()
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define FLT_MAX
#define FLT_EPSILON
IndexMask retrieve_editable_and_selected_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
IndexMask retrieve_editable_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static const EnumPropertyItem prop_grease_pencil_vertex_mode[]
static void GREASE_PENCIL_OT_vertex_color_levels(wmOperatorType *ot)
bool grease_pencil_vertex_painting_poll(bContext *C)
static wmOperatorStatus grease_pencil_vertex_paint_set_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_vertex_paint_invert_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_vertex_paint_hsv_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_stroke_reset_vertex_color(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_vertex_paint_brightness_contrast_exec(bContext *C, wmOperator *op)
IndexMask retrieve_editable_and_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static void GREASE_PENCIL_OT_vertex_color_brightness_contrast(wmOperatorType *ot)
static void GREASE_PENCIL_OT_vertex_color_set(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_vertex_paint_reset_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_vertex_paint_levels_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_vertex_color_invert(wmOperatorType *ot)
static void GREASE_PENCIL_OT_vertex_color_hsv(wmOperatorType *ot)
static bool apply_color_operation_for_mode(const VertexColorMode mode, Object &object, MutableDrawingInfo &info, const bool use_selection_mask, Fn &&fn)
IndexMask retrieve_editable_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
Vector< MutableDrawingInfo > retrieve_editable_drawings(const Scene &scene, GreasePencil &grease_pencil)
constexpr ParallelGrainSize grain_size(int grain_size)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
BMesh Iterators.
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
__int64 int64_t
Definition BKE_text.h:90
int RNA_enum_get(PointerRNA *ptr, const char *name)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void DEG_id_tag_update(ID *id, unsigned int flags)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
BMesh const char void * data
bool ED_grease_pencil_any_vertex_mask_selection(const ToolSettings *tool_settings)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Paint * BKE_paint_get_active_from_context(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
void ED_operatortypes_grease_pencil_vertex_paint()
Brush * BKE_paint_brush(Paint *paint)
Scene * CTX_data_scene(const bContext *C)
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
float RNA_float_get(PointerRNA *ptr, const char *name)
signed char int8_t
Definition BKE_text.h:76
struct ToolSettings * toolsettings
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165