#!/usr/bin/perl 

use Gimp qw( :auto );
use Gimp::Fu;

# alpha2color.pl
# by Seth Burgess <sjburges@gimp.org>
# Version 0.02
# Oct 16th, 1998
#
# Rewritten for GIMP 1.2 and the Graphics Muse Tools CD
# by Michael J. Hammel (mjhammel@graphics-muse.org
# Jan 26, 2002
#
# This script simply changes the current alpha channel to a given color
# instead.  I'm writing it primarily for use with the displace plugin, 
# but I imagine it'll have other uses.

# TODO: Selection is currently ignored.  It'd be better if it remembered
#       what the previous selection was.
#       Also, it needs to find a happier home than in the Filters/Misc menu.
#
# MJH: Moved to Filters/Colors, since that's what it operates on.

# Gimp::set_trace(TRACE_ALL);

# Revision History
# v0.02 - fixed up @color (should be $color) and undef; (should be return();)

sub alpha2col {
	my ($img, $drawable, $color) = @_;

	$img->undo_push_group_start;

	my $oldcolor = gimp_palette_get_background();

	my @layers = gimp_image_get_layers($img);

# if there's not enough layers, abort.	
	if ($#layers < 0) {
		gimp_message("You need at least 1 layer to perform alpha2color!");
		print "Only ", scalar(@layers), " layers found!(", $layers[0],")\n";
		return 0;
		}

	$target_layer = gimp_image_get_active_layer($img);
   gimp_palette_set_background($color);
	$target_layer->selection_layer_alpha;
	$img->selection_invert;
	$target_layer->edit_fill(1);
	$img->selection_none;

	gimp_palette_set_background($oldcolor);

	# End the undo group.
	$img->undo_push_group_end;

	# Display it.
	Gimp->displays_flush();
	return();
	}

register
	"plug_in_alpha2color",
	"Alpha 2 Color",
	"Change the current alpha to a selected color.",
	"Seth Burgess",
	"Seth Burgess<sjburges\@gimp.org>",
	"1998-10-18",
	"<Image>/Filters/Colors/Alpha to Color",
	"RGBA",
	[
	 [PF_COLOR, "color", "Color for current alpha", [127,127,127]]
	],
	\&alpha2col;

exit main();	
	


