if (FALSE) {
## first: Let's make a raster out of the 'volcano' data, which we can use in the example:
volcano.raster <- as.raster(t(matrix(terrain.colors(1 + diff(range(volcano)))[volcano -
min(volcano) + 1], nrow(volcano))))
## This will create an image of the original raster using an indexed palette:
volcano.index <- index.colours(volcano.raster)
## The index values can be converted back into colours, using the palette:
volcano.index <- as.raster(apply(volcano.index, 2,
function(x) attributes(volcano.index)$palette[x]))
## Create an indexed image using dithering
volcano.dith <- index.colours(volcano.raster, dither = "floyd-steinberg")
volcano.dith <- as.raster(apply(volcano.dith, 2,
function(x) attributes(volcano.dith)$palette[x]))
## plot the images side by side for comparison
par(mfcol = c(1, 3))
plot(volcano.raster, interpolate = F)
plot(volcano.index, interpolate = F)
plot(volcano.dith, interpolate = F)
}
Run the code above in your browser using DataLab