Why doesn't /.{r_ ,g_ ,b_ }:>1-{r,g,b} invert RGB encoding?

In the book by Paul Wellin “Programming With Mathematica: An Introduction” (3rd printing; ISBN 978-1-107-00946-2 hardback), he gives an example on page 85 in “Patterns and Rules, Section 4.1”.

It appears that the example attempts to demonstrate the use of “/.” (ReplaceAll) to invert the RGB encoding of the example Image using the Rule {r_,g_,b_}:>1-{r,g,b}.

When I attempt to use this construct on an Image that I have imported, the function does not invert the encoding.
I am using Mathematica v10.3.

Hi coachkic.

images can contain an alpha channel, i.e. each pixel then has the form {r,g,b,alpha}. You can check e.g. using:

Dimensions @ ImageData[img]

More information you can find in the documentation on AlphaChannel and friends.

If this is not the case here you should supply the image.

Regards – Henrik

Thanks, Henrik – The attached notebook excerpt contains the image.
I really appreciate your response.
Coachklc

Maybe something like this can be of help?

color /. RGBColor[r_, g_, b_] -> 1 - # & /@ RGBColor[r, g, b]

Then I think one has to do it like:

newImg = Image[ImageData[oldImg] /. {r_, g_, b_} :> 1 - {r, g, b}]

Thank you very much, Henrik!

Or one could use ColorNegate:

ColorNegate[img]

Well, I think my answer was sort of incomplete! It is a good and legitimate question, why the replacement is only working on the data, but not on the image itself. The reason is that Image yields an atomic object:

img = Image[RandomReal[1, {2, 2, 3}], ImageSize -> 200];
AtomQ[img]  (*  Out:  True  *)

And that means that one has no access to its subparts, i.e. one can not do things like replacements or e.g.:

List @@ img

Regards – Henrik