[Bf-blender-cvs] [a241948ad96] master: Fix errors ensuring grease pencil palette

Campbell Barton noreply at git.blender.org
Tue Sep 1 07:29:54 CEST 2020


Commit: a241948ad96be3a9cdf04a0ee53605d3ff4dd82b
Author: Campbell Barton
Date:   Tue Sep 1 14:40:07 2020 +1000
Branches: master
https://developer.blender.org/rBa241948ad96be3a9cdf04a0ee53605d3ff4dd82b

Fix errors ensuring grease pencil palette

- Direct assignment caused ID user counts to be invalid.
- The first palette would always be used,
  even when the named palette searched for was found.

Also pass 'const' string to `hex_to_rgb`, avoid casting to 'non-const'.

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

M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/blenlib/BLI_math_color.h
M	source/blender/blenlib/intern/math_color.c
M	source/blender/editors/interface/interface_eyedropper_gpencil_color.c

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

diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 10b6328ead4..1e37ae3892b 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -2110,7 +2110,6 @@ int BKE_gpencil_object_material_index_get(Object *ob, Material *ma)
  */
 void BKE_gpencil_palette_ensure(Main *bmain, Scene *scene)
 {
-  const int totcol = 120;
   const char *hexcol[] = {
       "FFFFFF", "F2F2F2", "E6E6E6", "D9D9D9", "CCCCCC", "BFBFBF", "B2B2B2", "A6A6A6", "999999",
       "8C8C8C", "808080", "737373", "666666", "595959", "4C4C4C", "404040", "333333", "262626",
@@ -2128,33 +2127,34 @@ void BKE_gpencil_palette_ensure(Main *bmain, Scene *scene)
       "0000FF", "3F007F", "00007F"};
 
   ToolSettings *ts = scene->toolsettings;
-  GpPaint *gp_paint = ts->gp_paint;
-  Paint *paint = &gp_paint->paint;
-
-  if (paint->palette != NULL) {
+  if (ts->gp_paint->paint.palette != NULL) {
     return;
   }
 
-  paint->palette = BLI_findstring(&bmain->palettes, "Palette", offsetof(ID, name) + 2);
-  /* Try with first palette. */
-  if (bmain->palettes.first != NULL) {
-    paint->palette = bmain->palettes.first;
-    ts->gp_vertexpaint->paint.palette = paint->palette;
-    return;
+  /* Try to find the default palette. */
+  const char *palette_id = "Palette";
+  struct Palette *palette = BLI_findstring(&bmain->palettes, palette_id, offsetof(ID, name) + 2);
+
+  if (palette == NULL) {
+    /* Fall back to the first palette. */
+    palette = bmain->palettes.first;
   }
 
-  if (paint->palette == NULL) {
-    paint->palette = BKE_palette_add(bmain, "Palette");
-    ts->gp_vertexpaint->paint.palette = paint->palette;
+  if (palette == NULL) {
+    /* Fall back to creating a palette. */
+    palette = BKE_palette_add(bmain, palette_id);
+    id_us_min(&palette->id);
 
     /* Create Colors. */
-    for (int i = 0; i < totcol; i++) {
-      PaletteColor *palcol = BKE_palette_color_add(paint->palette);
-      if (palcol) {
-        hex_to_rgb((char *)hexcol[i], palcol->rgb, palcol->rgb + 1, palcol->rgb + 2);
-      }
+    for (int i = 0; i < ARRAY_SIZE(hexcol); i++) {
+      PaletteColor *palcol = BKE_palette_color_add(palette);
+      hex_to_rgb(hexcol[i], palcol->rgb, palcol->rgb + 1, palcol->rgb + 2);
     }
   }
+
+  BLI_assert(palette != NULL);
+  BKE_paint_palette_set(&ts->gp_paint->paint, palette);
+  BKE_paint_palette_set(&ts->gp_vertexpaint->paint, palette);
 }
 
 /**
diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h
index 943f0fc764f..7b48b62b6e7 100644
--- a/source/blender/blenlib/BLI_math_color.h
+++ b/source/blender/blenlib/BLI_math_color.h
@@ -47,7 +47,7 @@ void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b);
 void hsv_to_rgb_v(const float hsv[3], float r_rgb[3]);
 void hsl_to_rgb(float h, float c, float l, float *r_r, float *r_g, float *r_b);
 void hsl_to_rgb_v(const float hcl[3], float r_rgb[3]);
-void hex_to_rgb(char *hexcol, float *r_r, float *r_g, float *r_b);
+void hex_to_rgb(const char *hexcol, float *r_r, float *r_g, float *r_b);
 void yuv_to_rgb(float y, float u, float v, float *r_r, float *r_g, float *r_b, int colorspace);
 void ycc_to_rgb(float y, float cb, float cr, float *r_r, float *r_g, float *r_b, int colorspace);
 void cpack_to_rgb(unsigned int col, float *r_r, float *r_g, float *r_b);
diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c
index 09bb7ea5711..4b62d6b9b5b 100644
--- a/source/blender/blenlib/intern/math_color.c
+++ b/source/blender/blenlib/intern/math_color.c
@@ -198,7 +198,7 @@ void ycc_to_rgb(float y, float cb, float cr, float *r_r, float *r_g, float *r_b,
   *r_b = b / 255.0f;
 }
 
-void hex_to_rgb(char *hexcol, float *r_r, float *r_g, float *r_b)
+void hex_to_rgb(const char *hexcol, float *r_r, float *r_g, float *r_b)
 {
   unsigned int ri, gi, bi;
 
diff --git a/source/blender/editors/interface/interface_eyedropper_gpencil_color.c b/source/blender/editors/interface/interface_eyedropper_gpencil_color.c
index aa5b4d2c255..f7c41a7142b 100644
--- a/source/blender/editors/interface/interface_eyedropper_gpencil_color.c
+++ b/source/blender/editors/interface/interface_eyedropper_gpencil_color.c
@@ -38,6 +38,7 @@
 
 #include "BKE_context.h"
 #include "BKE_gpencil.h"
+#include "BKE_lib_id.h"
 #include "BKE_main.h"
 #include "BKE_material.h"
 #include "BKE_paint.h"
@@ -208,9 +209,13 @@ static void eyedropper_add_palette_color(bContext *C, const float col_conv[4])
 
   /* Check for Palette in Draw and Vertex Paint Mode. */
   if (paint->palette == NULL) {
-    paint->palette = BKE_palette_add(bmain, "Grease Pencil");
+    Palette *palette = BKE_palette_add(bmain, "Grease Pencil");
+    id_us_min(&palette->id);
+
+    BKE_paint_palette_set(paint, palette);
+
     if (vertexpaint->palette == NULL) {
-      vertexpaint->palette = paint->palette;
+      BKE_paint_palette_set(vertexpaint, palette);
     }
   }
   /* Check if the color exist already. */



More information about the Bf-blender-cvs mailing list