1#!/usr/local/bin/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 3 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, see <https://www.gnu.org/licenses/>.
15
16from gimpfu import *
17
18gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
19
20def make_gradient(palette, num_segments, num_colors):
21    gradient = pdb.gimp_gradient_new(palette)
22
23    if (num_segments > 1):
24        pdb.gimp_gradient_segment_range_split_uniform(gradient, 0, -1,
25                                                      num_segments)
26
27    for color_number in range(0,num_segments):
28        if (color_number == num_colors-1):color_number_next = 0
29        else: color_number_next = color_number + 1
30        color_left = pdb.gimp_palette_entry_get_color(palette,
31                                                      color_number)
32        color_right = pdb.gimp_palette_entry_get_color(palette,
33                                                       color_number_next)
34        pdb.gimp_gradient_segment_set_left_color(gradient,
35                                                 color_number, color_left,
36                                                 100.0)
37        pdb.gimp_gradient_segment_set_right_color(gradient,
38                                                  color_number, color_right,
39                                                  100.0)
40    pdb.gimp_context_set_gradient(gradient)
41    return gradient
42
43
44def palette_to_gradient_repeating(palette):
45    num_colors = pdb.gimp_palette_get_info(palette)
46    num_segments = num_colors
47    return make_gradient(palette, num_segments, num_colors)
48
49
50register(
51    "python-fu-palette-to-gradient-repeating",
52    N_("Create a repeating gradient using colors from the palette"),
53    "Create a new repeating gradient using colors from the palette.",
54    "Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
55    "Carol Spears",
56    "2006",
57    N_("Palette to _Repeating Gradient"),
58    "",
59    [(PF_PALETTE,  "palette", _("Palette"), "")],
60    [(PF_GRADIENT, "new-gradient", "Result")],
61    palette_to_gradient_repeating,
62    menu="<Palettes>",
63    domain=("gimp20-python", gimp.locale_directory)
64    )
65
66
67def palette_to_gradient(palette):
68    num_colors = pdb.gimp_palette_get_info(palette)
69    num_segments = num_colors - 1
70    return make_gradient(palette, num_segments, num_colors)
71
72register(
73    "python-fu-palette-to-gradient",
74    N_("Create a gradient using colors from the palette"),
75    "Create a new gradient using colors from the palette.",
76    "Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
77    "Carol Spears",
78    "2006",
79    N_("Palette to _Gradient"),
80    "",
81    [(PF_PALETTE,  "palette", _("Palette"), "")],
82    [(PF_GRADIENT, "new-gradient", "Result")],
83    palette_to_gradient,
84    menu="<Palettes>",
85    domain=("gimp20-python", gimp.locale_directory)
86    )
87
88main ()
89