[Bf-blender-cvs] [a304f046040] greasepencil-object: GPencil: Add support to sort palette by SVH
Antonio Vazquez
noreply at git.blender.org
Wed Nov 20 16:49:09 CET 2019
Commit: a304f046040bccd488da92c735ddb9c951323b99
Author: Antonio Vazquez
Date: Wed Nov 20 16:48:58 2019 +0100
Branches: greasepencil-object
https://developer.blender.org/rBa304f046040bccd488da92c735ddb9c951323b99
GPencil: Add support to sort palette by SVH
===================================================================
M source/blender/blenkernel/BKE_paint.h
M source/blender/blenkernel/intern/paint.c
M source/blender/editors/interface/interface_templates.c
M source/blender/editors/sculpt_paint/paint_ops.c
===================================================================
diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 2875fc0e128..9a1c7debad8 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -129,6 +129,7 @@ void BKE_palette_color_remove(struct Palette *palette, struct PaletteColor *colo
void BKE_palette_clear(struct Palette *palette);
void BKE_palette_sort_hsv(struct tPaletteColorHSV *color_array, const int totcol);
+void BKE_palette_sort_svh(struct tPaletteColorHSV *color_array, const int totcol);
bool BKE_palette_from_hash(struct Main *bmain, struct GHash *color_table);
/* paint curves */
diff --git a/source/blender/blenkernel/intern/paint.c b/source/blender/blenkernel/intern/paint.c
index 39570b3613c..eb4bf79e485 100644
--- a/source/blender/blenkernel/intern/paint.c
+++ b/source/blender/blenkernel/intern/paint.c
@@ -691,12 +691,50 @@ static int palettecolor_compare_hsv(const void *a1, const void *a2)
return 0;
}
+/* helper function to sort using qsort */
+static int palettecolor_compare_svh(const void *a1, const void *a2)
+{
+ const tPaletteColorHSV *ps1 = a1, *ps2 = a2;
+
+ /* Saturation. */
+ if (ps1->s > ps2->s) {
+ return 1;
+ }
+ else if (ps1->s < ps2->s) {
+ return -1;
+ }
+
+ /* Value. */
+ if (1.0f - ps1->v > 1.0f - ps2->v) {
+ return 1;
+ }
+ else if (1.0f - ps1->v < 1.0f - ps2->v) {
+ return -1;
+ }
+
+ /* Hue */
+ if (ps1->h > ps2->h) {
+ return 1;
+ }
+ else if (ps1->h < ps2->h) {
+ return -1;
+ }
+
+ return 0;
+}
+
void BKE_palette_sort_hsv(tPaletteColorHSV *color_array, const int totcol)
{
/* Sort by Hue , Saturation and Value. */
qsort(color_array, totcol, sizeof(tPaletteColorHSV), palettecolor_compare_hsv);
}
+void BKE_palette_sort_svh(tPaletteColorHSV *color_array, const int totcol)
+{
+ /* Sort by Saturation, Value and Hue. */
+ qsort(color_array, totcol, sizeof(tPaletteColorHSV), palettecolor_compare_svh);
+}
+
bool BKE_palette_from_hash(Main *bmain, GHash *color_table)
{
tPaletteColorHSV *color_array = NULL;
diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index 8dc91a5546b..8f8f765453b 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -4815,16 +4815,31 @@ void uiTemplatePalette(uiLayout *layout,
UI_but_operator_ptr_get(but);
RNA_enum_set(but->opptr, "type", 1);
- uiDefIconButO(block,
- UI_BTYPE_BUT,
- "PALETTE_OT_sort",
- WM_OP_INVOKE_DEFAULT,
- ICON_SORTSIZE,
- 0,
- 0,
- UI_UNIT_X,
- UI_UNIT_Y,
- NULL);
+ but = uiDefIconButO(block,
+ UI_BTYPE_BUT,
+ "PALETTE_OT_sort",
+ WM_OP_INVOKE_DEFAULT,
+ ICON_SORTSIZE,
+ 0,
+ 0,
+ UI_UNIT_X,
+ UI_UNIT_Y,
+ NULL);
+ UI_but_operator_ptr_get(but);
+ RNA_enum_set(but->opptr, "type", -1);
+
+ but = uiDefIconButO(block,
+ UI_BTYPE_BUT,
+ "PALETTE_OT_sort",
+ WM_OP_INVOKE_DEFAULT,
+ ICON_SORT_DESC,
+ 0,
+ 0,
+ UI_UNIT_X,
+ UI_UNIT_Y,
+ NULL);
+ UI_but_operator_ptr_get(but);
+ RNA_enum_set(but->opptr, "type", 1);
}
col = uiLayoutColumn(layout, true);
diff --git a/source/blender/editors/sculpt_paint/paint_ops.c b/source/blender/editors/sculpt_paint/paint_ops.c
index 7a7ebbd71fd..deba25daa73 100644
--- a/source/blender/editors/sculpt_paint/paint_ops.c
+++ b/source/blender/editors/sculpt_paint/paint_ops.c
@@ -413,6 +413,8 @@ static bool palette_sort_poll(bContext *C)
static int palette_sort_exec(bContext *C, wmOperator *op)
{
+ const int type = RNA_enum_get(op->ptr, "type");
+
Paint *paint = BKE_paint_get_active_from_context(C);
Palette *palette = paint->palette;
@@ -441,7 +443,12 @@ static int palette_sort_exec(bContext *C, wmOperator *op)
t++;
}
/* Sort */
- BKE_palette_sort_hsv(color_array, totcol);
+ if (type == -1) {
+ BKE_palette_sort_hsv(color_array, totcol);
+ }
+ else {
+ BKE_palette_sort_svh(color_array, totcol);
+ }
/* Clear old color swatches. */
PaletteColor *color_next = NULL;
@@ -472,6 +479,12 @@ static int palette_sort_exec(bContext *C, wmOperator *op)
void PALETTE_OT_sort(wmOperatorType *ot)
{
+ static const EnumPropertyItem sort_type[] = {
+ {-1, "HSV", 0, "Hue, Saturation, Value", ""},
+ {1, "SVH", 0, "Saturation, Value, Hue", ""},
+ {0, NULL, 0, NULL, NULL},
+ };
+
/* identifiers */
ot->name = "Sort Palette";
ot->idname = "PALETTE_OT_sort";
@@ -483,6 +496,8 @@ void PALETTE_OT_sort(wmOperatorType *ot)
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+ ot->prop = RNA_def_enum(ot->srna, "type", sort_type, -1, "Type", "");
}
/* Move colors in palette. */
More information about the Bf-blender-cvs
mailing list