-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e40db95
commit 0d36573
Showing
3 changed files
with
41 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters