Skip to content

Commit

Permalink
Add assert_color()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvartan committed Nov 4, 2024
1 parent e40db95 commit 0d36573
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
40 changes: 40 additions & 0 deletions R/color.R
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 0 additions & 22 deletions README.qmd
Original file line number Diff line number Diff line change
@@ -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

<!-- badges: start -->
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
},
"SystemRequirements": null
},
"fileSize": "80.735KB",
"fileSize": "81.768KB",
"citation": [
{
"@type": "CreativeWork",
Expand Down

0 comments on commit 0d36573

Please sign in to comment.