gradient.pdb 41.1 KB
Newer Older
1
# GIMP - The GNU Image Manipulation Program
2 3
# Copyright (C) 1995 Spencer Kimball and Peter Mattis

4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
# (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
15
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
16

17
sub gradient_new {
18 19
    $blurb = 'Creates a new gradient';
    $help  = 'This procedure creates a new, uninitialized gradient';
20

21
    &shlomi_pdb_misc('2003', '2.2');
22 23

    @inargs = (
24
	{ name => 'name', type => 'string', non_empty => 1,
25 26 27 28
	  desc => 'The requested name of the new gradient' }
    );

    @outargs = (
29
	{ name => 'actual_name', type => 'string',
30
	  desc => 'The actual new gradient name' }
31 32 33 34 35
    );

    %invoke = (
        code => <<'CODE'
{
36
  GimpData *data = gimp_data_factory_data_new (gimp->gradient_factory,
37
                                               context, name);
38

39
  if (data)
40
    actual_name = g_strdup (gimp_object_get_name (data));
41 42
  else
    success = FALSE;
43 44 45 46 47 48
}
CODE
    );
}

sub gradient_duplicate {
49 50
    $blurb = 'Duplicates a gradient';
    $help  = 'This procedure creates an identical gradient by a different name';
51

52
    &shlomi_pdb_misc('2003', '2.2');
53 54

    @inargs = (
55
	{ name => 'name', type => 'string', non_empty => 1,
56
	  desc => 'The gradient name' }
57 58 59
    );

    @outargs = (
60
	{ name => 'copy_name', type => 'string',
61
	  desc => "The name of the gradient's copy" }
62 63 64
    );

    %invoke = (
65
        code => <<'CODE'
66
{
67
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name, GIMP_PDB_DATA_ACCESS_READ, error);
68 69 70

  if (gradient)
    {
71
      GimpGradient *gradient_copy = (GimpGradient *)
72 73 74
        gimp_data_factory_data_duplicate (gimp->gradient_factory,
                                          GIMP_DATA (gradient));

75
      if (gradient_copy)
76
        copy_name = g_strdup (gimp_object_get_name (gradient_copy));
77 78
      else
        success = FALSE;
79 80 81 82 83 84 85 86
    }
  else
    success = FALSE;
}
CODE
    );
}

87
sub gradient_is_editable {
88 89
    $blurb = 'Tests if gradient can be edited';
    $help  = 'Returns TRUE if you have permission to change the gradient';
90

91
    &bill_pdb_misc('2004', '2.4');
92 93

    @inargs = (
94
	{ name => 'name', type => 'string', non_empty => 1,
95
	  desc => 'The gradient name' }
96 97 98
    );

    @outargs = (
99
	{ name => 'editable', type => 'boolean',
100
	  desc => 'TRUE if the gradient can be edited' }
101 102 103 104 105
    );

    %invoke = (
        code => <<'CODE'
{
106
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name, GIMP_PDB_DATA_ACCESS_READ, error);
107 108

  if (gradient)
109
    editable = gimp_data_is_writable (GIMP_DATA (gradient));
110 111 112 113 114 115 116
  else
    success = FALSE;
}
CODE
    );
}

117
sub gradient_rename {
118 119
    $blurb = 'Rename a gradient';
    $help  = 'This procedure renames a gradient';
120

121
    &shlomi_pdb_misc('2003', '2.2');
122 123

    @inargs = (
124
	{ name => 'name', type => 'string', non_empty => 1,
125
	  desc => 'The gradient name' },
126
        { name => 'new_name', type => 'string', non_empty => 1,
127
          desc => 'The new name of the gradient' }
128 129 130
    );

    @outargs = (
131
	{ name => 'actual_name', type => 'string',
132
	  desc => 'The actual new name of the gradient' }
133 134 135
    );

    %invoke = (
136
	code => <<'CODE'
137
{
138
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name, GIMP_PDB_DATA_ACCESS_RENAME, error);
139

140
  if (gradient)
141 142
    {
      gimp_object_set_name (GIMP_OBJECT (gradient), new_name);
143
      actual_name = g_strdup (gimp_object_get_name (gradient));
144
    }
145 146 147 148 149 150 151 152
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_delete {
153 154
    $blurb = 'Deletes a gradient';
    $help  = 'This procedure deletes a gradient';
155

156
    &shlomi_pdb_misc('2003', '2.2');
157 158

    @inargs = (
159
	{ name => 'name', type => 'string', non_empty => 1,
160
	  desc => 'The gradient name' }
161 162 163
    );

    %invoke = (
164
	code => <<'CODE'
165
{
166
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name, GIMP_PDB_DATA_ACCESS_READ, error);
167

168
  if (gradient && gimp_data_is_deletable (GIMP_DATA (gradient)))
169 170 171
    success = gimp_data_factory_data_delete (gimp->gradient_factory,
                                             GIMP_DATA (gradient),
                                             TRUE, error);
172 173 174 175 176 177 178
  else
    success = FALSE;
}
CODE
    );
}

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
sub gradient_get_number_of_segments {
    $blurb = 'Returns the number of segments of the specified gradient';

    $help = <<'HELP';
This procedure returns the number of segments of the specified gradient.
HELP

    $author    = 'Lars-Peter Clausen <lars@metafoo.de>';
    $copyright = 'Lars-Peter Clausen';
    $date      = '2008';
    $since     = '2.6';


    @inargs = (
        { name => 'name', type => 'string', non_empty => 1,
          desc => 'The gradient name' }
    );

    @outargs = (
        { name => 'num_segments', type => 'int32',
          init => 0, desc => 'Number of segments' }
    );
    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

207
  gradient = gimp_pdb_get_gradient (gimp, name, GIMP_PDB_DATA_ACCESS_READ, error);
208 209 210 211 212 213 214 215 216 217 218 219 220

  if (gradient)
    {
      for (seg = gradient->segments; seg; seg = seg->next)
        num_segments++;
    }
  else
    success = FALSE;
}
CODE
    );
}

221 222 223 224 225 226 227 228 229 230 231 232
sub gradient_get_uniform_samples {
    $blurb = 'Sample the specified in uniform parts.';

    $help = <<'HELP';
This procedure samples the active gradient in the
specified number of uniform parts. It returns a list of floating-point values
which correspond to the RGBA values for each sample. The minimum number of
samples to take is 2, in which case the returned colors will correspond to the
{ 0.0, 1.0 } positions in the gradient. For example, if the number of samples
is 3, the procedure will return the colors at positions { 0.0, 0.5, 1.0 }.
HELP

233
    &federico_pdb_misc('1997', '2.2');
234 235

    @inargs = (
236
	{ name => 'name', type => 'string', non_empty => 1,
237
	  desc => 'The gradient name' },
238 239
	{ name => 'num_samples', type => '2 <= int32',
	  desc => 'The number of samples to take' },
240
	{ name => 'reverse', type => 'boolean',
241
	  desc => 'Use the reverse gradient' }
242 243
    );

244 245 246 247 248 249
    @outargs = (
        { name => 'color_samples', type => 'floatarray', void_ret => 1,
	  desc => 'Color samples: { R1, G1, B1, A1, ..., Rn, Gn, Bn, An }',
	  array => { desc => 'Length of the color_samples array (4 *
			      num_samples)' } }
    );
250 251 252 253

    %invoke = (
	code => <<'CODE'
{
254 255 256
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name,
                                                  GIMP_PDB_DATA_ACCESS_READ,
                                                  error);
257 258 259

  if (gradient)
    {
260 261 262 263
      GimpGradientSegment *seg   = NULL;
      gdouble              pos   = 0.0;
      gdouble              delta = 1.0 / (num_samples - 1);
      gdouble             *sample;
264 265 266 267 268 269 270 271 272

      num_color_samples = num_samples * 4;

      sample = color_samples = g_new (gdouble, num_color_samples);

      while (num_samples--)
        {
          GimpRGB color;

273
	  seg = gimp_gradient_get_color_at (gradient, context, seg,
274 275 276
                                            pos, reverse,
                                            GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
                                            &color);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

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

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

sub gradient_get_custom_samples {
294
    $blurb = 'Sample the specified gradient in custom positions.';
295 296 297 298 299 300 301 302 303 304

    $help = <<'HELP';
This procedure samples the active gradient in the specified number of
points. The procedure will sample the gradient in the specified
positions from the list. The left endpoint of the gradient corresponds
to position 0.0, and the right endpoint corresponds to 1.0. The
procedure returns a list of floating-point values which correspond to
the RGBA values for each sample.
HELP

305
    &federico_pdb_misc('1997', '2.2');
306 307

    @inargs = (
308
	{ name => 'name', type => 'string', non_empty => 1,
309
	  desc => 'The gradient name' },
310
	{ name  => 'positions', type  => 'floatarray',
311
	  desc  => 'The list of positions to sample along the gradient',
312 313
	  array => { name => 'num_samples', type => '1 <= int32',
		     desc => 'The number of samples to take' } },
314
	{ name => 'reverse', type => 'boolean',
315
	  desc => 'Use the reverse gradient' }
316 317
    );

318 319 320 321 322 323
    @outargs = (
        { name => 'color_samples', type => 'floatarray', void_ret => 1,
	  desc => 'Color samples: { R1, G1, B1, A1, ..., Rn, Gn, Bn, An }',
	  array => { desc => 'Length of the color_samples array (4 *
			      num_samples)' } }
    );
324 325 326 327

    %invoke = (
	code => <<'CODE'
{
328 329 330
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name,
                                                  GIMP_PDB_DATA_ACCESS_READ,
                                                  error);
331 332 333

  if (gradient)
    {
334 335
      GimpGradientSegment *seg = NULL;
      gdouble             *sample;
336 337 338 339 340 341 342 343 344

      num_color_samples = num_samples * 4;

      sample = color_samples = g_new (gdouble, num_color_samples);

      while (num_samples--)
        {
          GimpRGB color;

345 346
          seg = gimp_gradient_get_color_at (gradient, context,
                                            seg, *positions,
347 348 349
                                            reverse,
                                            GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
                                            &color);
350 351 352 353 354 355

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

356
          positions++;
357 358 359 360 361 362 363 364 365
        }
    }
  else
    success = FALSE;
}
CODE
    );
}

366 367
sub gradient_segment_get_left_color {
    $blurb = 'Retrieves the left endpoint color of the specified segment';
368 369

    $help = <<'HELP';
370 371
This procedure retrieves the left endpoint color of the specified segment of
the specified gradient.
372 373
HELP

374
    &shlomi_pdb_misc('2003', '2.2');
375

376
    @inargs = (
377
	{ name => 'name', type => 'string', non_empty => 1,
378 379 380 381
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' }
    );
382 383

    @outargs = (
384 385
        { name => 'color', type => 'color', void_ret => 1,
          desc => 'The return color' },
386 387
        { name => 'opacity', type => 'float',
          desc => 'The opacity of the endpoint' }
388 389 390
    );

    %invoke = (
391 392 393 394 395
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

396 397
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_READ, segment,
                           &seg, error);
398 399 400 401 402 403 404 405 406

  if (seg)
    {
      gimp_gradient_segment_get_left_color (gradient, seg, &color);
      opacity = color.a * 100.0;
    }
  else
    success = FALSE;
}
407 408 409 410
CODE
    );
}

411 412
sub gradient_segment_get_right_color {
    $blurb = 'Retrieves the right endpoint color of the specified segment';
413 414

    $help = <<'HELP';
415 416
This procedure retrieves the right endpoint color of the specified segment of
the specified gradient.
417 418
HELP

419
    &shlomi_pdb_misc('2003', '2.2');
420

421
    @inargs = (
422
	{ name => 'name', type => 'string', non_empty => 1,
423 424 425 426
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' }
    );
427 428

    @outargs = (
429 430
        { name => 'color', type => 'color', void_ret => 1,
          desc => 'The return color' },
431 432
        { name => 'opacity', type => 'float',
          desc => 'The opacity of the endpoint' }
433 434 435
    );

    %invoke = (
436 437 438 439 440
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

441 442
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_READ, segment,
                           &seg, error);
443 444 445 446 447 448 449 450 451

  if (seg)
    {
      gimp_gradient_segment_get_right_color (gradient, seg, &color);
      opacity = color.a * 100.0;
    }
  else
    success = FALSE;
}
452 453 454 455
CODE
    );
}

456
sub gradient_segment_set_left_color {
Simon Budig's avatar
Simon Budig committed
457
    $blurb = 'Sets the left endpoint color of the specified segment';
458

459
    $help = <<'HELP';
Simon Budig's avatar
Simon Budig committed
460
This procedure sets the left endpoint color of the specified segment of
461 462
the specified gradient.
HELP
463

464
    &shlomi_pdb_misc('2003', '2.2');
465

466
    @inargs = (
467
	{ name => 'name', type => 'string', non_empty => 1,
468
	  desc => 'The gradient name' },
469 470 471 472 473 474
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' },
        { name => 'color', type => 'color',
          desc => 'The color to set' },
        { name => 'opacity', type => '0 <= float <= 100.0',
          desc => 'The opacity to set for the endpoint' }
475
    );
476 477

    %invoke = (
478
        code => <<'CODE'
479
{
480 481
  GimpGradient        *gradient;
  GimpGradientSegment *seg;
482

483 484
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_WRITE, segment,
                           &seg, error);
485

486
  if (seg)
487 488 489
    {
      color.a = opacity / 100.0;
      gimp_gradient_segment_set_left_color (gradient, seg, &color);
490
    }
491 492
  else
    success = FALSE;
493 494 495 496 497
}
CODE
    );
}

498
sub gradient_segment_set_right_color {
Simon Budig's avatar
Simon Budig committed
499
    $blurb = 'Sets the right endpoint color of the specified segment';
500 501

    $help = <<'HELP';
Simon Budig's avatar
Simon Budig committed
502
This procedure sets the right endpoint color of the specified segment of
503
the specified gradient.
504 505
HELP

506 507 508
    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
509
	{ name => 'name', type => 'string', non_empty => 1,
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' },
        { name => 'color', type => 'color',
          desc => 'The color to set' },
        { name => 'opacity', type => '0 <= float <= 100.0',
          desc => 'The opacity to set for the endpoint' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

525 526
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_WRITE, segment,
                           &seg, error);
527

528
  if (seg)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    {
      color.a = opacity / 100.0;
      gimp_gradient_segment_set_right_color (gradient, seg, &color);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_get_left_pos {
    $blurb = 'Retrieves the left endpoint position of the specified segment';

    $help = <<'HELP';
This procedure retrieves the left endpoint position of the specified segment of
the specified gradient.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
551
	{ name => 'name', type => 'string', non_empty => 1,
552 553 554 555 556 557
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' }
    );

    @outargs = (
558 559
        { name => 'pos', type => 'float', void_ret => 1,
          desc => 'The return position' }
560 561 562 563 564 565 566 567
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

568 569
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_READ, segment,
                           &seg, error);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

  if (seg)
    {
      pos = gimp_gradient_segment_get_left_pos (gradient, seg);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_get_right_pos {
    $blurb = 'Retrieves the right endpoint position of the specified segment';

    $help = <<'HELP';
This procedure retrieves the right endpoint position of the specified segment of
the specified gradient.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
593
	{ name => 'name', type => 'string', non_empty => 1,
594 595 596 597 598 599
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' }
    );

    @outargs = (
600 601
        { name => 'pos', type => 'float', void_ret => 1,
          desc => 'The return position' }
602 603 604 605 606 607 608 609
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

610 611
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_READ, segment,
                           &seg, error);
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

  if (seg)
    {
      pos = gimp_gradient_segment_get_right_pos (gradient, seg);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_get_middle_pos {
    $blurb = 'Retrieves the middle point position of the specified segment';

    $help = <<'HELP';
This procedure retrieves the middle point position of the specified segment of
the specified gradient.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
635
	{ name => 'name', type => 'string', non_empty => 1,
636 637 638 639 640 641
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' }
    );

    @outargs = (
642 643
        { name => 'pos', type => 'float', void_ret => 1,
          desc => 'The return position' }
644 645 646 647 648 649 650 651
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

652 653
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_READ, segment,
                           &seg, error);
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672

  if (seg)
    {
      pos = gimp_gradient_segment_get_middle_pos (gradient, seg);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_set_left_pos {
    $blurb = 'Sets the left endpoint position of the specified segment';

    $help = <<'HELP';
This procedure sets the left endpoint position of the specified
segment of the specified gradient. The final position will be between
the position of the middle point to the left to the middle point of
673
the current segment.
674 675 676 677 678 679 680

This procedure returns the final position.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
681
	{ name => 'name', type => 'string', non_empty => 1,
682 683 684 685 686 687 688 689
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' },
        { name => 'pos', type => '0.0 <= float <= 1.0',
          desc => 'The position to set the guidepoint to' }
    );

    @outargs = (
690 691
        { name => 'final_pos', type => 'float', void_ret => 1,
          desc => 'The return position' }
692 693 694 695 696 697 698 699
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

700 701
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_WRITE, segment,
                           &seg, error);
702

703
  if (seg)
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    {
      final_pos = gimp_gradient_segment_set_left_pos (gradient, seg, pos);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_set_right_pos {
    $blurb = 'Sets the right endpoint position of the specified segment';

    $help = <<'HELP';
This procedure sets the right endpoint position of the specified
segment of the specified gradient. The final position will be between
the position of the middle point of the current segment and the middle
point of the segment to the right.

This procedure returns the final position.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
729
	{ name => 'name', type => 'string', non_empty => 1,
730 731 732 733 734 735 736 737
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' },
        { name => 'pos', type => '0.0 <= float <= 1.0',
          desc => 'The position to set the guidepoint to' }
    );

    @outargs = (
738 739
        { name => 'final_pos', type => 'float', void_ret => 1,
          desc => 'The return position' }
740 741 742 743 744 745 746 747
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

748 749
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_WRITE, segment,
                           &seg, error);
750

751
  if (seg)
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    {
      final_pos =
        gimp_gradient_segment_set_right_pos (gradient, seg, pos);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_set_middle_pos {
    $blurb = 'Sets the middle point position of the specified segment';

    $help = <<'HELP';
This procedure sets the middle point position of the specified segment
of the specified gradient. The final position will be between the two
endpoints of the segment.

This procedure returns the final position.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
777
	{ name => 'name', type => 'string', non_empty => 1,
778 779 780 781 782 783 784 785
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' },
        { name => 'pos', type => '0.0 <= float <= 1.0',
          desc => 'The position to set the guidepoint to' }
    );

    @outargs = (
786 787
        { name => 'final_pos', type => 'float', void_ret => 1,
          desc => 'The return position' }
788 789 790 791 792 793 794 795
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

796 797
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_WRITE, segment,
                           &seg, error);
798

799
  if (seg)
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
    {
      final_pos =
        gimp_gradient_segment_set_middle_pos (gradient, seg, pos);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_get_blending_function {
    $blurb = "Retrieves the gradient segment's blending function";

    $help = <<'HELP';
This procedure retrieves the blending function of the segment at the
specified gradient name and segment index.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
822
	{ name => 'name', type => 'string', non_empty => 1,
823 824 825 826 827 828 829
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' }
    );

    @outargs = (
        { name => 'blend_func', type => 'enum GimpGradientSegmentType',
830 831
          void_ret => 1,
	  desc => 'The blending function of the segment' }
832 833 834 835 836 837 838 839
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

840 841
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_READ, segment,
                           &seg, error);
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

  if (seg)
    {
      blend_func = gimp_gradient_segment_get_blending_function (gradient, seg);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_get_coloring_type {
    $blurb = "Retrieves the gradient segment's coloring type";

    $help = <<'HELP';
This procedure retrieves the coloring type of the segment at the
specified gradient name and segment index.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
865
	{ name => 'name', type => 'string', non_empty => 1,
866 867 868 869 870 871 872
	  desc => 'The gradient name' },
        { name => 'segment', type => '0 <= int32',
          desc => 'The index of the segment within the gradient' }
    );

    @outargs = (
        { name => 'coloring_type', type => 'enum GimpGradientSegmentColor',
873 874
          void_ret => 1,
	  desc => 'The coloring type of the segment' }
875 876 877 878 879 880 881 882
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *seg;

883 884
  gradient = gradient_get (gimp, name, GIMP_PDB_DATA_ACCESS_READ, segment,
                           &seg, error);
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

  if (seg)
    {
      coloring_type = gimp_gradient_segment_get_coloring_type (gradient, seg);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_range_set_blending_function {
    $blurb = 'Change the blending function of a segments range';

    $help = <<'HELP';
This function changes the blending function of a segment range to the
specified blending function.
HELP

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
908
	{ name => 'name', type => 'string', non_empty => 1,
909 910 911 912 913 914 915
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' },
        { name => 'blending_function', type => 'enum GimpGradientSegmentType',
916
          desc => 'The blending function' }
917 918 919 920 921 922 923 924 925 926
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
927
                                 &start_seg, &end_seg, error);
928

929
  if (start_seg)
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    {
      gimp_gradient_segment_range_set_blending_function (gradient,
                                                         start_seg, end_seg,
                                                         blending_function);
    }
  else
    success = FALSE;
}
CODE
    );
}

sub gradient_segment_range_set_coloring_type {
    $blurb = 'Change the coloring type of a segments range';

    $help = <<'HELP';
This function changes the coloring type of a segment range to the
947 948
specified coloring type.
HELP
949 950 951 952

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
953
	{ name => 'name', type => 'string', non_empty => 1,
954 955 956 957 958 959 960
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' },
        { name => 'coloring_type', type => 'enum GimpGradientSegmentColor',
961
          desc => 'The coloring type' }
962 963 964 965 966 967 968 969 970 971
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
972
                                 &start_seg, &end_seg, error);
973

974
  if (start_seg)
975 976 977 978 979 980 981 982
    {
      gimp_gradient_segment_range_set_coloring_type (gradient,
                                                     start_seg, end_seg,
                                                     coloring_type);
    }
  else
    success = FALSE;
}
983 984 985 986
CODE
    );
}

987 988 989 990 991 992 993
sub gradient_segment_range_flip {
    $blurb = 'Flip the segment range';
    $help  = 'This function flips a segment range.';

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
994
	{ name => 'name', type => 'string', non_empty => 1,
995 996 997 998 999 1000 1001 1002 1003 1004
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' }
    );

    %invoke = (
        code => <<'CODE'
1005
{
1006 1007 1008 1009 1010
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1011
                                 &start_seg, &end_seg, error);
1012

1013
  if (start_seg)
1014 1015 1016 1017 1018 1019 1020 1021
    {
      gimp_gradient_segment_range_flip (gradient,
                                        start_seg, end_seg,
                                        NULL, NULL);
    }
  else
    success = FALSE;
}
1022 1023 1024 1025
CODE
    );
}

1026 1027 1028 1029
sub gradient_segment_range_replicate {
    $blurb = 'Replicate the segment range';

    $help = <<'HELP';
1030 1031 1032 1033
This function replicates a segment range a given number of times. Instead
of the original segment range, several smaller scaled copies of it
will appear in equal widths.
HELP
1034 1035 1036 1037

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1038
	{ name => 'name', type => 'string', non_empty => 1,
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' },
        { name => 'replicate_times', type => '2 <= int32 <= 20',
          desc => 'The number of times to replicate' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1057
                                 &start_seg, &end_seg, error);
1058

1059
  if (start_seg && gimp_data_is_writable (GIMP_DATA (gradient)))
1060 1061 1062 1063 1064 1065 1066 1067 1068
    {
      gimp_gradient_segment_range_replicate (gradient,
                                             start_seg, end_seg,
                                             replicate_times,
                                             NULL, NULL);
    }
  else
    success = FALSE;
}
1069 1070 1071 1072
CODE
    );
}

1073 1074 1075 1076
sub gradient_segment_range_split_midpoint {
    $blurb = 'Splits each segment in the segment range at midpoint';

    $help = <<'HELP';
1077 1078
This function splits each segment in the segment range at its midpoint.
HELP
1079 1080 1081 1082

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1083
	{ name => 'name', type => 'string', non_empty => 1,
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1100
                                 &start_seg, &end_seg, error);
1101

1102
  if (start_seg)
1103
    {
1104
      gimp_gradient_segment_range_split_midpoint (gradient, context,
1105
                                                  start_seg, end_seg,
1106
                                                  GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
1107 1108 1109 1110 1111
                                                  NULL, NULL);
    }
  else
    success = FALSE;
}
1112 1113 1114 1115
CODE
    );
}

1116 1117 1118 1119
sub gradient_segment_range_split_uniform {
    $blurb = 'Splits each segment in the segment range uniformly';

    $help = <<'HELP';
1120 1121 1122
This function splits each segment in the segment range uniformly according
to the number of times specified by the parameter.
HELP
1123 1124 1125 1126

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1127
	{ name => 'name', type => 'string', non_empty => 1,
1128 1129 1130 1131 1132 1133
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' },
1134
        { name => 'split_parts', type => '2 <= int32 <= 1024',
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
          desc => 'The number of uniform divisions to split each segment to' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1146
                                 &start_seg, &end_seg, error);
1147

1148
  if (start_seg)
1149
    {
1150
      gimp_gradient_segment_range_split_uniform (gradient, context,
1151 1152
                                                 start_seg, end_seg,
                                                 split_parts,
1153
                                                 GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
1154 1155 1156 1157 1158
                                                 NULL, NULL);
    }
  else
    success = FALSE;
}
1159 1160 1161 1162
CODE
    );
}

1163 1164 1165 1166 1167 1168 1169
sub gradient_segment_range_delete {
    $blurb = 'Delete the segment range';
    $help  = 'This function deletes a segment range.';

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1170
	{ name => 'name', type => 'string', non_empty => 1,
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' }
    );

    %invoke = (
        code => <<'CODE'
1181
{
1182 1183 1184 1185 1186
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1187
                                 &start_seg, &end_seg, error);
1188

1189
  if (start_seg)
1190 1191 1192 1193 1194 1195 1196 1197
    {
      gimp_gradient_segment_range_delete (gradient,
                                          start_seg, end_seg,
                                          NULL, NULL);
    }
  else
    success = FALSE;
}
1198 1199 1200 1201
CODE
    );
}

1202 1203 1204 1205
sub gradient_segment_range_redistribute_handles {
    $blurb = "Uniformly redistribute the segment range's handles";

    $help = <<'HELP';
1206 1207 1208
This function redistributes the handles of the specified segment range of the
specified gradient, so they'll be evenly spaced.
HELP
1209 1210 1211 1212

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1213
	{ name => 'name', type => 'string', non_empty => 1,
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1230
                                 &start_seg, &end_seg, error);
1231

1232
  if (start_seg)
1233 1234 1235 1236 1237 1238 1239
    {
      gimp_gradient_segment_range_redistribute_handles (gradient,
                                                        start_seg, end_seg);
    }
  else
    success = FALSE;
}
1240 1241 1242 1243
CODE
    );
}

1244 1245 1246 1247
sub gradient_segment_range_blend_colors {
    $blurb = 'Blend the colors of the segment range.';

    $help = <<'HELP';
1248 1249 1250 1251
This function blends the colors (but not the opacity) of the segments'
range of the gradient. Using it, the colors' transition will be uniform
across the range.
HELP
1252 1253 1254 1255

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1256
	{ name => 'name', type => 'string', non_empty => 1,
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1273
                                 &start_seg, &end_seg, error);
1274

1275
  if (start_seg)
1276
    {
1277 1278 1279
      if (!end_seg)
        end_seg = gimp_gradient_segment_get_last (start_seg);

1280
      gimp_gradient_segment_range_blend (gradient,
1281 1282 1283 1284 1285 1286 1287 1288
                                         start_seg, end_seg,
                                         &start_seg->left_color,
                                         &end_seg->right_color,
                                         TRUE, FALSE);
    }
  else
    success = FALSE;
}
1289 1290 1291 1292
CODE
    );
}

1293 1294 1295 1296
sub gradient_segment_range_blend_opacity {
    $blurb = 'Blend the opacity of the segment range.';

    $help = <<'HELP';
1297 1298 1299 1300
This function blends the opacity (but not the colors) of the segments'
range of the gradient. Using it, the opacity's transition will be uniform
across the range.
HELP
1301 1302 1303 1304

    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1305
	{ name => 'name', type => 'string', non_empty => 1,
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1322
                                 &start_seg, &end_seg, error);
1323

1324
  if (start_seg)
1325
    {
1326 1327 1328
      if (!end_seg)
        end_seg = gimp_gradient_segment_get_last (start_seg);

1329 1330 1331 1332 1333 1334 1335 1336 1337
      gimp_gradient_segment_range_blend (gradient,
                                         start_seg, end_seg,
                                         &start_seg->left_color,
                                         &end_seg->right_color,
                                         FALSE, TRUE);
    }
  else
    success = FALSE;
}
1338 1339 1340 1341
CODE
    );
}

1342 1343 1344 1345
sub gradient_segment_range_move {
    $blurb = 'Move the position of an entire segment range by a delta.';

    $help = <<'HELP';
1346
This function moves the position of an entire segment range by a delta. The
1347 1348 1349 1350
actual delta (which is returned) will be limited by the control points of the
neighboring segments.
HELP

1351 1352 1353
    &shlomi_pdb_misc('2003', '2.2');

    @inargs = (
1354
	{ name => 'name', type => 'string', non_empty => 1,
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	  desc => 'The gradient name' },
        { name => 'start_segment', type => '0 <= int32',
          desc => 'The index of the first segment to operate on' },
        { name => 'end_segment', type => 'int32',
          desc => 'The index of the last segment to operate on. If negative,
                   the selection will extend to the end of the string.' },
        { name => 'delta', type => '-1.0 <= float <= 1.0',
          desc => 'The delta to move the segment range' },
        { name => 'control_compress', type => 'boolean',
          desc => 'Whether or not to compress the neighboring segments' }
    );

    @outargs = (
        { name => 'final_delta', type => 'float',
          desc => 'The final delta by which the range moved' }
    );

    %invoke = (
        code => <<'CODE'
{
  GimpGradient        *gradient;
  GimpGradientSegment *start_seg;
  GimpGradientSegment *end_seg;

  gradient = gradient_get_range (gimp, name, start_segment, end_segment,
1380
                                 &start_seg, &end_seg, error);
1381

1382
  if (start_seg)
1383 1384 1385 1386 1387 1388 1389 1390 1391
    {
      final_delta = gimp_gradient_segment_range_move (gradient,
                                                      start_seg, end_seg,
                                                      delta,
                                                      control_compress);
    }
  else
    success = FALSE;
}
1392 1393 1394 1395
CODE
    );
}

1396
$extra{app}->{code} = <<'CODE';
1397
static GimpGradient *
1398 1399
gradient_get (Gimp                 *gimp,
              const gchar          *name,
1400
              GimpPDBDataAccess     access,
1401
              gint                  segment,
1402
              GimpGradientSegment **seg,
1403
              GError              **error)
1404
{
1405
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name, access, error);
1406 1407 1408 1409 1410 1411 1412 1413 1414

  *seg = NULL;

  if (gradient)
    *seg = gimp_gradient_segment_get_nth (gradient->segments, segment);

  return gradient;
}

1415
static GimpGradient *
1416 1417 1418 1419 1420
gradient_get_range (Gimp                 *gimp,
                    const gchar          *name,
                    gint                  start_segment,
                    gint                  end_segment,
                    GimpGradientSegment **start_seg,
1421
                    GimpGradientSegment **end_seg,
1422
                    GError              **error)
1423
{
1424
  GimpGradient *gradient = gimp_pdb_get_gradient (gimp, name, GIMP_PDB_DATA_ACCESS_WRITE, error);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

  *start_seg = NULL;
  *end_seg   = NULL;

  if (end_segment >= 0 && end_segment < start_segment)
    return NULL;

  if (gradient)
    {
      *start_seg = gimp_gradient_segment_get_nth (gradient->segments,
                                                  start_segment);

      if (*start_seg && end_segment >= 0)
        *end_seg = gimp_gradient_segment_get_nth (*start_seg,
                                                  end_segment -
                                                  start_segment);
    }

  return gradient;
}
CODE

1447

1448 1449 1450 1451 1452 1453
@headers = qw(<string.h>
              "core/gimp.h"
              "core/gimpcontext.h"
              "core/gimpgradient.h"
              "core/gimpdatafactory.h"
              "gimppdb-utils.h");
1454

1455 1456 1457
@procs = qw(gradient_new
            gradient_duplicate
            gradient_is_editable
1458
            gradient_rename
1459
            gradient_delete
1460
            gradient_get_number_of_segments
1461
            gradient_get_uniform_samples gradient_get_custom_samples
1462
            gradient_segment_get_left_color gradient_segment_set_left_color
1463 1464 1465 1466 1467 1468
            gradient_segment_get_right_color gradient_segment_set_right_color
            gradient_segment_get_left_pos gradient_segment_set_left_pos
            gradient_segment_get_middle_pos gradient_segment_set_middle_pos
            gradient_segment_get_right_pos gradient_segment_set_right_pos
            gradient_segment_get_blending_function
            gradient_segment_get_coloring_type
1469 1470
            gradient_segment_range_set_blending_function 
            gradient_segment_range_set_coloring_type
1471 1472 1473 1474 1475 1476 1477 1478 1479
            gradient_segment_range_flip
            gradient_segment_range_replicate
            gradient_segment_range_split_midpoint
            gradient_segment_range_split_uniform
            gradient_segment_range_delete
            gradient_segment_range_redistribute_handles
            gradient_segment_range_blend_colors
            gradient_segment_range_blend_opacity
            gradient_segment_range_move);
1480

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

$desc = 'Gradient';
1484 1485 1486
$doc_title = 'gimpgradient';
$doc_short_desc = 'Functions operating on a single gradient.';
$doc_long_desc = 'Functions operating on a single gradient.';
1487 1488

1;