Skip to content

Commit

Permalink
Fix default colors
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Nov 21, 2024
1 parent 45d4b43 commit 54372be
Show file tree
Hide file tree
Showing 6 changed files with 2,401 additions and 17 deletions.
4 changes: 4 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,11 @@ setGeneric(
#' tiles on each axis.
#' @param palette A [`function`] that takes a single `numeric` vector
#' (the output of `f`) as argument and returns a vector of color.
#' If `NULL`, the default color scheme will be used. If `FALSE`, the output
#' of `f` is used as colors.
#' @param ... Further parameters to be passed to `f`.
#' @return
#' `ternary_image()` is called it for its side-effects.
#' @example inst/examples/ex-image.R
#' @author N. Frerebeau
#' @docType methods
Expand Down
18 changes: 11 additions & 7 deletions R/ternary_image.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ NULL
setMethod(
f = "ternary_image",
signature = c(f = "function"),
definition = function(f, n = 48,
palette = grDevices::colorRamp(c("red", "green")), ...) {
definition = function(f, n = 48, palette = NULL, ...) {

tri <- .triangle_center(n)
xyz <- coordinates_cartesian(tri$x, tri$y)
val <- f(xyz$x, xyz$y, xyz$z, ...)

if (is.null(palette)) {
if (isFALSE(palette)) {
color <- val
} else {
if (is.numeric(val)) {
val <- (val - min(val)) / (max(val) - min(val)) # Rescale to [0,1]
}
}
if (is.null(palette)) {
palette <- function(x) {
x <- (x - min(x)) / (max(x) - min(x)) # Rescale to [0,1]
col <- grDevices::hcl.colors(256L, palette = "viridis")
grDevices::rgb(grDevices::colorRamp(col)(x), maxColorValue = 255)
}
}
if (is.function(palette)) {
color <- palette(val)
}

Expand Down
3 changes: 2 additions & 1 deletion inst/examples/ex-image.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
## RGB
ternary_plot(NULL, xlab = "Red", ylab = "Green", zlab = "Blue")
ternary_image(f = rgb, n = 20, palette = NULL)
ternary_image(f = rgb, n = 20, palette = FALSE)
Loading

0 comments on commit 54372be

Please sign in to comment.