[Bf-blender-cvs] [deefd7fb312] greasepencil-object: GPencil: Some tweaks to Sort and Join Palettes

Antonio Vazquez noreply at git.blender.org
Mon Nov 18 09:52:20 CET 2019


Commit: deefd7fb31211bb31ed73461f495fc0cd8a112da
Author: Antonio Vazquez
Date:   Mon Nov 18 09:52:14 2019 +0100
Branches: greasepencil-object
https://developer.blender.org/rBdeefd7fb31211bb31ed73461f495fc0cd8a112da

GPencil: Some tweaks to Sort and Join Palettes

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

M	source/blender/blenkernel/BKE_paint.h
M	source/blender/blenkernel/intern/paint.c
M	source/blender/editors/sculpt_paint/paint_ops.c
M	source/blender/makesdna/DNA_brush_types.h

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

diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 2f22caa805d..2875fc0e128 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -52,7 +52,7 @@ struct SubdivCCG;
 struct SubdivCCG;
 struct Tex;
 struct ToolSettings;
-struct tPaletteColorHue;
+struct tPaletteColorHSV;
 struct UnifiedPaintSettings;
 struct View3D;
 struct ViewLayer;
@@ -128,7 +128,7 @@ bool BKE_palette_is_empty(const struct Palette *palette);
 void BKE_palette_color_remove(struct Palette *palette, struct PaletteColor *color);
 void BKE_palette_clear(struct Palette *palette);
 
-void BKE_palette_sort_hs(struct tPaletteColorHue *color_array, const int totcol);
+void BKE_palette_sort_hsv(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 3f999dfcae2..a6ff693eaa1 100644
--- a/source/blender/blenkernel/intern/paint.c
+++ b/source/blender/blenkernel/intern/paint.c
@@ -648,11 +648,11 @@ bool BKE_palette_is_empty(const struct Palette *palette)
 }
 
 /* helper function to sort using qsort */
-static int palettecolor_compare_hue_sat(const void *a1, const void *a2)
+static int palettecolor_compare_hsv(const void *a1, const void *a2)
 {
-  const tPaletteColorHue *ps1 = a1, *ps2 = a2;
-  int a = ps1->hue * 1e6 + ps1->sat * 1e3;
-  int b = ps2->hue * 1e6 + ps2->sat * 1e3;
+  const tPaletteColorHSV *ps1 = a1, *ps2 = a2;
+  uint a = hsv_to_cpack(ps1->h, ps1->s, ps1->v);
+  uint b = hsv_to_cpack(ps2->h, ps2->s, ps2->v);
 
   if (a < b) {
     return -1;
@@ -664,22 +664,22 @@ static int palettecolor_compare_hue_sat(const void *a1, const void *a2)
   return 0;
 }
 
-void BKE_palette_sort_hs(tPaletteColorHue *color_array, const int totcol)
+void BKE_palette_sort_hsv(tPaletteColorHSV *color_array, const int totcol)
 {
-  /* Sort by Hue and saturation. */
-  qsort(color_array, totcol, sizeof(tPaletteColorHue), palettecolor_compare_hue_sat);
+  /* Sort by Hue , Saturation and Value. */
+  qsort(color_array, totcol, sizeof(tPaletteColorHSV), palettecolor_compare_hsv);
 }
 
 bool BKE_palette_from_hash(Main *bmain, GHash *color_table)
 {
-  tPaletteColorHue *color_array = NULL;
-  tPaletteColorHue *col_elm = NULL;
+  tPaletteColorHSV *color_array = NULL;
+  tPaletteColorHSV *col_elm = NULL;
   bool done = false;
 
   const int totpal = BLI_ghash_len(color_table);
 
   if (totpal > 0) {
-    color_array = MEM_calloc_arrayN(totpal, sizeof(tPaletteColorHue), __func__);
+    color_array = MEM_calloc_arrayN(totpal, sizeof(tPaletteColorHSV), __func__);
     /* Put all colors in an array. */
     GHashIterator gh_iter;
     int t = 0;
@@ -694,8 +694,9 @@ bool BKE_palette_from_hash(Main *bmain, GHash *color_table)
       col_elm->rgb[0] = r;
       col_elm->rgb[1] = g;
       col_elm->rgb[2] = b;
-      col_elm->hue = h;
-      col_elm->sat = s;
+      col_elm->h = h;
+      col_elm->s = s;
+      col_elm->v = v;
       t++;
     }
   }
@@ -703,7 +704,7 @@ bool BKE_palette_from_hash(Main *bmain, GHash *color_table)
   /* Create the Palette. */
   if (totpal > 0) {
     /* Sort by Hue and saturation. */
-    BKE_palette_sort_hs(color_array, totpal);
+    BKE_palette_sort_hsv(color_array, totpal);
 
     Palette *palette = BKE_palette_add(bmain, "Palette");
     if (palette) {
diff --git a/source/blender/editors/sculpt_paint/paint_ops.c b/source/blender/editors/sculpt_paint/paint_ops.c
index 4caaf00c90a..4423719b2fb 100644
--- a/source/blender/editors/sculpt_paint/paint_ops.c
+++ b/source/blender/editors/sculpt_paint/paint_ops.c
@@ -420,13 +420,13 @@ static int palette_sort_exec(bContext *C, wmOperator *op)
     return OPERATOR_CANCELLED;
   }
 
-  tPaletteColorHue *color_array = NULL;
-  tPaletteColorHue *col_elm = NULL;
+  tPaletteColorHSV *color_array = NULL;
+  tPaletteColorHSV *col_elm = NULL;
 
   const int totcol = BLI_listbase_count(&palette->colors);
 
   if (totcol > 0) {
-    color_array = MEM_calloc_arrayN(totcol, sizeof(tPaletteColorHue), __func__);
+    color_array = MEM_calloc_arrayN(totcol, sizeof(tPaletteColorHSV), __func__);
     /* Put all colors in an array. */
     int t = 0;
     for (PaletteColor *color = palette->colors.first; color; color = color->next) {
@@ -434,12 +434,14 @@ static int palette_sort_exec(bContext *C, wmOperator *op)
       rgb_to_hsv(color->rgb[0], color->rgb[1], color->rgb[2], &h, &s, &v);
       col_elm = &color_array[t];
       copy_v3_v3(col_elm->rgb, color->rgb);
-      col_elm->hue = h;
-      col_elm->sat = s;
+      col_elm->value = color->value;
+      col_elm->h = h;
+      col_elm->s = s;
+      col_elm->v = v;
       t++;
     }
     /* Sort */
-    BKE_palette_sort_hs(color_array, totcol);
+    BKE_palette_sort_hsv(color_array, totcol);
 
     /* Clear old color swatches. */
     BLI_listbase_clear(&palette->colors);
@@ -467,9 +469,9 @@ static int palette_sort_exec(bContext *C, wmOperator *op)
 void PALETTE_OT_sort(wmOperatorType *ot)
 {
   /* identifiers */
-  ot->name = "Sort Palette by Hue and Saturation";
+  ot->name = "Sort Palette";
   ot->idname = "PALETTE_OT_sort";
-  ot->description = "Sort Palette Colors by Hue and Saturation";
+  ot->description = "Sort Palette Colors by Hue, Saturation and Value";
 
   /* api callbacks */
   ot->exec = palette_sort_exec;
@@ -518,6 +520,7 @@ static int palette_join_exec(bContext *C, wmOperator *op)
       PaletteColor *palcol = BKE_palette_color_add(palette);
       if (palcol) {
         copy_v3_v3(palcol->rgb, color->rgb);
+        palcol->value = color->value;
         done = true;
       }
     }
diff --git a/source/blender/makesdna/DNA_brush_types.h b/source/blender/makesdna/DNA_brush_types.h
index e7bc5eddaf7..3d312a8f563 100644
--- a/source/blender/makesdna/DNA_brush_types.h
+++ b/source/blender/makesdna/DNA_brush_types.h
@@ -370,11 +370,13 @@ typedef struct Brush {
 } Brush;
 
 /* Struct to hold palette colors for sorting. */
-typedef struct tPaletteColorHue {
+typedef struct tPaletteColorHSV {
   float rgb[3];
-  float hue;
-  float sat;
-} tPaletteColorHue;
+  float value;
+  float h;
+  float s;
+  float v;
+} tPaletteColorHSV;
 
 typedef struct PaletteColor {
   struct PaletteColor *next, *prev;
@@ -545,7 +547,7 @@ typedef enum eBrushUVSculptTool {
         SCULPT_TOOL_POSE, \
 \
         /* These brushes could handle dynamic topology, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
-         * \ but user feedback indicates it's better not to */ \
+         * \ \ \ but user feedback indicates it's better not to */ \
         SCULPT_TOOL_SMOOTH, \
         SCULPT_TOOL_MASK) == 0)



More information about the Bf-blender-cvs mailing list