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

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

From: Tony Cook (
Date: Thu 05 Feb 2004 - 04:07:17 GMT

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

    On Wed, Feb 04, 2004 at 08:02:34PM -0700, Aaron Dalton wrote:
    > On February 3, 2004 08:33 pm, Tony Cook wrote:
    > > 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
    > Thank you so much for your help thusfar, Tony. I'm trying to get my
    > head around the syntax of this command. What I want to do is write a
    > generic "colour replacement" subroutine that takes the rgb of the
    > colour to replace, along with the rgba of the colour to transform it
    > into. So far I've done:
    > rpnexpr => 'x y getp1 !p @p red '.$redOld.' eq @p green '.$greenOld.' eq
    > and @p blue '.$blueOld.' eq and 0 0 0 0 rgba @p red @p green @p blue
    > 255 rgba if'

    Rather than modifying the code, you might want to consider using
    constants instead, something like:

    my $out = Imager::transform2(
        rpnexpr => 'x y getp1 !p @p red inred eq @p green ingreen eq and @p
    inblue 255 eq' .' and outred outgreen outblue outalpha rgba @p red @p green @p blue 255 rgba if',
        constants =>
            inred => $redOld,
            ingreen => $greenOld,
            inblue => $blueOld,
            outred => $redNew,
            outgreen => $greenNew,
            outblue => $blueNew,
            outalpha => $alphaNew,
      }, $input_image);

    > I'm not quite sure what is happening with the 4 0's in a row. Where
    > should I be putting the variables to interpolate? Sorry for so many
    > questions. Any tutorials anywhere on the transform syntax? =) I'll
    > pour over the documentation again tomorrow and see what I can glean.

    The four zeroes are the output rgba.

    You can play with transform2() at:


    though this runs over a modem, so it's very slow.

    There's no real tutorial, your best choice is probably to look at
    the test code (t/t58trans2.t) and the Imager::Transform source code,
    though that uses some advanced techniques (like the "assembler" expression
    language) that you probably won't ever want to use.

    If I had the bandwidth I'd setup a wiki - this would let build tutorials
    and so on more casually - sitting down to write a tutorial always
    seems like so much of a chore, so they tend not to get done.