#!/opt/bin/perl

# <sjburges@gimp.org>
# This is adrian's idea - take random blends and difference them.  You're
# bound to come up w/ something cool eventually.

use Gimp qw(:auto __ N_);
use Gimp::Fu;
use Gimp::Util;

# Gimp::set_trace(TRACE_ALL);

sub randint {
    my ($int) = @_;
    return int(rand()*$int +0.5);
    }    

register "random_blends", 
         "Random Blends - Perform N random blends", 
         "A random approach to art, using the blend tool with DIFFERENCE_MODE.  Alpha is not affected; best used on opaque background.  Just try it. It might be good.",
         "Seth Burgess", 
         "Seth Burgess <sjburges\@gimp.org>", 
         "1999-03-18",
         N_"<Image>/Filters/Render/Random Blends...", 
         "RGB*, GRAY*", 
         [ 
           [PF_SPINNER, "number", "How many gradients to apply", 7, [1,255,1]], 
            ],
         [],
         ['gimp-1.1'],
         sub {
    my($img,$layer,$numgradients) =@_;
    eval { $img->undo_group_start };  
    $oldgradient = gimp_gradients_get_gradient();
    ($sel,$x1,$y1,$x2,$y2) = $img->gimp_selection_bounds;
    srand();

    @gradientlist = gimp_gradients_get_list("");  
   
    Gimp->progress_init("Random blends...");
    for ($i=0; $i<$numgradients; $i++) {
        gimp_gradients_set_gradient(@gradientlist[randint($#gradientlist)]);
	$layer->edit_blend(CUSTOM_MODE,  
                       DIFFERENCE_MODE,
                       randint(10),      # gradient type
                       randint(100),     # opacity
                       0,                # offset
                       randint(2),       # repeat
		       0,                # reverse
                       0,                # disable supersampling..
		       1,
		       0.1,              
		       0,                # no dithering
                       randint($x2-$x1)+$x1, # x1
                       randint($y2-$y1)+$y1, # y1
                       randint($x2-$x1)+$x1, # x2
                       randint($y2-$y1)+$y1, # y2
                       );
        Gimp->progress_update($i/(1.0*$numgradients));		   
        }
    eval { $img->undo_group_end };
    gimp_gradients_set_gradient($oldgradient);
    return();
};
exit main;
