app: don't add fg color to history in GimpEraser

Add GimpPaintbrush::get_color_history_color() virtual function,
which should return the color to be added to the color history upon
painting, if any.  The default implementation returns the
foreground color, when not using a pixmap brush or color-from-
gradient.

Override this function in GimpEraser, to return the background
color when the affected drawable has no alpha channel, instead of
overriding GimpPaintCore::paint() for this purpose.  This avoids
erroneously adding both the background and foreground colors to the
history.
This commit is contained in:
Ell 2019-05-29 04:25:38 -04:00
parent 890b554230
commit 86f4d4d41d
3 changed files with 90 additions and 85 deletions

View File

@ -38,27 +38,22 @@
#include "gimp-intl.h"
static void gimp_eraser_paint (GimpPaintCore *paint_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpPaintState paint_state,
guint32 time);
static void gimp_eraser_get_paint_params (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
static gboolean gimp_eraser_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpRGB *color);
static void gimp_eraser_get_paint_params (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
G_DEFINE_TYPE (GimpEraser, gimp_eraser, GIMP_TYPE_PAINTBRUSH)
#define parent_class gimp_eraser_parent_class
void
gimp_eraser_register (Gimp *gimp,
@ -75,12 +70,10 @@ gimp_eraser_register (Gimp *gimp,
static void
gimp_eraser_class_init (GimpEraserClass *klass)
{
GimpPaintCoreClass *paint_core_class = GIMP_PAINT_CORE_CLASS (klass);
GimpPaintbrushClass *paintbrush_class = GIMP_PAINTBRUSH_CLASS (klass);
paint_core_class->paint = gimp_eraser_paint;
paintbrush_class->get_paint_params = gimp_eraser_get_paint_params;
paintbrush_class->get_color_history_color = gimp_eraser_get_color_history_color;
paintbrush_class->get_paint_params = gimp_eraser_get_paint_params;
}
static void
@ -88,41 +81,25 @@ gimp_eraser_init (GimpEraser *eraser)
{
}
static void
gimp_eraser_paint (GimpPaintCore *paint_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpPaintState paint_state,
guint32 time)
static gboolean
gimp_eraser_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpRGB *color)
{
switch (paint_state)
/* Erasing on a drawable without alpha is equivalent to
* drawing with background color. So let's save history.
*/
if (! gimp_drawable_has_alpha (drawable))
{
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;
GimpContext *context = GIMP_CONTEXT (paint_options);
gimp_context_get_background (context, &background);
gimp_palettes_add_color_history (context->gimp,
&background);
gimp_context_get_background (context, color);
}
}
break;
default:
break;
return TRUE;
}
GIMP_PAINT_CORE_CLASS (parent_class)->paint (paint_core, drawable,
paint_options, sym,
paint_state, time);
return FALSE;
}
static void

View File

@ -46,21 +46,25 @@
#include "gimp-intl.h"
static void gimp_paintbrush_paint (GimpPaintCore *paint_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpPaintState paint_state,
guint32 time);
static void gimp_paintbrush_paint (GimpPaintCore *paint_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpPaintState paint_state,
guint32 time);
static void gimp_paintbrush_real_get_paint_params (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
static gboolean gimp_paintbrush_real_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpRGB *color);
static void gimp_paintbrush_real_get_paint_params (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
G_DEFINE_TYPE (GimpPaintbrush, gimp_paintbrush, GIMP_TYPE_BRUSH_CORE)
@ -88,6 +92,7 @@ gimp_paintbrush_class_init (GimpPaintbrushClass *klass)
brush_core_class->handles_changing_brush = TRUE;
klass->get_color_history_color = gimp_paintbrush_real_get_color_history_color;
klass->get_paint_params = gimp_paintbrush_real_get_paint_params;
}
@ -104,24 +109,21 @@ gimp_paintbrush_paint (GimpPaintCore *paint_core,
GimpPaintState paint_state,
guint32 time)
{
GimpPaintbrush *paintbrush = GIMP_PAINTBRUSH (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 = gimp_context_get_dynamics (context);
GimpRGB color;
if (! gimp_dynamics_is_output_enabled (dynamics, GIMP_DYNAMICS_OUTPUT_COLOR) &&
(! brush_core->brush || ! gimp_brush_get_pixmap (brush_core->brush)))
if (GIMP_PAINTBRUSH_GET_CLASS (paintbrush)->get_color_history_color &&
GIMP_PAINTBRUSH_GET_CLASS (paintbrush)->get_color_history_color (
paintbrush, drawable, paint_options, &color))
{
/* We don't save gradient color history and pixmap brushes
* have no color to save.
*/
GimpRGB foreground;
GimpContext *context = GIMP_CONTEXT (paint_options);
gimp_context_get_foreground (context, &foreground);
gimp_palettes_add_color_history (context->gimp, &foreground);
gimp_palettes_add_color_history (context->gimp, &color);
}
}
break;
@ -133,8 +135,6 @@ gimp_paintbrush_paint (GimpPaintCore *paint_core,
case GIMP_PAINT_STATE_FINISH:
{
GimpPaintbrush *paintbrush = GIMP_PAINTBRUSH (paint_core);
if (paintbrush->paint_buffer)
{
g_object_remove_weak_pointer (
@ -150,6 +150,30 @@ gimp_paintbrush_paint (GimpPaintCore *paint_core,
}
}
static gboolean
gimp_paintbrush_real_get_color_history_color (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpRGB *color)
{
GimpContext *context = GIMP_CONTEXT (paint_options);
GimpBrushCore *brush_core = GIMP_BRUSH_CORE (paintbrush);
GimpDynamics *dynamics = gimp_context_get_dynamics (context);
/* We don't save gradient color history and pixmap brushes
* have no color to save.
*/
if (gimp_dynamics_is_output_enabled (dynamics, GIMP_DYNAMICS_OUTPUT_COLOR) ||
(brush_core->brush && gimp_brush_get_pixmap (brush_core->brush)))
{
return FALSE;
}
gimp_context_get_foreground (context, color);
return TRUE;
}
static void
gimp_paintbrush_real_get_paint_params (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,

View File

@ -46,14 +46,18 @@ struct _GimpPaintbrushClass
GimpBrushCoreClass parent_class;
/* virtual functions */
void (* get_paint_params) (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
gboolean (* get_color_history_color) (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpRGB *color);
void (* get_paint_params) (GimpPaintbrush *paintbrush,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
GimpSymmetry *sym,
GimpLayerMode *paint_mode,
GimpPaintApplicationMode *paint_appl_mode,
const GimpTempBuf **paint_pixmap,
GimpRGB *paint_color);
};