Blender V5.3
palette.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "DNA_brush_types.h"
9#include "DNA_scene_types.h"
10#include "DNA_space_types.h"
12
13#include "RNA_access.hh"
14#include "RNA_define.hh"
15
16#include "BLI_listbase.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_set.hh"
19
20#include "BKE_brush.hh"
21#include "BKE_context.hh"
22#include "BKE_image.hh"
23#include "BKE_lib_id.hh"
24#include "BKE_library.hh"
25#include "BKE_paint.hh"
26#include "BKE_paint_types.hh"
27
29#include "IMB_imbuf.hh"
30#include "IMB_imbuf_types.hh"
31
32#include "WM_api.hh"
33#include "WM_types.hh"
34
35#include "paint_intern.hh"
36
37namespace blender {
38
40 float rgb[3] = {};
41 float value = 0;
42 float h = 0;
43 float s = 0;
44 float v = 0;
45};
46
47static int palettecolor_compare_hsv(const void *a1, const void *a2)
48{
49 const PaletteColorHSV *ps1 = static_cast<const PaletteColorHSV *>(a1);
50 const PaletteColorHSV *ps2 = static_cast<const PaletteColorHSV *>(a2);
51
52 /* Hue */
53 if (ps1->h > ps2->h) {
54 return 1;
55 }
56 if (ps1->h < ps2->h) {
57 return -1;
58 }
59
60 /* Saturation. */
61 if (ps1->s > ps2->s) {
62 return 1;
63 }
64 if (ps1->s < ps2->s) {
65 return -1;
66 }
67
68 /* Value. */
69 if (1.0f - ps1->v > 1.0f - ps2->v) {
70 return 1;
71 }
72 if (1.0f - ps1->v < 1.0f - ps2->v) {
73 return -1;
74 }
75
76 return 0;
77}
78
79static void palette_sort_hsv(PaletteColorHSV *color_array, const int totcol)
80{
81 qsort(color_array, totcol, sizeof(PaletteColorHSV), palettecolor_compare_hsv);
82}
83
84static int palettecolor_compare_svh(const void *a1, const void *a2)
85{
86 const PaletteColorHSV *ps1 = static_cast<const PaletteColorHSV *>(a1);
87 const PaletteColorHSV *ps2 = static_cast<const PaletteColorHSV *>(a2);
88
89 /* Saturation. */
90 if (ps1->s > ps2->s) {
91 return 1;
92 }
93 if (ps1->s < ps2->s) {
94 return -1;
95 }
96
97 /* Value. */
98 if (1.0f - ps1->v > 1.0f - ps2->v) {
99 return 1;
100 }
101 if (1.0f - ps1->v < 1.0f - ps2->v) {
102 return -1;
103 }
104
105 /* Hue */
106 if (ps1->h > ps2->h) {
107 return 1;
108 }
109 if (ps1->h < ps2->h) {
110 return -1;
111 }
112
113 return 0;
114}
115
116static void palette_sort_svh(PaletteColorHSV *color_array, const int totcol)
117{
118 qsort(color_array, totcol, sizeof(PaletteColorHSV), palettecolor_compare_svh);
119}
120
121static int palettecolor_compare_vhs(const void *a1, const void *a2)
122{
123 const PaletteColorHSV *ps1 = static_cast<const PaletteColorHSV *>(a1);
124 const PaletteColorHSV *ps2 = static_cast<const PaletteColorHSV *>(a2);
125
126 /* Value. */
127 if (1.0f - ps1->v > 1.0f - ps2->v) {
128 return 1;
129 }
130 if (1.0f - ps1->v < 1.0f - ps2->v) {
131 return -1;
132 }
133
134 /* Hue */
135 if (ps1->h > ps2->h) {
136 return 1;
137 }
138 if (ps1->h < ps2->h) {
139 return -1;
140 }
141
142 /* Saturation. */
143 if (ps1->s > ps2->s) {
144 return 1;
145 }
146 if (ps1->s < ps2->s) {
147 return -1;
148 }
149
150 return 0;
151}
152
153static void palette_sort_vhs(PaletteColorHSV *color_array, const int totcol)
154{
155 qsort(color_array, totcol, sizeof(PaletteColorHSV), palettecolor_compare_vhs);
156}
157
158static int palettecolor_compare_luminance(const void *a1, const void *a2)
159{
160 const PaletteColorHSV *ps1 = static_cast<const PaletteColorHSV *>(a1);
161 const PaletteColorHSV *ps2 = static_cast<const PaletteColorHSV *>(a2);
162
163 float lumi1 = (ps1->rgb[0] + ps1->rgb[1] + ps1->rgb[2]) / 3.0f;
164 float lumi2 = (ps2->rgb[0] + ps2->rgb[1] + ps2->rgb[2]) / 3.0f;
165
166 if (lumi1 > lumi2) {
167 return -1;
168 }
169 if (lumi1 < lumi2) {
170 return 1;
171 }
172
173 return 0;
174}
175
176static void palette_sort_luminance(PaletteColorHSV *color_array, const int totcol)
177{
178 /* Sort by Luminance (calculated with the average, enough for sorting). */
179 qsort(color_array, totcol, sizeof(PaletteColorHSV), palettecolor_compare_luminance);
180}
181
182static bool palette_from_hash(Main *bmain, const Set<uint> &color_table, const char *name)
183{
184 if (color_table.is_empty()) {
185 return false;
186 }
187
188 /* Put colors into array. */
189 Array<PaletteColorHSV> color_array(color_table.size());
190 int64_t index = 0;
191 for (uint col : color_table) {
192 PaletteColorHSV pal_col;
193 cpack_to_rgb(col, &pal_col.rgb[0], &pal_col.rgb[1], &pal_col.rgb[2]);
194 rgb_to_hsv(pal_col.rgb[0], pal_col.rgb[1], pal_col.rgb[2], &pal_col.h, &pal_col.s, &pal_col.v);
195 pal_col.value = 0;
196 color_array[index++] = pal_col;
197 }
198
199 /* Sort by Hue and saturation. */
200 palette_sort_hsv(color_array.data(), color_array.size());
201
202 /* Create the Palette. */
203 Palette *palette = BKE_palette_add(bmain, name);
204 if (!palette) {
205 return false;
206 }
207 for (const PaletteColorHSV &col_src : color_array) {
208 PaletteColor *col_dst = BKE_palette_color_add(palette);
209 if (col_dst) {
211 }
212 }
213 return true;
214}
215
217{
219 Main *bmain = CTX_data_main(C);
220 Palette *palette;
221
222 palette = BKE_palette_add(bmain, "Palette");
223
225
226 return OPERATOR_FINISHED;
227}
228
230{
231 /* identifiers */
232 ot->name = "Add New Palette";
233 ot->description = "Add new palette";
234 ot->idname = "PALETTE_OT_new";
235
236 /* API callbacks. */
238
239 /* flags */
241}
242
244{
246
247 if (paint && paint->palette != nullptr && ID_IS_EDITABLE(paint->palette) &&
248 !ID_IS_OVERRIDE_LIBRARY(paint->palette))
249 {
250 return true;
251 }
252
253 return false;
254}
255
257{
260 Palette *palette = paint->palette;
262
263 color = BKE_palette_color_add(palette);
264 palette->active_color = palette->colors.count() - 1;
265
267 if (brush) {
268 if (ELEM(mode,
275 {
277 color->value = 0.0;
278 }
279 else if (mode == PaintMode::Weight) {
280 zero_v3(color->color);
281 color->value = brush->weight;
282 }
283 }
284
285 return OPERATOR_FINISHED;
286}
287
289{
290 /* identifiers */
291 ot->name = "New Palette Color";
292 ot->description = "Add new color to active palette";
293 ot->idname = "PALETTE_OT_color_add";
294
295 /* API callbacks. */
297 ot->poll = palette_poll;
298 /* flags */
300}
301
303{
305 Palette *palette = paint->palette;
306 PaletteColor *color = static_cast<PaletteColor *>(
307 BLI_findlink(&palette->colors, palette->active_color));
308
309 if (color) {
311 }
312
313 return OPERATOR_FINISHED;
314}
315
317{
318 /* identifiers */
319 ot->name = "Delete Palette Color";
320 ot->description = "Remove active color from palette";
321 ot->idname = "PALETTE_OT_color_delete";
322
323 /* API callbacks. */
325 ot->poll = palette_poll;
326 /* flags */
328}
329
330/* --- Extract Palette from Image. */
332{
334 if ((sl != nullptr) && (sl->spacetype == SPACE_IMAGE)) {
336 Image *image = sima->image;
337 ImageUser iuser = sima->iuser;
338 return BKE_image_has_ibuf(image, &iuser);
339 }
340
341 return false;
342}
343
345{
346 const int threshold = RNA_int_get(op->ptr, "threshold");
347
348 Main *bmain = CTX_data_main(C);
349 bool done = false;
350
352 Image *image = sima->image;
353 ImageUser iuser = sima->iuser;
354 void *lock;
355 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
356
357 if (ibuf && ibuf->byte_data()) {
358 /* Extract all colors. */
359 Set<uint> color_table;
360 const int range = int(pow(10.0f, threshold));
361 Span<uchar4> byte_buffer = Span(reinterpret_cast<const uchar4 *>(ibuf->byte_data()),
362 IMB_get_pixel_count(ibuf));
363 for (uchar4 pix : byte_buffer) {
364 float color[3];
368
369 for (int i = 0; i < 3; i++) {
370 color[i] = truncf(color[i] * range) / range;
371 }
372 uint key = rgb_to_cpack(color[0], color[1], color[2]);
373 color_table.add(key);
374 }
375
376 done = palette_from_hash(bmain, color_table, image->id.name + 2);
377 }
378
380
381 if (done) {
382 BKE_reportf(op->reports, RPT_INFO, "Palette created");
383 }
384
385 return OPERATOR_FINISHED;
386}
387
389{
390 PropertyRNA *prop;
391
392 /* identifiers */
393 ot->name = "Extract Palette from Image";
394 ot->idname = "PALETTE_OT_extract_from_image";
395 ot->description = "Extract all colors used in Image and create a Palette";
396
397 /* API callbacks. */
400
401 /* flags */
403
404 /* properties */
405 prop = RNA_def_int(ot->srna, "threshold", 1, 1, 1, "Threshold", "", 1, 1);
407}
408
409/* Sort Palette color by Hue and Saturation. */
411{
412 const int type = RNA_enum_get(op->ptr, "type");
413
415 Palette *palette = paint->palette;
416
417 if (palette == nullptr) {
418 return OPERATOR_CANCELLED;
419 }
420
421 PaletteColorHSV *color_array = nullptr;
422 PaletteColorHSV *col_elm = nullptr;
423
424 const int totcol = palette->colors.count();
425
426 if (totcol > 0) {
427 color_array = MEM_new_array<PaletteColorHSV>(totcol, __func__);
428 /* Put all colors in an array. */
429 int t = 0;
430 for (PaletteColor &color : palette->colors) {
431 float h, s, v;
432 rgb_to_hsv(color.color[0], color.color[1], color.color[2], &h, &s, &v);
433 col_elm = &color_array[t];
434 copy_v3_v3(col_elm->rgb, color.color);
435 col_elm->value = color.value;
436 col_elm->h = h;
437 col_elm->s = s;
438 col_elm->v = v;
439 t++;
440 }
441 /* Sort */
442 if (type == 1) {
443 palette_sort_hsv(color_array, totcol);
444 }
445 else if (type == 2) {
446 palette_sort_svh(color_array, totcol);
447 }
448 else if (type == 3) {
449 palette_sort_vhs(color_array, totcol);
450 }
451 else {
452 palette_sort_luminance(color_array, totcol);
453 }
454
455 /* Clear old color swatches. */
456 for (PaletteColor &color : palette->colors.items_mutable()) {
458 }
459
460 /* Recreate swatches sorted. */
461 for (int i = 0; i < totcol; i++) {
462 col_elm = &color_array[i];
463 PaletteColor *palcol = BKE_palette_color_add(palette);
464 if (palcol) {
465 copy_v3_v3(palcol->color, col_elm->rgb);
466 }
467 }
468 }
469
470 /* Free memory. */
471 if (totcol > 0) {
472 MEM_SAFE_DELETE(color_array);
473 }
474
476
477 return OPERATOR_FINISHED;
478}
479
481{
482 static const EnumPropertyItem sort_type[] = {
483 {1, "HSV", 0, "Hue, Saturation, Value", ""},
484 {2, "SVH", 0, "Saturation, Value, Hue", ""},
485 {3, "VHS", 0, "Value, Hue, Saturation", ""},
486 {4, "LUMINANCE", 0, "Luminance", ""},
487 {0, nullptr, 0, nullptr, nullptr},
488 };
489
490 /* identifiers */
491 ot->name = "Sort Palette";
492 ot->idname = "PALETTE_OT_sort";
493 ot->description = "Sort Palette Colors";
494
495 /* API callbacks. */
497 ot->poll = palette_poll;
498
499 /* flags */
501
502 ot->prop = RNA_def_enum(ot->srna, "type", sort_type, 1, "Type", "");
503}
504
505/* Move colors in palette. */
507{
509 Palette *palette = paint->palette;
510 PaletteColor *palcolor = static_cast<PaletteColor *>(
511 BLI_findlink(&palette->colors, palette->active_color));
512
513 if (palcolor == nullptr) {
514 return OPERATOR_CANCELLED;
515 }
516
517 const int direction = RNA_enum_get(op->ptr, "type");
518
519 BLI_assert(ELEM(direction, -1, 0, 1)); /* we use value below */
520 if (BLI_listbase_link_move(&palette->colors, palcolor, direction)) {
521 palette->active_color += direction;
523 }
524
525 return OPERATOR_FINISHED;
526}
527
529{
530 static const EnumPropertyItem slot_move[] = {
531 {-1, "UP", 0, "Up", ""},
532 {1, "DOWN", 0, "Down", ""},
533 {0, nullptr, 0, nullptr, nullptr},
534 };
535
536 /* identifiers */
537 ot->name = "Move Palette Color";
538 ot->idname = "PALETTE_OT_color_move";
539 ot->description = "Move the active Color up/down in the list";
540
541 /* API callbacks. */
543 ot->poll = palette_poll;
544
545 /* flags */
547
548 ot->prop = RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
549}
550
551/* Join Palette swatches. */
553{
554 Main *bmain = CTX_data_main(C);
556 Palette *palette = paint->palette;
557 Palette *palette_join = nullptr;
558 bool done = false;
559
560 char name[MAX_ID_NAME - 2];
561 RNA_string_get(op->ptr, "palette", name);
562
563 if ((palette == nullptr) || (name[0] == '\0')) {
564 return OPERATOR_CANCELLED;
565 }
566
567 palette_join = id_cast<Palette *>(BKE_libblock_find_name(bmain, ID_PAL, name));
568 if (palette_join == nullptr) {
569 return OPERATOR_CANCELLED;
570 }
571
572 const int totcol = palette_join->colors.count();
573
574 if (totcol > 0) {
575 for (PaletteColor &color : palette_join->colors) {
576 PaletteColor *palcol = BKE_palette_color_add(palette);
577 if (palcol) {
578 copy_v3_v3(palcol->color, color.color);
579 palcol->value = color.value;
580 done = true;
581 }
582 }
583 }
584
585 if (done) {
586 /* Clear old color swatches. */
587 for (PaletteColor &color : palette_join->colors.items_mutable()) {
588 BKE_palette_color_remove(palette_join, &color);
589 }
590
591 /* Notifier. */
593 }
594
595 return OPERATOR_FINISHED;
596}
597
599{
600 /* identifiers */
601 ot->name = "Join Palette Swatches";
602 ot->idname = "PALETTE_OT_join";
603 ot->description = "Join Palette Swatches";
604
605 /* API callbacks. */
607 ot->poll = palette_poll;
608
609 /* flags */
611
612 /* properties */
613 RNA_def_string(ot->srna, "palette", nullptr, MAX_ID_NAME - 2, "Palette", "Name of the Palette");
614}
615} // namespace blender
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define C
Definition RandGen.cpp:29
#define NC_BRUSH
Definition WM_types.hh:398
#define NA_EDITED
Definition WM_types.hh:634
volatile int lock
#define ELEM(...)
int64_t size() const
Definition BLI_array.hh:267
const T * data() const
Definition BLI_array.hh:323
int64_t size() const
Definition BLI_set.hh:606
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_set.hh:614
constexpr Span()=default
#define pow
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< uint8_t, 4 > uchar4
__int64 int64_t
Definition BKE_text.h:90
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static bool palette_poll(bContext *C)
Definition palette.cc:243
void cpack_to_rgb(unsigned int col, float *r_r, float *r_g, float *r_b)
static wmOperatorStatus palette_color_add_exec(bContext *C, wmOperator *)
Definition palette.cc:256
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
static void palette_sort_hsv(PaletteColorHSV *color_array, const int totcol)
Definition palette.cc:79
void PALETTE_OT_new(wmOperatorType *ot)
Definition palette.cc:229
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static bool palette_extract_img_poll(bContext *C)
Definition palette.cc:331
unsigned int rgb_to_cpack(float r, float g, float b)
static int palettecolor_compare_vhs(const void *a1, const void *a2)
Definition palette.cc:121
void PALETTE_OT_extract_from_image(wmOperatorType *ot)
Definition palette.cc:388
static wmOperatorStatus palette_sort_exec(bContext *C, wmOperator *op)
Definition palette.cc:410
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], const ColorSpace *colorspace)
static wmOperatorStatus palette_join_exec(bContext *C, wmOperator *op)
Definition palette.cc:552
static void palette_sort_luminance(PaletteColorHSV *color_array, const int totcol)
Definition palette.cc:176
BLI_INLINE void IMB_colormanagement_srgb_to_scene_linear_v3(float scene_linear[3], const float srgb[3])
static int palettecolor_compare_hsv(const void *a1, const void *a2)
Definition palette.cc:47
static int palettecolor_compare_luminance(const void *a1, const void *a2)
Definition palette.cc:158
static wmOperatorStatus palette_color_delete_exec(bContext *C, wmOperator *)
Definition palette.cc:302
static wmOperatorStatus palette_new_exec(bContext *C, wmOperator *)
Definition palette.cc:216
static wmOperatorStatus palette_extract_img_exec(bContext *C, wmOperator *op)
Definition palette.cc:344
MINLINE void zero_v3(float r[3])
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
PaletteColor * BKE_palette_color_add(Palette *palette)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
SpaceImage * CTX_wm_space_image(const bContext *C)
SpaceLink * CTX_wm_space_data(const bContext *C)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void PALETTE_OT_sort(wmOperatorType *ot)
Definition palette.cc:480
Main * CTX_data_main(const bContext *C)
void PALETTE_OT_color_add(wmOperatorType *ot)
Definition palette.cc:288
Paint * BKE_paint_get_active_from_context(const bContext *C)
void PALETTE_OT_color_move(wmOperatorType *ot)
Definition palette.cc:528
static bool palette_from_hash(Main *bmain, const Set< uint > &color_table, const char *name)
Definition palette.cc:182
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
bool BKE_image_has_ibuf(Image *ima, ImageUser *iuser)
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
static void palette_sort_vhs(PaletteColorHSV *color_array, const int totcol)
Definition palette.cc:153
void PALETTE_OT_join(wmOperatorType *ot)
Definition palette.cc:598
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
void PALETTE_OT_color_delete(wmOperatorType *ot)
Definition palette.cc:316
static wmOperatorStatus palette_color_move_exec(bContext *C, wmOperator *op)
Definition palette.cc:506
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
void BKE_paint_palette_set(Paint *paint, Palette *palette)
static int palettecolor_compare_svh(const void *a1, const void *a2)
Definition palette.cc:84
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:443
Palette * BKE_palette_add(Main *bmain, const char *name)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
static void palette_sort_svh(PaletteColorHSV *color_array, const int totcol)
Definition palette.cc:116
const ColorSpace * colorspace
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
ListBaseT< PaletteColor > colors
struct ImageUser iuser
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
struct ReportList * reports