mirror of
https://github.com/agdamsbo/FreesearchR.git
synced 2026-06-19 12:37:30 +02:00
Compare commits
No commits in common. "0d4f51f176a3becccef8f620b66e1f52ad194528" and "41c855a71c4f5d14bd8be47c0546c189916cf3e0" have entirely different histories.
0d4f51f176
...
41c855a71c
3 changed files with 49 additions and 46 deletions
|
|
@ -17,5 +17,3 @@
|
||||||
^app*$
|
^app*$
|
||||||
^page$
|
^page$
|
||||||
^demo$
|
^demo$
|
||||||
^\.positai$
|
|
||||||
^\.claude$
|
|
||||||
|
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -16,4 +16,3 @@ app
|
||||||
page
|
page
|
||||||
demo
|
demo
|
||||||
visuals
|
visuals
|
||||||
.positai
|
|
||||||
|
|
|
||||||
|
|
@ -56,25 +56,38 @@
|
||||||
#'
|
#'
|
||||||
#' @export
|
#' @export
|
||||||
generate_colors <- function(n, palette = "viridis", ...) {
|
generate_colors <- function(n, palette = "viridis", ...) {
|
||||||
|
if (!is.numeric(n) ||
|
||||||
# --- Input validation -------------------------------------------------------
|
length(n) != 1 || n < 1 || n != as.integer(n)) {
|
||||||
if (!is.numeric(n) || length(n) != 1 || n < 1 || n %% 1 != 0) {
|
|
||||||
stop("`n` must be a single positive integer.")
|
stop("`n` must be a single positive integer.")
|
||||||
}
|
}
|
||||||
if (!is.function(palette) && (!is.character(palette) || length(palette) != 1)) {
|
|
||||||
stop("`palette` must be a single character string or a function.")
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- Function passthrough ---------------------------------------------------
|
# Function passthrough — call directly with n and ...
|
||||||
if (is.function(palette)) {
|
if (is.function(palette)) {
|
||||||
return(palette(n, ...))
|
return(palette(n, ...))
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Named palette dispatch -------------------------------------------------
|
if (!is.character(palette) || length(palette) != 1) {
|
||||||
|
stop("`palette` must be a single character string or a function.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is.numeric(n) ||
|
||||||
|
length(n) != 1 || n < 1 || n != as.integer(n)) {
|
||||||
|
stop("`n` must be a single positive integer.")
|
||||||
|
}
|
||||||
|
if (!is.character(palette) || length(palette) != 1) {
|
||||||
|
stop("`palette` must be a single character string.")
|
||||||
|
}
|
||||||
|
|
||||||
palette_lower <- tolower(palette)
|
palette_lower <- tolower(palette)
|
||||||
|
|
||||||
viridis_palettes <- c("viridis", "magma", "plasma", "inferno",
|
viridis_palettes <- c("viridis",
|
||||||
"cividis", "mako", "rocket", "turbo")
|
"magma",
|
||||||
|
"plasma",
|
||||||
|
"inferno",
|
||||||
|
"cividis",
|
||||||
|
"mako",
|
||||||
|
"rocket",
|
||||||
|
"turbo")
|
||||||
|
|
||||||
if (palette_lower %in% viridis_palettes) {
|
if (palette_lower %in% viridis_palettes) {
|
||||||
viridisLite::viridis(n = n, option = palette_lower, ...)
|
viridisLite::viridis(n = n, option = palette_lower, ...)
|
||||||
|
|
@ -94,42 +107,35 @@ generate_colors <- function(n, palette = "viridis", ...) {
|
||||||
} else if (palette_lower == "topo") {
|
} else if (palette_lower == "topo") {
|
||||||
grDevices::topo.colors(n = n, ...)
|
grDevices::topo.colors(n = n, ...)
|
||||||
|
|
||||||
|
} else if (palette %in% rownames(RColorBrewer::brewer.pal.info)) {
|
||||||
|
max_n <- RColorBrewer::brewer.pal.info[palette, "maxcolors"]
|
||||||
|
fetch_n <- max(min(n, max_n), 3L) # clamp to [3, max_n] for brewer.pal()
|
||||||
|
base_colors <- RColorBrewer::brewer.pal(n = fetch_n, name = palette)
|
||||||
|
grDevices::colorRampPalette(base_colors)(n)
|
||||||
|
|
||||||
|
} else if (palette %in% grDevices::palette.pals()) {
|
||||||
|
grDevices::colorRampPalette(palette.colors(palette = palette))(n)
|
||||||
|
|
||||||
|
} else if (palette %in% grDevices::hcl.pals()) {
|
||||||
|
grDevices::hcl.colors(n = n, palette = palette, ...)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
# Case-insensitive RColorBrewer lookup
|
message(
|
||||||
brewer_names <- rownames(RColorBrewer::brewer.pal.info)
|
paste0(
|
||||||
brewer_match <- brewer_names[match(palette_lower, tolower(brewer_names))]
|
"Unknown palette: '",
|
||||||
|
palette,
|
||||||
if (!is.na(brewer_match)) {
|
"'. ",
|
||||||
max_n <- RColorBrewer::brewer.pal.info[brewer_match, "maxcolors"]
|
"Falling back to default R colors.\n",
|
||||||
fetch_n <- max(min(n, max_n), 3L)
|
"Available options:\n",
|
||||||
base_colors <- RColorBrewer::brewer.pal(n = fetch_n, name = brewer_match)
|
" viridisLite : viridis, magma, plasma, inferno, cividis, mako, rocket, turbo\n",
|
||||||
grDevices::colorRampPalette(base_colors)(n)
|
" grDevices : hcl, rainbow, heat, terrain, topo\n",
|
||||||
|
" grDevices HCL: use grDevices::hcl.pals() to see all options\n",
|
||||||
} else {
|
" grDevices : use grDevices::palette.pals() to see all options\n",
|
||||||
# Case-insensitive grDevices palette.pals() lookup
|
" RColorBrewer : use RColorBrewer::brewer.pal.info to see all options"
|
||||||
pal_names <- grDevices::palette.pals()
|
)
|
||||||
pal_match <- pal_names[match(palette_lower, tolower(pal_names))]
|
)
|
||||||
|
viridisLite::viridis(n = n, option = "viridis")
|
||||||
if (!is.na(pal_match)) {
|
# grDevices::hcl.colors(n = n)
|
||||||
grDevices::colorRampPalette(grDevices::palette.colors(palette = pal_match))(n)
|
|
||||||
|
|
||||||
} else if (palette %in% grDevices::hcl.pals()) {
|
|
||||||
# Named HCL palettes (e.g. "Rocket", "Plasma") — distinct from viridisLite
|
|
||||||
grDevices::hcl.colors(n = n, palette = palette, ...)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
warning(
|
|
||||||
"Unknown palette: '", palette, "'. Falling back to viridis.\n",
|
|
||||||
"Available options:\n",
|
|
||||||
" viridisLite : viridis, magma, plasma, inferno, cividis, mako, rocket, turbo\n",
|
|
||||||
" grDevices : hcl, rainbow, heat, terrain, topo\n",
|
|
||||||
" grDevices HCL: use grDevices::hcl.pals() to see all options\n",
|
|
||||||
" grDevices : use grDevices::palette.pals() to see all options\n",
|
|
||||||
" RColorBrewer : use RColorBrewer::brewer.pal.info to see all options"
|
|
||||||
)
|
|
||||||
viridisLite::viridis(n = n, option = "viridis")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue