Skip to content

Commit

Permalink
add 'alpha' and 'direction' arguments for #5
Browse files Browse the repository at this point in the history
  • Loading branch information
aitap committed Nov 13, 2020
1 parent ae673b2 commit a33aa0e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
importFrom(grDevices, colorRampPalette, rgb)
export(cmocean)
15 changes: 8 additions & 7 deletions R/cmocean.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
cmocean <- function(name, version = getOption('cmocean-version', '2.0'), clip = 0, start = clip/2, end = 1 - clip/2) {
cmocean <- function(
name, version = getOption('cmocean-version', '2.0'), clip = 0,
start = clip/2, end = 1 - clip/2, direction = 1, alpha = 1
) {
cols <- palettes[[version]][[name]]
ncols <- nrow(cols)-1
grDevices::colorRampPalette(
grDevices::rgb(
cols[1 + (ncols*start):(ncols*end),]
)
)
ncols <- nrow(cols) - 1
rows <- (ncols*start):(ncols*end)
if (direction < 0) rows <- rev(rows)
colorRampPalette(rgb(red = cols[1 + rows,], alpha = alpha), alpha = TRUE)
}
24 changes: 16 additions & 8 deletions man/cmocean.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ paramter.
\usage{
cmocean(
name, version = getOption("cmocean-version", "2.0"),
clip = 0, start = clip/2, end = 1 - clip/2
clip = 0, start = clip/2, end = 1 - clip/2,
direction = 1, alpha = 1
)
}
\arguments{
Expand Down Expand Up @@ -41,6 +42,14 @@ cmocean(
fraction of the colormap to clip from its end (overrides
\code{clip}).
}

\item{direction}{
set to \code{-1} to reverse the palette.
}

\item{alpha}{
opacity of the palette in the range \eqn{[0, 1]}.
}
}
\details{
Names of available palettes depend on the version you would
Expand Down Expand Up @@ -189,13 +198,12 @@ cmocean(
sense to request more than 256 of them.
}
\examples{
z <- xtabs(weight~Time+Chick, ChickWeight)
x <- sort(as.numeric(rownames(z)))
y <- sort(as.numeric(colnames(z)))
image(
x = x, y = y, z = z, col = cmocean('delta')(100),
xlab = 'Time', ylab = 'Chick'
)
image(volcano, col = cmocean('thermal')(100))
image(volcano, col = cmocean('Temperature', '0.03')(128))
image(volcano, col = cmocean('thermal', clip = .2)(256))
image(volcano, col = cmocean('thermal', start = .1, end = .7)(256))
image(volcano, col = cmocean('thermal', direction = -1)(256))
image(volcano, col = cmocean('thermal', alpha = .5)(256))
}
\keyword{ color }
% vi:ts=2:et

0 comments on commit a33aa0e

Please sign in to comment.