From 54372be54d31afbfc6a271813c2ed98089b4d6c5 Mon Sep 17 00:00:00 2001 From: nfrerebeau Date: Thu, 21 Nov 2024 16:27:26 +0100 Subject: [PATCH] Fix default colors --- R/AllGenerics.R | 4 + R/ternary_image.R | 18 +- inst/examples/ex-image.R | 3 +- .../tinytest/_tinysnapshot/image_function.svg | 2367 +++++++++++++++++ inst/tinytest/test_geometry.R | 9 +- man/ternary_image.Rd | 17 +- 6 files changed, 2401 insertions(+), 17 deletions(-) create mode 100644 inst/tinytest/_tinysnapshot/image_function.svg diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 29f9170..809eadf 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -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 diff --git a/R/ternary_image.R b/R/ternary_image.R index f7ab9fc..291b7ea 100644 --- a/R/ternary_image.R +++ b/R/ternary_image.R @@ -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) } diff --git a/inst/examples/ex-image.R b/inst/examples/ex-image.R index 8051b32..35629f4 100644 --- a/inst/examples/ex-image.R +++ b/inst/examples/ex-image.R @@ -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) diff --git a/inst/tinytest/_tinysnapshot/image_function.svg b/inst/tinytest/_tinysnapshot/image_function.svg new file mode 100644 index 0000000..0f2ce6e --- /dev/null +++ b/inst/tinytest/_tinysnapshot/image_function.svg @@ -0,0 +1,2367 @@ + + + + + + + + + + + + + + + + + + + +20 +40 +60 +80 + + + + + +20 +40 +60 +80 + + + + + +20 +40 +60 +80 + + + + + + +x +y +z + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test_geometry.R b/inst/tinytest/test_geometry.R index 07fd39f..ca46c8b 100644 --- a/inst/tinytest/test_geometry.R +++ b/inst/tinytest/test_geometry.R @@ -76,9 +76,16 @@ if (at_home()) { expect_snapshot_plot(geom_text, "geom_text") # Image ====================================================================== + f <- function(x, y, z) x - z + (3 * x * y) + (50 * x * y * z) + image_function <- function() { + ternary_plot(NULL) + ternary_image(f = f, n = 48) + } + expect_snapshot_plot(image_function, "image_function") + image_rgb <- function() { ternary_plot(NULL, xlab = "Red", ylab = "Green", zlab = "Blue") - ternary_image(f = grDevices::rgb, n = 20, palette = NULL) + ternary_image(f = grDevices::rgb, n = 20, palette = FALSE) } expect_snapshot_plot(image_rgb, "image_rgb") } diff --git a/man/ternary_image.Rd b/man/ternary_image.Rd index 7776d1d..ad9bed6 100644 --- a/man/ternary_image.Rd +++ b/man/ternary_image.Rd @@ -9,12 +9,7 @@ \usage{ ternary_image(f, ...) -\S4method{ternary_image}{function}( - f, - n = 48, - palette = grDevices::colorRamp(c("red", "green")), - ... -) +\S4method{ternary_image}{function}(f, n = 48, palette = NULL, ...) } \arguments{ \item{f}{A \code{\link{function}} that takes three arguments (x, y and z coordinates) @@ -26,15 +21,21 @@ and returns a \code{numeric} vector.} tiles on each axis.} \item{palette}{A \code{\link{function}} that takes a single \code{numeric} vector -(the output of \code{f}) as argument and returns a vector of color.} +(the output of \code{f}) as argument and returns a vector of color. +If \code{NULL}, the default color scheme will be used. If \code{FALSE}, the output +of \code{f} is used as colors.} +} +\value{ +\code{ternary_image()} is called it for its side-effects. } \description{ Creates a grid of colored triangles with colors corresponding to the output of a function. } \examples{ +## 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) } \seealso{ Other geometries: