[gimp/blend-tool-fun: 91/163] app: save color history when a color is used.



commit ffb81910d0c3620afeca450501ddd0d9217ed7d0
Author: Jehan <jehan girinstud io>
Date:   Tue Sep 29 13:43:15 2015 +0200

    app: save color history when a color is used.
    
    Now the history of recently used colors is not updated when selecting
    in the color dialog anymore, but when a color is actually used.
    Tools supported right now are: Ink, MyPaint brush, all PaintBrush tools,
    bucket fill and eraser (background color on non-alpha drawables).
    Moreover from now on, colors already saved are properly moved to first
    position when reused.

 app/core/gimp-palettes.c            |    9 +++++
 app/core/gimp-palettes.h            |   10 +++--
 app/core/gimpdrawable-bucket-fill.c |    6 +++-
 app/core/gimppalette.c              |   52 ++++++++++++++++++++++++++++
 app/core/gimppalette.h              |    4 ++
 app/core/gimppalettemru.c           |   15 ++++++--
 app/paint/gimperaser.c              |   17 +++++++++
 app/paint/gimpink.c                 |   48 +++++++++++++++-----------
 app/paint/gimpmybrush.c             |   64 ++++++++++++++++++++---------------
 app/paint/gimppaintbrush.c          |   23 ++++++++++++
 app/widgets/gimpcolordialog.c       |    2 -
 11 files changed, 193 insertions(+), 57 deletions(-)
---
diff --git a/app/core/gimp-palettes.c b/app/core/gimp-palettes.c
index 2a6b716..52cdb39 100644
--- a/app/core/gimp-palettes.c
+++ b/app/core/gimp-palettes.c
@@ -110,6 +110,15 @@ gimp_palettes_get_color_history (Gimp *gimp)
   return g_object_get_data (G_OBJECT (gimp), COLOR_HISTORY_KEY);
 }
 
+void
+gimp_palettes_add_color_history (Gimp    *gimp,
+                                 GimpRGB *color)
+{
+  GimpPalette *history;
+
+  history = gimp_palettes_get_color_history (gimp);
+  gimp_palette_mru_add (GIMP_PALETTE_MRU (history), color);
+}
 
 /*  private functions  */
 
diff --git a/app/core/gimp-palettes.h b/app/core/gimp-palettes.h
index 036bfa9..e789629 100644
--- a/app/core/gimp-palettes.h
+++ b/app/core/gimp-palettes.h
@@ -22,12 +22,14 @@
 #define __GIMP_PALETTES__
 
 
-void          gimp_palettes_init              (Gimp *gimp);
+void          gimp_palettes_init              (Gimp    *gimp);
 
-void          gimp_palettes_load              (Gimp *gimp);
-void          gimp_palettes_save              (Gimp *gimp);
+void          gimp_palettes_load              (Gimp    *gimp);
+void          gimp_palettes_save              (Gimp    *gimp);
 
-GimpPalette * gimp_palettes_get_color_history (Gimp *gimp);
+GimpPalette * gimp_palettes_get_color_history (Gimp    *gimp);
+void          gimp_palettes_add_color_history (Gimp    *gimp,
+                                               GimpRGB *color);
 
 
 #endif /* __GIMP_PALETTES__ */
diff --git a/app/core/gimpdrawable-bucket-fill.c b/app/core/gimpdrawable-bucket-fill.c
index 5889eeb..c4253a8 100644
--- a/app/core/gimpdrawable-bucket-fill.c
+++ b/app/core/gimpdrawable-bucket-fill.c
@@ -33,6 +33,7 @@
 #include "gegl/gimp-gegl-utils.h"
 
 #include "gimp.h"
+#include "gimp-palettes.h"
 #include "gimp-utils.h"
 #include "gimpchannel-combine.h"
 #include "gimpcontext.h"
@@ -76,8 +77,8 @@ gimp_drawable_bucket_fill (GimpDrawable         *drawable,
                            gdouble               y,
                            GError              **error)
 {
-  GimpRGB      color;
   GimpPattern *pattern;
+  GimpRGB      color;
 
   g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
   g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
@@ -87,6 +88,9 @@ gimp_drawable_bucket_fill (GimpDrawable         *drawable,
   if (! gimp_get_fill_params (context, fill_type, &color, &pattern, error))
     return FALSE;
 
+  gimp_palettes_add_color_history (context->gimp,
+                                   &color);
+
   gimp_drawable_bucket_fill_internal (drawable,
                                       fill_type,
                                       paint_mode, opacity,
diff --git a/app/core/gimppalette.c b/app/core/gimppalette.c
index a7a2c70..bfe7271 100644
--- a/app/core/gimppalette.c
+++ b/app/core/gimppalette.c
@@ -382,6 +382,58 @@ gimp_palette_get_n_colors (GimpPalette *palette)
   return palette->n_colors;
 }
 
+void
+gimp_palette_move_entry (GimpPalette      *palette,
+                         GimpPaletteEntry *entry,
+                         gint              position)
+{
+  GList *list;
+  gint   pos = 0;
+
+  g_return_if_fail (GIMP_IS_PALETTE (palette));
+  g_return_if_fail (entry != NULL);
+
+  if (g_list_find (palette->colors, entry))
+    {
+      pos = entry->position;
+
+      if (entry->position == position)
+        return;
+
+      entry->position = position;
+      palette->colors = g_list_remove (palette->colors,
+                                       entry);
+      palette->colors = g_list_insert (palette->colors,
+                                       entry, position);
+
+      if (pos < position)
+        {
+          for (list = g_list_nth (palette->colors, pos);
+               list && pos < position;
+               list = g_list_next (list))
+            {
+              entry = (GimpPaletteEntry *) list->data;
+
+              entry->position = pos++;
+            }
+        }
+      else
+        {
+          for (list = g_list_nth (palette->colors, position + 1);
+               list && position < pos;
+               list = g_list_next (list))
+            {
+              entry = (GimpPaletteEntry *) list->data;
+
+              entry->position += 1;
+              pos--;
+            }
+        }
+
+      gimp_data_dirty (GIMP_DATA (palette));
+    }
+}
+
 GimpPaletteEntry *
 gimp_palette_add_entry (GimpPalette   *palette,
                         gint           position,
diff --git a/app/core/gimppalette.h b/app/core/gimppalette.h
index 155901c..a573554 100644
--- a/app/core/gimppalette.h
+++ b/app/core/gimppalette.h
@@ -67,6 +67,10 @@ GimpData         * gimp_palette_get_standard    (GimpContext      *context);
 GList            * gimp_palette_get_colors      (GimpPalette      *palette);
 gint               gimp_palette_get_n_colors    (GimpPalette      *palette);
 
+void               gimp_palette_move_entry      (GimpPalette      *palette,
+                                                 GimpPaletteEntry *entry,
+                                                 gint              position);
+
 GimpPaletteEntry * gimp_palette_add_entry       (GimpPalette      *palette,
                                                  gint              position,
                                                  const gchar      *name,
diff --git a/app/core/gimppalettemru.c b/app/core/gimppalettemru.c
index 300c296..2fb6426 100644
--- a/app/core/gimppalettemru.c
+++ b/app/core/gimppalettemru.c
@@ -241,7 +241,16 @@ gimp_palette_mru_add (GimpPaletteMru *mru,
  doit:
 
   if (found)
-    gimp_palette_delete_entry (palette, found);
-
-  found = gimp_palette_add_entry (palette, 0, _("History Color"), color);
+    {
+      gimp_palette_move_entry (palette, found, 0);
+      /* Even though they are nearly the same color, let's make them exactly
+      * equal. */
+      gimp_palette_set_entry_color (palette,
+                                    0,
+                                    color);
+    }
+  else
+    {
+      gimp_palette_add_entry (palette, 0, _("History Color"), color);
+    }
 }
diff --git a/app/paint/gimperaser.c b/app/paint/gimperaser.c
index 00ff7e9..b3c6e46 100644
--- a/app/paint/gimperaser.c
+++ b/app/paint/gimperaser.c
@@ -25,6 +25,7 @@
 #include "gegl/gimp-gegl-utils.h"
 
 #include "core/gimp.h"
+#include "core/gimp-palettes.h"
 #include "core/gimpdrawable.h"
 #include "core/gimpdynamics.h"
 #include "core/gimpimage.h"
@@ -88,6 +89,22 @@ gimp_eraser_paint (GimpPaintCore    *paint_core,
 {
   switch (paint_state)
     {
+    case GIMP_PAINT_STATE_INIT:
+        {
+          if (! gimp_drawable_has_alpha (drawable))
+            {
+              /* Erasing on a drawable without alpha is equivalent to
+               * drawing with background color. So let's save history. */
+              GimpContext *context = GIMP_CONTEXT (paint_options);
+              GimpRGB      background;
+
+              gimp_context_get_background (context, &background);
+              gimp_palettes_add_color_history (context->gimp,
+                                               &background);
+
+            }
+        }
+      break;
     case GIMP_PAINT_STATE_MOTION:
       gimp_eraser_motion (paint_core, drawable, paint_options, coords);
       break;
diff --git a/app/paint/gimpink.c b/app/paint/gimpink.c
index dffbc5c..ee9cc4f 100644
--- a/app/paint/gimpink.c
+++ b/app/paint/gimpink.c
@@ -28,6 +28,7 @@
 
 #include "gegl/gimp-gegl-utils.h"
 
+#include "core/gimp-palettes.h"
 #include "core/gimpdrawable.h"
 #include "core/gimpimage.h"
 #include "core/gimpimage-undo.h"
@@ -153,34 +154,41 @@ gimp_ink_paint (GimpPaintCore    *paint_core,
 
   switch (paint_state)
     {
-
     case GIMP_PAINT_STATE_INIT:
-
-      if (coords->x == last_coords.x &&
-          coords->y == last_coords.y)
         {
-          /*  start with new blobs if we're not interpolating  */
+          GimpContext *context = GIMP_CONTEXT (paint_options);
+          GimpRGB      foreground;
 
-          if (ink->start_blob)
-            {
-              g_free (ink->start_blob);
-              ink->start_blob = NULL;
-            }
+          gimp_context_get_foreground (context, &foreground);
+          gimp_palettes_add_color_history (context->gimp,
+                                           &foreground);
 
-          if (ink->last_blob)
+          if (coords->x == last_coords.x &&
+              coords->y == last_coords.y)
             {
-              g_free (ink->last_blob);
-              ink->last_blob = NULL;
+              /*  start with new blobs if we're not interpolating  */
+
+              if (ink->start_blob)
+                {
+                  g_free (ink->start_blob);
+                  ink->start_blob = NULL;
+                }
+
+              if (ink->last_blob)
+                {
+                  g_free (ink->last_blob);
+                  ink->last_blob = NULL;
+                }
             }
-        }
-      else if (ink->last_blob)
-        {
-          /*  save the start blob of the line for undo otherwise  */
+          else if (ink->last_blob)
+            {
+              /*  save the start blob of the line for undo otherwise  */
 
-          if (ink->start_blob)
-            g_free (ink->start_blob);
+              if (ink->start_blob)
+                g_free (ink->start_blob);
 
-          ink->start_blob = gimp_blob_duplicate (ink->last_blob);
+              ink->start_blob = gimp_blob_duplicate (ink->last_blob);
+            }
         }
       break;
 
diff --git a/app/paint/gimpmybrush.c b/app/paint/gimpmybrush.c
index 229e90b..69aa5d9 100644
--- a/app/paint/gimpmybrush.c
+++ b/app/paint/gimpmybrush.c
@@ -39,6 +39,7 @@
 #include "config/gimpguiconfig.h" /* playground */
 
 #include "core/gimp.h"
+#include "core/gimp-palettes.h"
 #include "core/gimpdrawable.h"
 #include "core/gimpimage.h"
 #include "core/gimpimage-undo.h"
@@ -123,38 +124,47 @@ gimp_mybrush_paint (GimpPaintCore    *paint_core,
   switch (paint_state)
     {
     case GIMP_PAINT_STATE_INIT:
-      mybrush->private->surface = mypaint_gegl_tiled_surface_new ();
-
-      buffer = mypaint_gegl_tiled_surface_get_buffer (mybrush->private->surface);
-      buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
-                                                gimp_item_get_width (GIMP_ITEM (drawable)),
-                                                gimp_item_get_height (GIMP_ITEM (drawable))),
-                                gegl_buffer_get_format (buffer));
-      gegl_buffer_copy (gimp_drawable_get_buffer (drawable), NULL,
-                        GEGL_ABYSS_NONE,
-                        buffer, NULL);
-      mypaint_gegl_tiled_surface_set_buffer (mybrush->private->surface, buffer);
-      g_object_unref (buffer);
-
-      mybrush->private->brush = mypaint_brush_new ();
-      mypaint_brush_from_defaults (mybrush->private->brush);
-
-      if (options->mybrush)
         {
-          gchar *string;
-          gsize  length;
-
-          if (g_file_get_contents (options->mybrush,
-                                   &string, &length, NULL))
+          GimpContext *context = GIMP_CONTEXT (paint_options);
+          GimpRGB      foreground;
+
+          gimp_context_get_foreground (context, &foreground);
+          gimp_palettes_add_color_history (context->gimp,
+                                           &foreground);
+
+          mybrush->private->surface = mypaint_gegl_tiled_surface_new ();
+
+          buffer = mypaint_gegl_tiled_surface_get_buffer (mybrush->private->surface);
+          buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
+                                                    gimp_item_get_width (GIMP_ITEM (drawable)),
+                                                    gimp_item_get_height (GIMP_ITEM (drawable))),
+                                    gegl_buffer_get_format (buffer));
+          gegl_buffer_copy (gimp_drawable_get_buffer (drawable), NULL,
+                            GEGL_ABYSS_NONE,
+                            buffer, NULL);
+          mypaint_gegl_tiled_surface_set_buffer (mybrush->private->surface, buffer);
+          g_object_unref (buffer);
+
+          mybrush->private->brush = mypaint_brush_new ();
+          mypaint_brush_from_defaults (mybrush->private->brush);
+
+          if (options->mybrush)
             {
-              if (! mypaint_brush_from_string (mybrush->private->brush, string))
-                g_printerr ("Failed to deserialize MyPaint brush\n");
+              gchar *string;
+              gsize  length;
 
-              g_free (string);
+              if (g_file_get_contents (options->mybrush,
+                                       &string, &length, NULL))
+                {
+                  if (! mypaint_brush_from_string (mybrush->private->brush, string))
+                    g_printerr ("Failed to deserialize MyPaint brush\n");
+
+                  g_free (string);
+                }
             }
-        }
 
-      mypaint_brush_new_stroke (mybrush->private->brush);
+          mypaint_brush_new_stroke (mybrush->private->brush);
+        }
       break;
 
     case GIMP_PAINT_STATE_MOTION:
diff --git a/app/paint/gimppaintbrush.c b/app/paint/gimppaintbrush.c
index c86995f..a41c278 100644
--- a/app/paint/gimppaintbrush.c
+++ b/app/paint/gimppaintbrush.c
@@ -30,6 +30,7 @@
 #include "gegl/gimp-gegl-utils.h"
 
 #include "core/gimp.h"
+#include "core/gimp-palettes.h"
 #include "core/gimpbrush.h"
 #include "core/gimpdrawable.h"
 #include "core/gimpdynamics.h"
@@ -92,6 +93,28 @@ gimp_paintbrush_paint (GimpPaintCore    *paint_core,
 {
   switch (paint_state)
     {
+    case GIMP_PAINT_STATE_INIT:
+        {
+          GimpContext   *context = GIMP_CONTEXT (paint_options);
+          GimpBrushCore *brush_core = GIMP_BRUSH_CORE (paint_core);
+          GimpDynamics  *dynamics;
+
+          dynamics = gimp_context_get_dynamics (GIMP_CONTEXT (paint_options));
+
+          if (! gimp_dynamics_is_output_enabled (dynamics, GIMP_DYNAMICS_OUTPUT_COLOR) &&
+              (! brush_core->brush || ! gimp_brush_get_pixmap (brush_core->brush)))
+            {
+              /* We don't save gradient color history and
+               * pixmap brushes have no color to save.
+               */
+              GimpRGB foreground;
+
+              gimp_context_get_foreground (context, &foreground);
+              gimp_palettes_add_color_history (context->gimp,
+                                               &foreground);
+            }
+        }
+      break;
     case GIMP_PAINT_STATE_MOTION:
       _gimp_paintbrush_motion (paint_core, drawable, paint_options, coords,
                                GIMP_OPACITY_OPAQUE);
diff --git a/app/widgets/gimpcolordialog.c b/app/widgets/gimpcolordialog.c
index d630d08..4456e52 100644
--- a/app/widgets/gimpcolordialog.c
+++ b/app/widgets/gimpcolordialog.c
@@ -221,8 +221,6 @@ gimp_color_dialog_response (GtkDialog *gtk_dialog,
       break;
 
     case GTK_RESPONSE_OK:
-      gimp_color_history_add_clicked (NULL, dialog);
-
       gimp_color_selection_get_color (GIMP_COLOR_SELECTION (dialog->selection),
                                       &color);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]