summaryrefslogtreecommitdiffstats
path: root/pdb/groups/gradients.pdb
blob: ed47a15b0e248907ea5102f687f374d8b94e08e2 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# GIMP - The GNU Image Manipulation Program
# Copyright (C) 1995 Spencer Kimball and Peter Mattis

# 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/>.

# "Perlized" from C source by Manish Singh <yosh@gimp.org>

sub gradients_refresh {
    $blurb = 'Refresh current gradients. This function always succeeds.';

    $help = <<'HELP';
This procedure retrieves all gradients currently in the user's gradient path
and updates the gradient dialogs accordingly.
HELP

    &mitch_pdb_misc('2002');

    %invoke = (
        code => <<'CODE'
{
  gimp_data_factory_data_refresh (gimp->gradient_factory, context);
}
CODE
    );
}

sub gradients_get_list {
    $blurb = 'Retrieve the list of loaded gradients.';

    $help = <<'HELP';
This procedure returns a list of the gradients that are currently loaded.
You can later use the gimp_context_set_gradient() function to
set the active gradient.
HELP

    &federico_pdb_misc('1997');

    @inargs = (
	{ name => 'filter', type => 'string', null_ok => 1,
          desc => 'An optional regular expression used to filter the list' }
    );

    @outargs = (
	{ name => 'gradient_list', type => 'stringarray',
	  desc => 'The list of gradient names',
	  array => { name => 'num_gradients',
		     desc => 'The number of loaded gradients' } }
    );

    %invoke = (
        headers => [ qw("core/gimpcontainer-filter.h") ],
	code => <<'CODE'
{
  gradient_list = gimp_container_get_filtered_name_array (gimp_data_factory_get_container (gimp->gradient_factory),
                                                          filter, &num_gradients);
}
CODE
    );
}

sub gradients_sample_uniform {
    &std_pdb_deprecated ('gimp-gradient-get-uniform-samples');

    @inargs = (
	{ name => 'num_samples', type => '2 <= int32',
	  desc => 'The number of samples to take' },
	{ name => 'reverse', type => 'boolean',
	  desc => 'Use the reverse gradient' }
    );

    @outargs = (
        { name => 'color_samples', type => 'floatarray',
	  desc => 'Color samples: { R1, G1, B1, A1, ..., Rn, Gn, Bn, An }',
	  array => { name => 'array_length', no_lib => 1,
		     desc => 'Length of the color_samples array (4 *
			      num_samples)' } }
    );

    %invoke = (
	code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg = NULL;
  gdouble              pos, delta;
  GimpRGB              color;
  gdouble             *pv;

  pos   = 0.0;
  delta = 1.0 / (num_samples - 1);

  array_length = num_samples * 4;

  pv = color_samples = g_new (gdouble, array_length);

  gradient = gimp_context_get_gradient (context);

  while (num_samples--)
    {
      seg = gimp_gradient_get_color_at (gradient, context, seg,
                                        pos, reverse,
                                        GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
                                        &color);

      *pv++ = color.r;
      *pv++ = color.g;
      *pv++ = color.b;
      *pv++ = color.a;

      pos += delta;
    }
}
CODE
    );
}

sub gradients_sample_custom {
    &std_pdb_deprecated ('gimp-gradient-get-custom-samples');

    @inargs = (
	{ name  => 'positions', type  => 'floatarray',
	  desc  => 'The list of positions to sample along the gradient',
	  array => { name => 'num_samples',
		     desc => 'The number of samples to take' } },
	{ name => 'reverse', type => 'boolean',
	  desc => 'Use the reverse gradient' }
    );

    @outargs = (
        { name => 'color_samples', type => 'floatarray',
	  desc => 'Color samples: { R1, G1, B1, A1, ..., Rn, Gn, Bn, An }',
	  array => { name => 'array_length', no_lib => 1,
		     desc => 'Length of the color_samples array (4 *
			      num_samples)' } }
    );

    %invoke = (
	code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg = NULL;
  GimpRGB              color;
  gdouble             *pv;

  array_length = num_samples * 4;

  pv = color_samples = g_new (gdouble, array_length);

  gradient = gimp_context_get_gradient (context);

  while (num_samples--)
    {
      seg = gimp_gradient_get_color_at (gradient, context, seg,
                                        *positions, reverse,
                                        GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
                                        &color);

      *pv++ = color.r;
      *pv++ = color.g;
      *pv++ = color.b;
      *pv++ = color.a;

      positions++;
    }
}
CODE
    );
}

sub gradients_get_gradient_data {
    &std_pdb_deprecated ('gimp-gradient-get-uniform-samples');

    @inargs = (
	{ name => 'name', type => 'string', null_ok => 1,
	  desc => 'The gradient name ("" means current active gradient)' },
	{ name => 'sample_size', type => '1 <= int32 <= 10000',
	  no_validate => 1,
	  desc => 'Size of the sample to return when the gradient is changed' },
	{ name => 'reverse', type => 'boolean',
	  desc => 'Use the reverse gradient' }
    );

    @outargs = (
	{ name => 'actual_name', type => 'string',
	  desc => 'The gradient name' },
	{ name => 'grad_data', type => 'floatarray',
	  desc => 'The gradient sample data',
	  array => { name => 'width',
		     desc => 'The gradient sample width (r,g,b,a)' } }
    );

    %invoke = (
	code => <<"CODE"
{
  GimpGradient *gradient;

  if (sample_size < 1 || sample_size > 10000)
    sample_size = GIMP_GRADIENT_DEFAULT_SAMPLE_SIZE;

  if (name && strlen (name))
    gradient = gimp_pdb_get_gradient (gimp, name, FALSE, error);
  else
    gradient = gimp_context_get_gradient (context);

  if (gradient)
    {
      GimpGradientSegment *seg = NULL;
      gdouble             *pv;
      gdouble              pos, delta;
      GimpRGB              color;

      pos   = 0.0;
      delta = 1.0 / (sample_size - 1);

      actual_name = g_strdup (gimp_object_get_name (gradient));
      grad_data   = g_new (gdouble, sample_size * 4);
      width       = sample_size * 4;

      pv = grad_data;

      while (sample_size--)
	{
	  seg = gimp_gradient_get_color_at (gradient, context, seg,
                                            pos, reverse,
                                            GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
                                            &color);

	  *pv++ = color.r;
	  *pv++ = color.g;
	  *pv++ = color.b;
	  *pv++ = color.a;

	  pos += delta;
	}
    }
  else
    success = FALSE;
}
CODE
    );
}


@headers = qw(<string.h>
              "core/gimp.h"
              "core/gimpcontext.h"
              "core/gimpdatafactory.h"
              "core/gimpgradient.h"
              "gimppdb-utils.h");

@procs = qw(gradients_refresh
            gradients_get_list
	    gradients_sample_uniform
            gradients_sample_custom
            gradients_get_gradient_data);

%exports = (app => [@procs], lib => [@procs]);

$desc = 'Gradients';
$doc_title = 'gimpgradients';
$doc_short_desc = 'Operations related to gradients.';
$doc_long_desc = 'Operations related to gradients.';

1;