summaryrefslogtreecommitdiffstats
path: root/plug-ins/pygimp/plug-ins/palette-to-gradient.py
blob: 9b2da49d1ca36f21720e1654cbaad81515f22705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python2
#
# 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 make_gradient(palette, num_segments, num_colors):
    gradient = pdb.gimp_gradient_new(palette)

    if (num_segments > 1):
        pdb.gimp_gradient_segment_range_split_uniform(gradient, 0, -1,
                                                      num_segments)

    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
        color_left = pdb.gimp_palette_entry_get_color(palette,
                                                      color_number)
        color_right = pdb.gimp_palette_entry_get_color(palette,
                                                       color_number_next)
        pdb.gimp_gradient_segment_set_left_color(gradient,
                                                 color_number, color_left,
                                                 100.0)
        pdb.gimp_gradient_segment_set_right_color(gradient,
                                                  color_number, color_right,
                                                  100.0)
    pdb.gimp_context_set_gradient(gradient)
    return gradient


def palette_to_gradient_repeating(palette):
    num_colors = pdb.gimp_palette_get_info(palette)
    num_segments = num_colors
    return make_gradient(palette, num_segments, num_colors)


register(
    "python-fu-palette-to-gradient-repeating",
    N_("Create a repeating gradient using colors from the palette"),
    "Create a new repeating gradient using colors from the palette.",
    "Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
    "Carol Spears",
    "2006",
    N_("Palette to _Repeating Gradient"),
    "",
    [(PF_PALETTE,  "palette", _("Palette"), "")],
    [(PF_GRADIENT, "new-gradient", "Result")],
    palette_to_gradient_repeating,
    menu="<Palettes>",
    domain=("gimp20-python", gimp.locale_directory)
    )


def palette_to_gradient(palette):
    num_colors = pdb.gimp_palette_get_info(palette)
    num_segments = num_colors - 1
    return make_gradient(palette, num_segments, num_colors)

register(
    "python-fu-palette-to-gradient",
    N_("Create a gradient using colors from the palette"),
    "Create a new gradient using colors from the palette.",
    "Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
    "Carol Spears",
    "2006",
    N_("Palette to _Gradient"),
    "",
    [(PF_PALETTE,  "palette", _("Palette"), "")],
    [(PF_GRADIENT, "new-gradient", "Result")],
    palette_to_gradient,
    menu="<Palettes>",
    domain=("gimp20-python", gimp.locale_directory)
    )

main ()