From d4d8dbbc1b24306692514308d67cd2d4bfda1a12 Mon Sep 17 00:00:00 2001 From: Ell Date: Mon, 16 Oct 2017 11:47:43 -0400 Subject: [PATCH] app: add custom gui for gegl:color-to-alpha(-plus) Add a specialized propgui constructor for gegl:color-to-alpha-plus. This op is currently in the workshop, but is set to be merged with the existing gegl:color-to-alpha, so we omit the '-plus' from file- and function-names. The new op adds a pair of properties to control the radii, relative to the selected color, below which colors become fully transparent, and above which colors remain fully opaque. Allow these properties to be set by picking a color from the image, and calculating the radius accordingly. --- app/propgui/Makefile.am | 2 + app/propgui/gimppropgui-color-to-alpha.c | 148 +++++++++++++++++++++++ app/propgui/gimppropgui-color-to-alpha.h | 35 ++++++ app/propgui/gimppropgui.c | 3 + po/POTFILES.in | 1 + 5 files changed, 189 insertions(+) create mode 100644 app/propgui/gimppropgui-color-to-alpha.c create mode 100644 app/propgui/gimppropgui-color-to-alpha.h diff --git a/app/propgui/Makefile.am b/app/propgui/Makefile.am index 2aee896251..e94e9eb348 100644 --- a/app/propgui/Makefile.am +++ b/app/propgui/Makefile.am @@ -25,6 +25,8 @@ libapppropgui_a_SOURCES = \ gimppropgui-color-balance.h \ gimppropgui-color-rotate.c \ gimppropgui-color-rotate.h \ + gimppropgui-color-to-alpha.c \ + gimppropgui-color-to-alpha.h \ gimppropgui-convolution-matrix.c \ gimppropgui-convolution-matrix.h \ gimppropgui-diffraction-patterns.c \ diff --git a/app/propgui/gimppropgui-color-to-alpha.c b/app/propgui/gimppropgui-color-to-alpha.c new file mode 100644 index 0000000000..af4729787b --- /dev/null +++ b/app/propgui/gimppropgui-color-to-alpha.c @@ -0,0 +1,148 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis + * + * gimppropgui-color-to-alpha.c + * Copyright (C) 2017 Ell + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "config.h" + +#include +#include + +#include "libgimpmath/gimpmath.h" +#include "libgimpwidgets/gimpwidgets.h" + +#include "propgui-types.h" + +#include "core/gimpcontext.h" + +#include "gimppropgui.h" +#include "gimppropgui-color-to-alpha.h" +#include "gimppropgui-generic.h" + +#include "gimp-intl.h" + + +static void +threshold_picked (GObject *config, + gpointer identifier, + gdouble x, + gdouble y, + const Babl *sample_format, + const GimpRGB *color) +{ + GimpRGB *bg_color; + gdouble radius = 0.0; + gdouble target_radius = 0.0; + gdouble threshold; + + g_object_get (config, + "color", &bg_color, + NULL); + + radius = MAX (radius, MAX (bg_color->r, 1.0 - bg_color->r)); + radius = MAX (radius, MAX (bg_color->g, 1.0 - bg_color->g)); + radius = MAX (radius, MAX (bg_color->b, 1.0 - bg_color->b)); + + target_radius = MAX (target_radius, fabs (color->r - bg_color->r)); + target_radius = MAX (target_radius, fabs (color->g - bg_color->g)); + target_radius = MAX (target_radius, fabs (color->b - bg_color->b)); + + threshold = target_radius / radius; + + g_object_set (config, + identifier, threshold, + NULL); + + g_free (bg_color); +} + +GtkWidget * +_gimp_prop_gui_new_color_to_alpha (GObject *config, + GParamSpec **param_specs, + guint n_param_specs, + GeglRectangle *area, + GimpContext *context, + GimpCreatePickerFunc create_picker_func, + GimpCreateControllerFunc create_controller_func, + gpointer creator) +{ + GtkWidget *vbox; + GtkWidget *button; + GtkWidget *hbox; + GtkWidget *scale; + const gchar *label; + + g_return_val_if_fail (G_IS_OBJECT (config), NULL); + g_return_val_if_fail (param_specs != NULL, NULL); + g_return_val_if_fail (n_param_specs > 0, NULL); + g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL); + + vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4); + + button = _gimp_prop_gui_new_generic (config, param_specs, 1, + area, context, create_picker_func, NULL, + creator); + gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); + gtk_widget_show (button); + + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4); + gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); + gtk_widget_show (hbox); + + scale = gimp_prop_widget_new (config, "transparency-threshold", + area, context, NULL, NULL, NULL, &label); + gtk_box_pack_start (GTK_BOX (hbox), scale, TRUE, TRUE, 0); + gtk_widget_show (scale); + + if (create_picker_func) + { + button = create_picker_func (creator, + "transparency-threshold", + GIMP_ICON_COLOR_PICKER_GRAY, + _("Pick farthest full-transparency color"), + /* pick_abyss = */ FALSE, + (GimpPickerCallback) threshold_picked, + config); + gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0); + gtk_widget_show (button); + } + + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4); + gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); + gtk_widget_show (hbox); + + scale = gimp_prop_widget_new (config, "opacity-threshold", + area, context, NULL, NULL, NULL, &label); + gtk_box_pack_start (GTK_BOX (hbox), scale, TRUE, TRUE, 0); + gtk_widget_show (scale); + + if (create_picker_func) + { + button = create_picker_func (creator, + "opacity-threshold", + GIMP_ICON_COLOR_PICKER_GRAY, + _("Pick nearest full-opacity color"), + /* pick_abyss = */ FALSE, + (GimpPickerCallback) threshold_picked, + config); + gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0); + gtk_widget_show (button); + } + + return vbox; +} diff --git a/app/propgui/gimppropgui-color-to-alpha.h b/app/propgui/gimppropgui-color-to-alpha.h new file mode 100644 index 0000000000..67e225ba6f --- /dev/null +++ b/app/propgui/gimppropgui-color-to-alpha.h @@ -0,0 +1,35 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis + * + * gimppropgui-color-to-alpha.h + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __GIMP_PROP_GUI_COLOR_TO_ALPHA_H__ +#define __GIMP_PROP_GUI_COLOR_TO_ALPHA_H__ + + +GtkWidget * +_gimp_prop_gui_new_color_to_alpha (GObject *config, + GParamSpec **param_specs, + guint n_param_specs, + GeglRectangle *area, + GimpContext *context, + GimpCreatePickerFunc create_picker_func, + GimpCreateControllerFunc create_controller_func, + gpointer creator); + + +#endif /* __GIMP_PROP_GUI_COLOR_TO_ALPHA_H__ */ diff --git a/app/propgui/gimppropgui.c b/app/propgui/gimppropgui.c index d9ad8745bb..586c17629a 100644 --- a/app/propgui/gimppropgui.c +++ b/app/propgui/gimppropgui.c @@ -48,6 +48,7 @@ #include "gimppropgui-channel-mixer.h" #include "gimppropgui-color-balance.h" #include "gimppropgui-color-rotate.h" +#include "gimppropgui-color-to-alpha.h" #include "gimppropgui-convolution-matrix.h" #include "gimppropgui-diffraction-patterns.h" #include "gimppropgui-eval.h" @@ -435,6 +436,8 @@ gui_new_funcs[] = _gimp_prop_gui_new_hue_saturation }, { "GimpGegl-gegl-color-rotate-config", _gimp_prop_gui_new_color_rotate }, + { "GimpGegl-gegl-color-to-alpha-plus-config", + _gimp_prop_gui_new_color_to_alpha }, { "GimpGegl-gegl-convolution-matrix-config", _gimp_prop_gui_new_convolution_matrix }, { "GimpGegl-gegl-channel-mixer-config", diff --git a/po/POTFILES.in b/po/POTFILES.in index 9745ec373e..961b14a58a 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -372,6 +372,7 @@ app/plug-in/plug-in-rc.c app/propgui/gimppropgui-channel-mixer.c app/propgui/gimppropgui-color-balance.c app/propgui/gimppropgui-color-rotate.c +app/propgui/gimppropgui-color-to-alpha.c app/propgui/gimppropgui-convolution-matrix.c app/propgui/gimppropgui-diffraction-patterns.c app/propgui/gimppropgui-generic.c