-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from poissonconsulting/chk5
- Added chk_color(), chk_colour(), vld_color() and vld_colour().
- Loading branch information
Showing
10 changed files
with
219 additions
and
17 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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(chk_color) | ||
export(chk_colour) | ||
export(darken) | ||
export(lighten) | ||
export(tinter) | ||
export(vld_color) | ||
export(vld_colour) | ||
import(chk) |
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,57 @@ | ||
#' Check Color String | ||
#' | ||
#' Checks that x is a string (non-missing character vector of length 1) | ||
#' that specifies a color. | ||
#' | ||
#' @inheritParams chk::chk_true | ||
#' @return `NULL`, invisibly. Called for the side effect of throwing an error | ||
#' if the condition is not met. | ||
#' @seealso [vld_color()] | ||
#' @name chk_color | ||
NULL | ||
|
||
#' @describeIn chk_color Check Color String Object | ||
#' | ||
#' @description | ||
#' | ||
#' `chk_color` | ||
#' checks if a color string. | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' # chk_color | ||
#' chk_color("blue") | ||
#' try(chk_color("glue")) | ||
chk_color <- function(x, x_name = NULL) { | ||
if (vld_color(x)) { | ||
return(invisible()) | ||
} | ||
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x)) | ||
chk_string(x, x_name = x_name) | ||
abort_chk(x_name, " must be a valid color", call. = FALSE) | ||
} | ||
|
||
#' @describeIn chk_color Check Color String Object | ||
#' | ||
#' @description | ||
#' | ||
#' `chk_colour` | ||
#' checks if a color string. | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' # chk_colour | ||
#' chk_colour("blue") | ||
#' try(chk_colour("glue")) | ||
chk_colour <- function(x, x_name = NULL) { | ||
if (vld_colour(x)) { | ||
return(invisible()) | ||
} | ||
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x)) | ||
chk_string(x, x_name = x_name) | ||
abort_chk(x_name, " must be a valid color", call. = FALSE) | ||
} |
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
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,36 @@ | ||
#' Validate Color String | ||
#' | ||
#' Validates whether x is a string (non-missing character vector of length 1) | ||
#' that specifies a color. | ||
#' | ||
#' @inheritParams chk::chk_true | ||
#' @return A flag indicating whether the object was validated. | ||
#' @seealso \code{\link{chk_color}()} | ||
#' @name vld_color | ||
NULL | ||
|
||
#' @describeIn vld_color Validate Color String | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' # vld_color | ||
#' vld_color("blue") | ||
#' vld_color("glue") | ||
vld_color <- function(x) { | ||
vld_string(x) && !inherits(try(grDevices::col2rgb(x), silent = TRUE), "try-error") | ||
} | ||
|
||
#' @describeIn vld_color Validate Colour String | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' # vld_color | ||
#' vld_colour("blue") | ||
#' vld_colour("glue") | ||
vld_colour <- function(x) { | ||
vld_color(x) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,11 @@ | ||
test_that("chk_color", { | ||
expect_null(chk_color("green")) | ||
expect_invisible(chk_color("green")) | ||
chk::expect_chk_error(chk_color(1)) | ||
}) | ||
|
||
test_that("chk_colour", { | ||
expect_null(chk_colour("green")) | ||
expect_invisible(chk_colour("green")) | ||
chk::expect_chk_error(chk_colour(1)) | ||
}) |
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 |
---|---|---|
@@ -1,6 +0,0 @@ | ||
context("utils") | ||
|
||
test_that("utils", { | ||
expect_error(check_colour("b"), "b is not a valid color") | ||
expect_error(check_colour("#000"), "#000 is not a valid color") | ||
}) | ||
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,17 @@ | ||
test_that("vld_color", { | ||
expect_true(vld_color("blue")) | ||
expect_false(vld_color(1)) | ||
expect_false(vld_color(character(0))) | ||
expect_false(vld_color(NA_character_)) | ||
expect_false(vld_color(c("blue", "green"))) | ||
expect_false(vld_color("glue")) | ||
}) | ||
|
||
test_that("vld_colour", { | ||
expect_true(vld_colour("blue")) | ||
expect_false(vld_colour(1)) | ||
expect_false(vld_colour(character(0))) | ||
expect_false(vld_colour(NA_character_)) | ||
expect_false(vld_colour(c("blue", "green"))) | ||
expect_false(vld_colour("glue")) | ||
}) |