Skip to content

Simulating alpha for Postscript EPS image output.

Vince Buffalo edited this page Mar 22, 2016 · 3 revisions

Postscript/EPS images don't support transparency (e.g. alpha) values. One hack around this that approximates (I have yet to work out the actual mapping using color theory) alpha is to use colorRampPalette(). This assumes your background is white, but this can be changed.

simulateAlpha <- Vectorize(function(col, alpha, bg='#FFFFFF') {
  cols <- colorRampPalette(c(bg, col))(100)
  cols[floor(alpha*100)]
}, 'col')


addAlpha <- Vectorize(function(col, alpha) {
  # for comparison
  args <- as.list(col2rgb(col)[,1]/255)
  args$alpha <- alpha
  do.call(rgb, args)
}, c('col', 'alpha'))

plot(1:100, pch=19, cex=2,
     col=addAlpha(wes_palette("Darjeeling")[1], seq(0, 1, length.out=100)))
points(1:100 - 5, 1:100, pch=19, cex=2,
     col=simulateAlpha(wes_palette("Darjeeling")[1], seq(0, 1, length.out=100)))

It's a quick approximation, but works.

Clone this wiki locally