diff --git a/R/color.R b/R/color.R new file mode 100644 index 0000000..512f3e2 --- /dev/null +++ b/R/color.R @@ -0,0 +1,40 @@ +assert_color <- function(color, null_ok = FALSE, na_ok = FALSE) { + color_pattern <- "(?i)^#[a-f0-9]{3}$|^#[a-f0-9]{6}$|^transparent$" + + checkmate::assert_flag(null_ok) + checkmate::assert_flag(na_ok) + + name <- deparse(substitute(color)) + + if (is.null(color) && isFALSE(null_ok)) { + cli::cli_abort( + paste0( + "{.strong {cli::col_red(name)}} cannot be {.strong NULL}." + ) + ) + } + + if (!is.null(color)) { + if (is.na(color) && isFALSE(na_ok)) { + cli::cli_abort( + paste0( + "{.strong {cli::col_red(name)}} cannot be {.strong NA}." + ) + ) + } + } + + if (!is.null(color) && !is.na(color) && + !color %in% grDevices::colors() && + !checkmate::test_string(color, pattern = color_pattern)) { + cli::cli_abort( + paste0( + "{.strong {cli::col_red(name)}} is not a valid color code. ", + "It must contain a hexadecimal color code or one of the ", + "values in {.strong {cli::col_blue('grDevices::color()')}}." + ) + ) + } + + invisible(NULL) +} diff --git a/README.qmd b/README.qmd index e9ac25e..786c56f 100644 --- a/README.qmd +++ b/README.qmd @@ -1,25 +1,3 @@ ---- -editor: source -output-dir: "." -post-render: "R/.post-render.R" -format: gfm ---- - -```{r} -#| label: setup -#| include: false - -library(beepr) -library(cffr) -library(codemetar) -library(groomr) # https://github.com/danielvartan/groomr -library(here) -library(prettycheck) -library(readr) -library(rutils) # https://github.com/danielvartan/rutils -library(stringr) -``` - # prettycheck diff --git a/codemeta.json b/codemeta.json index 12f2599..4a8208a 100644 --- a/codemeta.json +++ b/codemeta.json @@ -298,7 +298,7 @@ }, "SystemRequirements": null }, - "fileSize": "80.735KB", + "fileSize": "81.768KB", "citation": [ { "@type": "CreativeWork",