2019-07-29 20:01:46 +08:00
|
|
|
#!/usr/bin/env python3
|
2019-07-29 00:29:42 +08:00
|
|
|
#
|
|
|
|
# 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gimp', '3.0')
|
|
|
|
from gi.repository import Gimp
|
2023-01-15 03:00:48 +08:00
|
|
|
gi.require_version('GimpUi', '3.0')
|
|
|
|
from gi.repository import GimpUi
|
2019-08-02 02:34:14 +08:00
|
|
|
from gi.repository import GObject
|
|
|
|
from gi.repository import GLib
|
|
|
|
from gi.repository import Gio
|
2023-01-15 03:00:48 +08:00
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
2019-07-29 00:29:42 +08:00
|
|
|
import sys
|
|
|
|
|
2022-05-26 06:59:36 +08:00
|
|
|
def N_(message): return message
|
|
|
|
def _(message): return GLib.dgettext(None, message)
|
|
|
|
|
2019-07-29 00:29:42 +08:00
|
|
|
|
|
|
|
def make_gradient(palette, num_segments, num_colors):
|
|
|
|
|
2022-09-06 07:28:35 +08:00
|
|
|
# name the gradient same as the source palette
|
|
|
|
# For now, the name of a resource is the same as the ID
|
2023-06-17 05:36:11 +08:00
|
|
|
palette_name = palette.get_name()
|
2022-09-06 07:28:35 +08:00
|
|
|
gradient = Gimp.Gradient.new(palette_name)
|
|
|
|
# assert gradient is valid but is has only one segment
|
|
|
|
assert gradient.get_number_of_segments() == 1
|
|
|
|
|
|
|
|
# split one segment into desired count
|
|
|
|
# index is zero-based
|
|
|
|
gradient.segment_range_split_uniform( 0, 0, num_segments)
|
2019-07-29 00:29:42 +08:00
|
|
|
|
|
|
|
for color_number in range(0,num_segments):
|
|
|
|
if color_number == num_colors - 1:
|
|
|
|
color_number_next = 0
|
|
|
|
else:
|
|
|
|
color_number_next = color_number + 1
|
2024-03-21 21:06:39 +08:00
|
|
|
color_left = palette.entry_get_color(color_number)
|
|
|
|
color_right = palette.entry_get_color(color_number_next)
|
|
|
|
gradient.segment_set_left_color(color_number, color_left)
|
|
|
|
gradient.segment_set_right_color(color_number, color_right)
|
2022-09-06 07:28:35 +08:00
|
|
|
|
|
|
|
# Side effects on the context. Probably not what most would expect.
|
2019-07-29 00:29:42 +08:00
|
|
|
Gimp.context_set_gradient(gradient)
|
2019-08-03 04:48:38 +08:00
|
|
|
return gradient
|
2019-07-29 00:29:42 +08:00
|
|
|
|
2023-06-17 05:36:11 +08:00
|
|
|
def run(procedure, config, data):
|
2019-08-06 06:26:22 +08:00
|
|
|
# Get the parameters
|
2023-06-17 05:36:11 +08:00
|
|
|
run_mode = config.get_property("run-mode")
|
2023-01-15 03:00:48 +08:00
|
|
|
|
|
|
|
if run_mode == Gimp.RunMode.INTERACTIVE:
|
|
|
|
GimpUi.init(procedure.get_name())
|
|
|
|
dialog = GimpUi.ProcedureDialog(procedure=procedure, config=config)
|
|
|
|
|
|
|
|
# Add palette button
|
|
|
|
dialog.fill (["palette"])
|
|
|
|
|
|
|
|
if not dialog.run():
|
|
|
|
dialog.destroy()
|
|
|
|
return procedure.new_return_values(Gimp.PDBStatusType.CANCEL, GLib.Error())
|
|
|
|
|
2023-06-17 05:36:11 +08:00
|
|
|
dialog.destroy()
|
2023-01-15 03:00:48 +08:00
|
|
|
|
2023-06-17 05:36:11 +08:00
|
|
|
palette = config.get_property("palette")
|
2023-01-15 03:00:48 +08:00
|
|
|
if palette is None or not palette.is_valid():
|
|
|
|
if palette is not None:
|
|
|
|
sys.stderr.write(f'Invalid palette id: {palette.get_id()}\n')
|
|
|
|
sys.stderr.write('This should not happen. Please report to GIMP project.\n')
|
|
|
|
sys.stderr.write('Falling back to context palette instead.\n')
|
2019-08-06 06:26:22 +08:00
|
|
|
palette = Gimp.context_get_palette()
|
2023-06-17 05:36:11 +08:00
|
|
|
config.set_property("palette", palette)
|
2022-09-06 07:28:35 +08:00
|
|
|
|
|
|
|
num_colors = palette.get_color_count()
|
2019-08-02 02:34:14 +08:00
|
|
|
|
|
|
|
if procedure.get_name() == 'python-fu-palette-to-gradient':
|
|
|
|
num_segments = num_colors - 1
|
|
|
|
else: # 'python-fu-palette-to-gradient-repeating'
|
|
|
|
num_segments = num_colors
|
|
|
|
gradient = make_gradient(palette, num_segments, num_colors)
|
|
|
|
|
|
|
|
# XXX: for the error parameter, we want to return None.
|
|
|
|
# Unfortunately even though the argument is (nullable), pygobject
|
|
|
|
# looks like it may have a bug. So workaround is to just set a
|
|
|
|
# generic GLib.Error() since anyway the error won't be process with
|
|
|
|
# GIMP_PDB_SUCCESS status.
|
|
|
|
# See pygobject#351
|
|
|
|
retval = procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
|
|
|
|
|
2019-08-03 04:48:38 +08:00
|
|
|
# XXX: I don't try to get the GValue with retval.index(1) because it
|
|
|
|
# actually return a string (cf. pygobject#353). Just create a new
|
|
|
|
# GValue and replace the default one with this one.
|
2022-09-06 07:28:35 +08:00
|
|
|
# This comment dates from when resources are strings:
|
|
|
|
# value = GObject.Value(GObject.TYPE_STRING, gradient)
|
|
|
|
|
|
|
|
value = GObject.Value(Gimp.Gradient, gradient)
|
2019-08-03 04:48:38 +08:00
|
|
|
retval.remove(1)
|
|
|
|
retval.insert(1, value)
|
|
|
|
|
2019-08-02 02:34:14 +08:00
|
|
|
return retval
|
|
|
|
|
|
|
|
class PaletteToGradient (Gimp.PlugIn):
|
2019-08-06 06:26:22 +08:00
|
|
|
## Parameter: run mode ##
|
2019-08-03 04:48:38 +08:00
|
|
|
@GObject.Property(type=Gimp.RunMode,
|
|
|
|
default=Gimp.RunMode.NONINTERACTIVE,
|
|
|
|
nick="Run mode", blurb="The run mode")
|
|
|
|
def run_mode(self):
|
2019-08-06 06:26:22 +08:00
|
|
|
'''The run mode (unused)'''
|
2019-08-03 04:48:38 +08:00
|
|
|
return self.runmode
|
|
|
|
|
|
|
|
@run_mode.setter
|
|
|
|
def run_mode(self, runmode):
|
|
|
|
self.runmode = runmode
|
|
|
|
|
2019-08-06 06:26:22 +08:00
|
|
|
## Parameter: palette ##
|
2022-09-06 07:28:35 +08:00
|
|
|
@GObject.Property(type=Gimp.Palette,
|
2019-08-06 06:26:22 +08:00
|
|
|
default=None,
|
2023-06-17 05:36:11 +08:00
|
|
|
nick= _("_Palette"))
|
2019-08-06 06:26:22 +08:00
|
|
|
def palette(self):
|
2022-09-06 07:28:35 +08:00
|
|
|
'''Palette or None for the currently selected palette'''
|
2019-08-06 06:26:22 +08:00
|
|
|
return self.palette
|
|
|
|
|
|
|
|
@palette.setter
|
|
|
|
def palette(self, palette):
|
|
|
|
self.palette = palette
|
|
|
|
|
2019-08-03 04:48:38 +08:00
|
|
|
## Properties: return values ##
|
2022-09-06 07:28:35 +08:00
|
|
|
@GObject.Property(type=Gimp.Gradient,
|
2019-08-03 04:48:38 +08:00
|
|
|
default="",
|
2022-09-06 07:28:35 +08:00
|
|
|
nick=_("The newly created gradient"),
|
|
|
|
blurb=_("The newly created gradient"))
|
2019-08-03 04:48:38 +08:00
|
|
|
def new_gradient(self):
|
|
|
|
"""Read-write integer property."""
|
|
|
|
return self.new_gradient
|
|
|
|
|
|
|
|
@new_gradient.setter
|
|
|
|
def new_gradient(self, new_gradient):
|
|
|
|
self.new_gradient = new_gradient
|
|
|
|
|
|
|
|
## GimpPlugIn virtual methods ##
|
2022-05-26 06:59:36 +08:00
|
|
|
def do_set_i18n(self, procname):
|
|
|
|
return True, 'gimp30-python', None
|
|
|
|
|
2019-08-02 02:34:14 +08:00
|
|
|
def do_query_procedures(self):
|
|
|
|
return ['python-fu-palette-to-gradient',
|
libgimp, plug-ins: remove n_procedures from (init|query)_procedures().
The way currently implemented plug-ins are, they are already
NULL-terminating the returned arrays. Since a procedure name cannot be
NULL itself by definition, defining the array length by a terminal NULL
is enough. There is no need to also add a n_procedures parameters which
is just one more possible bug source, even more as we were already
expecting the NULL termination by using g_strfreev() to free the memory.
Anyway a length parameter does not bring any advantage since a plug-in
can still "lie" about its array size (just as it can forget to
NULL-terminate it) and when this happens, the plug-in will segfault.
That's it, it's just a plug-in programming error.
Last but not least, some binding seem to have issues with returned array
setting an (out) parameter as the length. In pygobject at least, the
length parameter doesn't disappear and we end up with this ugly
signature:
> In [3]: Gimp.PlugIn.do_query_procedures.__doc__
> Out[3]: 'query_procedures(self) -> list, n_procedures:int'
See bug report pygobject#352.
To avoid this, we should either set both the array and the length as
(out) parameters or just set the returned array as NULL-terminated
(which is the solution I chose).
2019-08-02 19:36:11 +08:00
|
|
|
'python-fu-palette-to-gradient-repeating']
|
2019-08-02 02:34:14 +08:00
|
|
|
|
|
|
|
def do_create_procedure(self, name):
|
|
|
|
procedure = Gimp.Procedure.new(self, name,
|
|
|
|
Gimp.PDBProcType.PLUGIN,
|
2019-08-03 04:48:38 +08:00
|
|
|
run, None)
|
2019-08-02 02:34:14 +08:00
|
|
|
if name == 'python-fu-palette-to-gradient':
|
2022-05-26 06:26:25 +08:00
|
|
|
procedure.set_menu_label(_("Palette to _Gradient"))
|
|
|
|
procedure.set_documentation(_("Create a gradient using colors from the palette"),
|
|
|
|
_("Create a new gradient using colors from the palette."),
|
2019-08-02 02:34:14 +08:00
|
|
|
"")
|
|
|
|
elif name == 'python-fu-palette-to-gradient-repeating':
|
2022-05-26 06:26:25 +08:00
|
|
|
procedure.set_menu_label(_("Palette to _Repeating Gradient"))
|
|
|
|
procedure.set_documentation(_("Create a repeating gradient using colors from the palette"),
|
|
|
|
_("Create a new repeating gradient using colors from the palette."),
|
2019-08-02 02:34:14 +08:00
|
|
|
"")
|
|
|
|
else:
|
|
|
|
procedure = None
|
|
|
|
|
|
|
|
if procedure is not None:
|
|
|
|
procedure.set_attribution("Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
|
|
|
|
"Carol Spears", "2006")
|
2019-08-03 04:48:38 +08:00
|
|
|
# We don't build a GParamSpec ourselves because passing it
|
|
|
|
# around is apparently broken in Python. Hence this trick.
|
|
|
|
# See pygobject#227
|
|
|
|
procedure.add_argument_from_property(self, "run-mode")
|
2019-08-06 06:26:22 +08:00
|
|
|
procedure.add_argument_from_property(self, "palette")
|
2019-08-03 04:48:38 +08:00
|
|
|
procedure.add_return_value_from_property(self, "new-gradient")
|
|
|
|
|
2024-02-25 07:13:01 +08:00
|
|
|
procedure.add_menu_path ('<Palettes>/Palettes Menu')
|
2019-08-02 02:34:14 +08:00
|
|
|
|
|
|
|
return procedure
|
|
|
|
|
|
|
|
Gimp.main(PaletteToGradient.__gtype__, sys.argv)
|