diff --git a/NAMESPACE b/NAMESPACE index 06d81d3..acde10c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/chk.R b/R/chk.R new file mode 100644 index 0000000..7deec60 --- /dev/null +++ b/R/chk.R @@ -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) +} diff --git a/R/tinter.R b/R/tinter.R index 46af02d..d142c12 100644 --- a/R/tinter.R +++ b/R/tinter.R @@ -13,7 +13,7 @@ #' tinter("#fa6a5c", steps = 10, crop = 3) #' tinter("#fa6a5c", direction = "tints") tinter <- function(x, steps = 5, crop = 1, direction = "both", adjust = 0) { - check_colour(x) + chk_colour(x) chk_whole_number(steps) chk_whole_number(crop) chk_string(direction) @@ -56,7 +56,7 @@ tinter <- function(x, steps = 5, crop = 1, direction = "both", adjust = 0) { #' @examples #' darken(tinter("blue"), 0.2) darken <- function(x, amount) { - lapply(x, check_colour) + chk_all(x, chk_colour) chk_number(amount) chk_range(amount) sapply(x, function(x) { @@ -74,7 +74,7 @@ darken <- function(x, amount) { #' @examples #' lighten(tinter("blue"), 0.2) lighten <- function(x, amount) { - lapply(x, check_colour) + chk_all(x, chk_colour) chk_number(amount) chk_range(amount) diff --git a/R/utils.R b/R/utils.R index d3db3cf..8d75479 100644 --- a/R/utils.R +++ b/R/utils.R @@ -13,11 +13,3 @@ tint <- function(x, steps, crop) { } tint[-(seq_len(crop))] } - -check_colour <- function(x) { - chk_string(x) - res <- try(grDevices::col2rgb(x), silent = TRUE) - if (class(res) == "try-error") { - stop(x, " is not a valid color", call. = FALSE) - } -} diff --git a/R/vld.R b/R/vld.R new file mode 100644 index 0000000..0e8b2bb --- /dev/null +++ b/R/vld.R @@ -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) +} diff --git a/man/chk_color.Rd b/man/chk_color.Rd new file mode 100644 index 0000000..32f311b --- /dev/null +++ b/man/chk_color.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/chk.R +\name{chk_color} +\alias{chk_color} +\alias{chk_colour} +\title{Check Color String} +\usage{ +chk_color(x, x_name = NULL) + +chk_colour(x, x_name = NULL) +} +\arguments{ +\item{x}{The object to check.} + +\item{x_name}{A string of the name of object x or NULL.} +} +\value{ +\code{NULL}, invisibly. Called for the side effect of throwing an error +if the condition is not met. +} +\description{ +Checks that x is a string (non-missing character vector of length 1) +that specifies a color. + +\code{chk_color} +checks if a color string. + +\code{chk_colour} +checks if a color string. +} +\section{Functions}{ +\itemize{ +\item \code{chk_color}: Check Color String Object + +\item \code{chk_colour}: Check Color String Object +}} + +\examples{ + +# chk_color +chk_color("blue") +try(chk_color("glue")) + +# chk_colour +chk_colour("blue") +try(chk_colour("glue")) +} +\seealso{ +\code{\link[=vld_color]{vld_color()}} +} diff --git a/man/vld_color.Rd b/man/vld_color.Rd new file mode 100644 index 0000000..4094a41 --- /dev/null +++ b/man/vld_color.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/vld.R +\name{vld_color} +\alias{vld_color} +\alias{vld_colour} +\title{Validate Color String} +\usage{ +vld_color(x) + +vld_colour(x) +} +\arguments{ +\item{x}{The object to check.} +} +\value{ +A flag indicating whether the object was validated. +} +\description{ +Validates whether x is a string (non-missing character vector of length 1) +that specifies a color. +} +\section{Functions}{ +\itemize{ +\item \code{vld_color}: Validate Color String + +\item \code{vld_colour}: Validate Colour String +}} + +\examples{ + +# vld_color +vld_color("blue") +vld_color("glue") + +# vld_color +vld_colour("blue") +vld_colour("glue") +} +\seealso{ +\code{\link{chk_color}()} +} diff --git a/tests/testthat/test-chk.R b/tests/testthat/test-chk.R new file mode 100644 index 0000000..c7cae00 --- /dev/null +++ b/tests/testthat/test-chk.R @@ -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)) +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 270cd74..e69de29 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -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") -}) diff --git a/tests/testthat/test-vld.R b/tests/testthat/test-vld.R new file mode 100644 index 0000000..6fa8294 --- /dev/null +++ b/tests/testthat/test-vld.R @@ -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")) +})