shawnhcorey has asked for the wisdom of the Perl Monks concerning the following question:

I'm using GD to create some images that will blend with a transparent background but things aren't working out right. I'm hoping someone can point out what is wrong.

Here is the code; as you can see there's no anti-aliasing at all.

#!/usr/bin/env perl use 5.010; use strict; use warnings; # -------------------------------------- use GD; my @white = ( 0xFF, 0xFF, 0xFF ); my @black = ( 0x00, 0x00, 0x00 ); my $size = shift @ARGV || 400; my $offset = $size / 2; my $height = 0.9 * $offset; my $width = 0.1 * $offset; my $img = GD::Image->new( $size, $size, 1 ); $img->saveAlpha( 1 ); $img->alphaBlending( 1 ); # background color: black & transparent my $bg = $img->colorAllocateAlpha( @black, gdAlphaTransparent ); $img->fill( 0, 0, $bg ); # a simple polygon my $p = GD::Polygon->new(); $p->addPt( $offset + $width, $offset + 0 ); $p->addPt( $offset + 0, $offset - $height ); $p->addPt( $offset - $width, $offset + 0 ); $p->addPt( $offset + 0, $offset + $width ); # polygon color: black & opaque my $clr = $img->colorAllocateAlpha( @black, gdAlphaOpaque ); $img->setAntiAliased( $clr ); $img->filledPolygon( $p, gdAntiAliased ); # for testing, use script name for PNG file my $png_file = "$0.png"; open my $fh, '>:raw', $png_file or die "could not open $png_file: $!\n +"; print {$fh} $img->png() or die "could not print to $png_file: $!\n"; close $fh or die "could not close $png_file: $!\n"; __END__

I changed the background colour to transparent white and it seems that the transparency is anti-aliasing in the complete opposite of what it should be. That is, opaque where it should be transparent and vice versa.

#!/usr/bin/env perl use 5.010; use strict; use warnings; # -------------------------------------- use GD; my @white = ( 0xFF, 0xFF, 0xFF ); my @black = ( 0x00, 0x00, 0x00 ); my $size = shift @ARGV || 400; my $offset = $size / 2; my $height = 0.9 * $offset; my $width = 0.1 * $offset; my $img = GD::Image->new( $size, $size, 1 ); $img->saveAlpha( 1 ); $img->alphaBlending( 1 ); # background color: white & transparent my $bg = $img->colorAllocateAlpha( @white, gdAlphaTransparent ); $img->fill( 0, 0, $bg ); # a simple polygon my $p = GD::Polygon->new(); $p->addPt( $offset + $width, $offset + 0 ); $p->addPt( $offset + 0, $offset - $height ); $p->addPt( $offset - $width, $offset + 0 ); $p->addPt( $offset + 0, $offset + $width ); # polygon color: black & opaque my $clr = $img->colorAllocateAlpha( @black, gdAlphaOpaque ); $img->setAntiAliased( $clr ); $img->filledPolygon( $p, gdAntiAliased ); # for testing, use script name for PNG file my $png_file = "$0.png"; open my $fh, '>:raw', $png_file or die "could not open $png_file: $!\n +"; print {$fh} $img->png() or die "could not print to $png_file: $!\n"; close $fh or die "could not close $png_file: $!\n"; __END__

How can I fix this?