Here I produce a Raster
image with large uniformly colored rectangles and put it in an Inset
:
With[{n = 5},
Graphics[Inset[
Graphics[Raster[
{Map[
List @@ ColorConvert[ColorData["LakeColors"][#],
RGBColor] &, (Range[n - 1] - 1)/(n - 2)]}
]],
Automatic, Automatic, {.45, .5}
]
]
]
Now export this to PDF
:
Export["p.pdf", %]
The uniform regions have been replaced by a series of discrete bands that seem to try an interpolate between the rectangles. This happens if I choose fewer rectangles too (e.g., n = 3
).
If I generate the same thing without using Raster
, the exported PDF
looks fine - e.g., try this:
With[{n = 5},
Graphics[Inset[
Graphics[
MapIndexed[{ColorData["LakeColors"][#],
Translate[Rectangle[], {#2[[1]], 0}]} &, (Range[n - 1] -
1)/(n - 2)]
]
,
Automatic, Automatic, {.45, .5}
]
]
]
So it's the conversion of the rasterized image in Export
that's responsible for this effect. Interestingly, one can work around this (on Mac) by selecting the graphic, going to Edit > Copy As > PDF
and pasting the result into Preview
.
Although I have some workarounds now, it still seems mysterious to me why this banding effect happens with Raster
. And it would of course be nice if someone knew how to use Raster
in an Inset
inside a Graphics
without triggering the effect that causes the blocks of the raster to lose their uniform color.
Also - the arguments to Inset
can be left out (in particular the scaling {.45, .5}
). But it seemed to me that this problem didn't always happen with Raster
, and that it probably has something to do with the size of the bitmap being scaled up relative to some assumed coordinate system used in Export
.
PDF
source and see if I can come up with a fix along the lines of theMediaBox
replacement I did on this page - it didn't occur to me that I'd have to revisit that in this context... $\endgroup$ColorConvert[..., "CMYK"]
to theGraphics[Raster[...]]
expression, and the output looks fine. I want to try and see if one can do even better, though... $\endgroup$