plug-ins: port palette-offset to new API.

This first version will just offset by 1 in interactive mode, by
default. The GIMP 2 version used to have a GUI, but it was not created
by the plug-in itself. I am guessing that maybe our Python wrapper used
to create GUI by default.
If so, this will have to change. Python plug-ins will be responsible of
their own GUI, just like C plug-ins.
This commit is contained in:
Jehan 2019-08-06 00:31:50 +02:00
parent 35d4b68edc
commit dca353f8cd
3 changed files with 150 additions and 63 deletions

View File

@ -1,61 +0,0 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# 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/>.
from gimpfu import *
gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
def palette_offset(palette, amount):
#If palette is read only, work on a copy:
editable = pdb.gimp_palette_is_editable(palette)
if not editable:palette = pdb.gimp_palette_duplicate (palette)
num_colors = pdb.gimp_palette_get_info (palette)
tmp_entry_array = []
for i in xrange (num_colors):
tmp_entry_array.append ((pdb.gimp_palette_entry_get_name (palette, i),
pdb.gimp_palette_entry_get_color (palette, i)))
for i in xrange (num_colors):
target_index = i + amount
if target_index >= num_colors:
target_index -= num_colors
elif target_index < 0:
target_index += num_colors
pdb.gimp_palette_entry_set_name (palette, target_index, tmp_entry_array[i][0])
pdb.gimp_palette_entry_set_color (palette, target_index, tmp_entry_array[i][1])
return palette
register(
"python-fu-palette-offset",
N_("Offset the colors in a palette"),
"palette_offset (palette, amount) -> modified_palette",
"Joao S. O. Bueno Calligaris, Carol Spears",
"(c) Joao S. O. Bueno Calligaris",
"2004, 2006",
N_("_Offset Palette..."),
"",
[
(PF_PALETTE, "palette", _("Palette"), ""),
(PF_INT, "amount", _("Off_set"), 1),
],
[(PF_PALETTE, "new-palette", "Result")],
palette_offset,
menu="<Palettes>",
domain=("gimp20-python", gimp.locale_directory)
)
main ()

View File

@ -11,7 +11,7 @@ source_scripts = \
## foggify.py \
## gradients-save-as-css.py \
## histogram-export.py \
## palette-offset.py \
palette-offset.py \
## palette-sort.py \
palette-to-gradient.py
## py-slice.py \
@ -30,7 +30,7 @@ scripts = \
## foggify/foggify.py \
## gradients-save-as-css/gradients-save-as-css.py \
## histogram-export/histogram-export.py \
## palette-offset/palette-offset.py \
palette-offset/palette-offset.py \
## palette-sort/palette-sort.py \
palette-to-gradient/palette-to-gradient.py
## py-slice/py-slice.py \

View File

@ -0,0 +1,148 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# 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
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gio
import sys
import gettext
_ = gettext.gettext
def N_(message): return message
def run(procedure, args, data):
# Get the parameters
palette = None
if args.length() > 1:
palette = args.index(1)
if palette == '' or palette is None:
palette = Gimp.context_get_palette()
(exists, num_colors) = Gimp.palette_get_info(palette)
if not exists:
error = 'Unknown palette: {}'.format(palette)
return procedure.new_return_values(Gimp.PDBStatusType.CALLING_ERROR,
GLib.Error(error))
if args.length() > 2:
amount = args.index(2)
else:
amount = 1
#If palette is read only, work on a copy:
editable = Gimp.palette_is_editable(palette)
if not editable:
palette = Gimp.palette_duplicate (palette)
tmp_entry_array = []
for i in xrange (num_colors):
tmp_entry_array.append ((Gimp.palette_entry_get_name (palette, i)[1],
Gimp.palette_entry_get_color (palette, i)[1]))
for i in xrange (num_colors):
target_index = i + amount
if target_index >= num_colors:
target_index -= num_colors
elif target_index < 0:
target_index += num_colors
Gimp.palette_entry_set_name (palette, target_index, tmp_entry_array[i][0])
Gimp.palette_entry_set_color (palette, target_index, tmp_entry_array[i][1])
retval = procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
value = GObject.Value(GObject.TYPE_STRING, palette)
retval.remove(1)
retval.insert(1, value)
return retval
class PaletteOffset (Gimp.PlugIn):
## Parameter: run-mode ##
@GObject.Property(type=Gimp.RunMode,
default=Gimp.RunMode.NONINTERACTIVE,
nick="Run mode", blurb="The run mode")
def run_mode(self):
"""Read-write integer property."""
return self.runmode
@run_mode.setter
def run_mode(self, runmode):
self.runmode = runmode
## Parameter: palette ##
@GObject.Property(type=str,
default="",
nick= _("Palette"),
blurb= _("Palette"))
def palette(self):
return self.palette
@palette.setter
def palette(self, palette):
self.palette = palette
## Parameter: amount ##
@GObject.Property(type=int,
default=1,
nick= _("Off_set"),
blurb= _("Offset"))
def amount(self):
return self.amount
@amount.setter
def amount(self, amount):
self.amount = amount
## Return: new-palette ##
@GObject.Property(type=str,
default=None,
nick="Name of the edited palette",
blurb="Name of the newly created palette if read-only or the input palette otherwise")
def new_palette(self):
return self.new_palette
@new_palette.setter
def new_palette(self, new_palette):
self.new_palette = new_palette
## GimpPlugIn virtual methods ##
def do_query_procedures(self):
# Localization
self.set_translation_domain ("gimp30-python",
Gio.file_new_for_path(Gimp.locale_directory()))
return [ "python-fu-palette-offset" ]
def do_create_procedure(self, name):
procedure = Gimp.Procedure.new(self, name,
Gimp.PDBProcType.PLUGIN,
run, None)
if name == 'python-fu-palette-offset':
procedure.set_menu_label(N_("_Offset Palette..."))
procedure.set_documentation(N_("Offset the colors in a palette"),
"palette_offset (palette, amount) -> modified_palette",
"")
procedure.set_attribution("Joao S. O. Bueno Calligaris, Carol Spears",
"(c) Joao S. O. Bueno Calligaris",
"2004, 2006")
procedure.add_argument_from_property(self, "run-mode")
procedure.add_argument_from_property(self, "palette")
procedure.add_argument_from_property(self, "amount")
procedure.add_return_value_from_property(self, "new-palette")
procedure.add_menu_path ('<Palettes>')
else:
procedure = None
return procedure
Gimp.main(PaletteOffset.__gtype__, sys.argv)