[Bf-blender-cvs] [d76e64aad43] greasepencil-object: GPencil: Add new Value and Luminance sorting for Palettes

Antonio Vazquez noreply at git.blender.org
Wed Feb 19 18:02:12 CET 2020


Commit: d76e64aad4325eeef009d83f5b8ae81be29eaa52
Author: Antonio Vazquez
Date:   Wed Feb 19 18:02:02 2020 +0100
Branches: greasepencil-object
https://developer.blender.org/rBd76e64aad4325eeef009d83f5b8ae81be29eaa52

GPencil: Add new Value and Luminance sorting for Palettes

===================================================================

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 c26d8f8ec57..ae305720142 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -146,6 +146,8 @@ 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);
+void BKE_palette_sort_vhs(struct tPaletteColorHSV *color_array, const int totcol);
+void BKE_palette_sort_luminance(struct tPaletteColorHSV *color_array, const int totcol);
 bool BKE_palette_from_hash(struct Main *bmain,
                            struct GHash *color_table,
                            const char *name,
diff --git a/source/blender/blenkernel/intern/paint.c b/source/blender/blenkernel/intern/paint.c
index 327ab9d30d4..3ca58928933 100644
--- a/source/blender/blenkernel/intern/paint.c
+++ b/source/blender/blenkernel/intern/paint.c
@@ -754,6 +754,54 @@ static int palettecolor_compare_svh(const void *a1, const void *a2)
   return 0;
 }
 
+static int palettecolor_compare_vhs(const void *a1, const void *a2)
+{
+  const tPaletteColorHSV *ps1 = a1, *ps2 = a2;
+
+  /* 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;
+  }
+
+  /* Saturation. */
+  if (ps1->s > ps2->s) {
+    return 1;
+  }
+  else if (ps1->s < ps2->s) {
+    return -1;
+  }
+
+  return 0;
+}
+
+static int palettecolor_compare_luminance(const void *a1, const void *a2)
+{
+  const tPaletteColorHSV *ps1 = a1, *ps2 = a2;
+
+  float lumi1 = (ps1->rgb[0] + ps1->rgb[1] + ps1->rgb[2]) / 3.0f;
+  float lumi2 = (ps2->rgb[0] + ps2->rgb[1] + ps2->rgb[2]) / 3.0f;
+
+  if (lumi1 > lumi2) {
+    return 1;
+  }
+  else if (lumi1 < lumi2) {
+    return -1;
+  }
+
+  return 0;
+}
+
 void BKE_palette_sort_hsv(tPaletteColorHSV *color_array, const int totcol)
 {
   /* Sort by Hue , Saturation and Value. */
@@ -766,6 +814,18 @@ void BKE_palette_sort_svh(tPaletteColorHSV *color_array, const int totcol)
   qsort(color_array, totcol, sizeof(tPaletteColorHSV), palettecolor_compare_svh);
 }
 
+void BKE_palette_sort_vhs(tPaletteColorHSV *color_array, const int totcol)
+{
+  /* Sort by Saturation, Value and Hue. */
+  qsort(color_array, totcol, sizeof(tPaletteColorHSV), palettecolor_compare_vhs);
+}
+
+void BKE_palette_sort_luminance(tPaletteColorHSV *color_array, const int totcol)
+{
+  /* Sort by Luminance (calculated with the average, enough for sorting). */
+  qsort(color_array, totcol, sizeof(tPaletteColorHSV), palettecolor_compare_luminance);
+}
+
 bool BKE_palette_from_hash(Main *bmain, GHash *color_table, const char *name, const bool linear)
 {
   tPaletteColorHSV *color_array = NULL;
diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index 232c86f22bc..87d37746a75 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -5389,13 +5389,15 @@ static void ui_template_palette_menu(bContext *C, uiLayout *layout, void *but_p)
 {
   uiLayout *row;
 
-  uiItemL(layout, IFACE_("Sort by"), ICON_NONE);
+  uiItemL(layout, IFACE_("Sort by:"), ICON_NONE);
   row = uiLayoutRow(layout, false);
-  uiItemEnumO_value(
-      row, IFACE_("Hue, Saturation, Value"), ICON_NONE, "PALETTE_OT_sort", "type", 1);
+  uiItemEnumO_value(row, IFACE_("Hue"), ICON_NONE, "PALETTE_OT_sort", "type", 1);
   row = uiLayoutRow(layout, false);
-  uiItemEnumO_value(
-      row, IFACE_("Saturation, Value, Hue"), ICON_NONE, "PALETTE_OT_sort", "type", -1);
+  uiItemEnumO_value(row, IFACE_("Saturation"), ICON_NONE, "PALETTE_OT_sort", "type", 2);
+  row = uiLayoutRow(layout, false);
+  uiItemEnumO_value(row, IFACE_("Value"), ICON_NONE, "PALETTE_OT_sort", "type", 3);
+  row = uiLayoutRow(layout, false);
+  uiItemEnumO_value(row, IFACE_("Luminance"), ICON_NONE, "PALETTE_OT_sort", "type", 4);
 }
 
 void uiTemplatePalette(uiLayout *layout,
diff --git a/source/blender/editors/sculpt_paint/paint_ops.c b/source/blender/editors/sculpt_paint/paint_ops.c
index e615f60a9ed..bd82f05c34b 100644
--- a/source/blender/editors/sculpt_paint/paint_ops.c
+++ b/source/blender/editors/sculpt_paint/paint_ops.c
@@ -420,12 +420,18 @@ static int palette_sort_exec(bContext *C, wmOperator *op)
       t++;
     }
     /* Sort */
-    if (type == -1) {
+    if (type == 1) {
       BKE_palette_sort_hsv(color_array, totcol);
     }
-    else {
+    else if (type == 2) {
       BKE_palette_sort_svh(color_array, totcol);
     }
+    else if (type == 3) {
+      BKE_palette_sort_vhs(color_array, totcol);
+    }
+    else {
+      BKE_palette_sort_luminance(color_array, totcol);
+    }
 
     /* Clear old color swatches. */
     PaletteColor *color_next = NULL;
@@ -457,15 +463,17 @@ static int palette_sort_exec(bContext *C, wmOperator *op)
 static void PALETTE_OT_sort(wmOperatorType *ot)
 {
   static const EnumPropertyItem sort_type[] = {
-      {-1, "HSV", 0, "Hue, Saturation, Value", ""},
-      {1, "SVH", 0, "Saturation, Value, Hue", ""},
+      {1, "HSV", 0, "Hue, Saturation, Value", ""},
+      {2, "SVH", 0, "Saturation, Value, Hue", ""},
+      {3, "VHS", 0, "Value, Hue, Saturation", ""},
+      {4, "LUMINANCE", 0, "Luminance", ""},
       {0, NULL, 0, NULL, NULL},
   };
 
   /* identifiers */
   ot->name = "Sort Palette";
   ot->idname = "PALETTE_OT_sort";
-  ot->description = "Sort Palette Colors by Hue, Saturation and Value";
+  ot->description = "Sort Palette Colors";
 
   /* api callbacks */
   ot->exec = palette_sort_exec;



More information about the Bf-blender-cvs mailing list