home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 October / maximum-cd-2009-10.iso / DiscContents / gimp-2.6.6-i686-setup.exe / {app} / lib / gimp / 2.0 / plug-ins / palette-sort.py < prev    next >
Encoding:
Python Source  |  2009-03-21  |  2.7 KB  |  75 lines

  1. #!/usr/bin/env python
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. from gimpfu import *
  18.  
  19. gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
  20.  
  21. def palette_sort (palette, model, channel, ascending):
  22.     #If palette is read only, work on a copy:
  23.     editable = pdb.gimp_palette_is_editable(palette)
  24.     if not editable:palette = pdb.gimp_palette_duplicate (palette)
  25.  
  26.     num_colors = pdb.gimp_palette_get_info (palette)
  27.     entry_list = []
  28.     for i in xrange (num_colors):
  29.         entry =  (pdb.gimp_palette_entry_get_name (palette, i),
  30.                   pdb.gimp_palette_entry_get_color (palette, i))
  31.         index = entry[1][channel]
  32.         if model == "HSV":
  33.             index = entry[1].to_hsv()[channel]
  34.         else:
  35.             index = entry[1][channel]
  36.         entry_list.append ((index, entry))
  37.     entry_list.sort(lambda x,y: cmp(x[0], y[0]))
  38.     if not ascending:
  39.         entry_list.reverse()
  40.     for i in xrange(num_colors):
  41.         pdb.gimp_palette_entry_set_name (palette, i, entry_list[i][1][0])
  42.         pdb.gimp_palette_entry_set_color (palette, i, entry_list[i][1][1])
  43.  
  44.     return palette
  45.  
  46.  
  47. register(
  48.     "python-fu-palette-sort",
  49.     N_("Sort the colors in a palette"),
  50.     "palette_merge (palette, model, channel, ascending) -> new_palette",
  51.     "Joao S. O. Bueno Calligaris, Carol Spears",
  52.     "Joao S. O. Bueno Calligaris",
  53.     "2006",
  54.     N_("_Sort Palette..."),
  55.     "",
  56.     [
  57.         (PF_PALETTE, "palette",  _("Palette"), ""),
  58.         (PF_RADIO,   "model",    _("Color _model"), "HSV",
  59.                                     ((_("RGB"), "RGB"),
  60.                                      (_("HSV"), "HSV"))),
  61.         (PF_RADIO,   "channel",  _("Channel to _sort"), 2,
  62.                                     ((_("Red or Hue"),          0),
  63.                                      (_("Green or Saturation"), 1),
  64.                                      (_("Blue or Value"),       2))),
  65.         (PF_BOOL,   "ascending", _("_Ascending"), True)
  66.     ],
  67.     [],
  68.     palette_sort,
  69.     menu="<Palettes>",
  70.     domain=("gimp20-python", gimp.locale_directory)
  71.     )
  72.  
  73.  
  74. main ()
  75.