(none) imager-devel
/ help / lists / applications / search /
 

Re: Replacing one colour with a different (transparent) colour (PNG)

From: Tony Cook (
11219@xyz.molar.is)
Date: Wed 04 Feb 2004 - 03:33:47 GMT

  • Next message: Aaron Dalton: "Re: Replacing one colour with a different (transparent) colour (PNG)"

    On Tue, Feb 03, 2004 at 06:28:23PM -0700, Aaron Dalton wrote:
    > OK. So I can now replace one colour with another. I am now trying to
    > make the replacement colour transparent. Say I want to replace white
    > with red; I have the following code:
    >
    > <code>
    > my $redmap = [0 .. 255];
    > my $blumap = [0 .. 255];
    > my $grnmap = [0 .. 255];
    >
    > $blumap->[255] = 0;
    > $grnmap->[255] = 0;
    >
    > $img->map(red=>$redmap, blue=>$blumap, green=>$grnmap);
    > </code>
    >
    > This replaces all instances of (255,255,255) with (255,0,0). Now how do
    > I make that red transparent? I thought I would need to do:

    Actually, it sets the blue channel to 0 if it was 255 and the green
    channel to 0 if it was 255. map() treats the channels independently.

    >
    > <code>
    > my $redmap = [0 .. 255];
    > my $blumap = [0 .. 255];
    > my $grnmap = [0 .. 255];
    > my $alpha = [0 .. 255];
    >
    > $blumap->[255] = 0;
    > $grnmap->[255] = 0;
    > $alpha->[0] = 255;
    >
    > $img->map(red=>$redmap, blue=>$blumap, green=>$grnmap, alpha=>$alpha);
    > </code>
    >
    > But that doesn't do anything. Any help would be greatly appreciated.

    Again, as above, and the alpha channel is set to 0 if it was 255.

    You might want to look at using transform2() to do the conversion:

    my $out = Imager::transform2(
      {
        channels=>4,
        rpnexpr => 'x y getp1 !p @p red 255 eq @p green 255 eq and @p blue 255 eq'
                   .' and 0 0 0 0 rgba @p red @p green @p blue 255 rgba if'
      }, $input_image);

    Tony



  •