diff --git a/DESCRIPTION b/DESCRIPTION index b53d71f..d9a89d9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,3 +19,4 @@ Encoding: UTF-8 LazyData: true RoxygenNote: 7.1.0 Language: en-US +Roxygen: list(markdown = TRUE) diff --git a/R/tinter.R b/R/tinter.R index 2082fd4..46af02d 100644 --- a/R/tinter.R +++ b/R/tinter.R @@ -12,8 +12,7 @@ #' tinter("blue") #' tinter("#fa6a5c", steps = 10, crop = 3) #' tinter("#fa6a5c", direction = "tints") - -tinter <- function(x, steps = 5, crop = 1, direction = "both", adjust = 0){ +tinter <- function(x, steps = 5, crop = 1, direction = "both", adjust = 0) { check_colour(x) chk_whole_number(steps) chk_whole_number(crop) @@ -22,25 +21,26 @@ tinter <- function(x, steps = 5, crop = 1, direction = "both", adjust = 0){ chk_number(adjust) chk_range(adjust, c(-1, 1)) - if(crop > steps) + if (crop > steps) { stop("crop cannot be greater than steps.", call. = FALSE) + } shades <- shade(x, steps, crop) tints <- tint(x, steps, crop) res <- c(tints, shades[-1]) - if(direction == "shades"){ + if (direction == "shades") { res <- shades } - if(direction == "tints"){ + if (direction == "tints") { res <- tints } - if(adjust == 0){ + if (adjust == 0) { return(res) } - if(adjust > 0){ + if (adjust > 0) { return(lighten(res, 1 - adjust)) } darken(res, abs(adjust)) @@ -55,12 +55,13 @@ tinter <- function(x, steps = 5, crop = 1, direction = "both", adjust = 0){ #' @export #' @examples #' darken(tinter("blue"), 0.2) - -darken <- function(x, amount){ +darken <- function(x, amount) { lapply(x, check_colour) chk_number(amount) chk_range(amount) - sapply(x, function(x){shade(x, 100, 0)[amount*100]}, USE.NAMES = FALSE) + sapply(x, function(x) { + shade(x, 100, 0)[amount * 100] + }, USE.NAMES = FALSE) } #' Lighten colour. @@ -72,14 +73,15 @@ darken <- function(x, amount){ #' @export #' @examples #' lighten(tinter("blue"), 0.2) - -lighten <- function(x, amount){ +lighten <- function(x, amount) { lapply(x, check_colour) chk_number(amount) chk_range(amount) - if(amount == 0){ + if (amount == 0) { amount <- 0.01 } - sapply(x, function(x){tint(x, 100, 0)[amount*100]}, USE.NAMES = FALSE) + sapply(x, function(x) { + tint(x, 100, 0)[amount * 100] + }, USE.NAMES = FALSE) } diff --git a/R/utils.R b/R/utils.R index ce65fc4..d3db3cf 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,23 +1,23 @@ -shade <- function(x, steps, crop){ +shade <- function(x, steps, crop) { shade <- rev(grDevices::colorRampPalette(c(x, "black"))(steps + 1)) - if(crop == 0){ + if (crop == 0) { return(rev(shade)) } rev(shade[-(seq_len(crop))]) } -tint <- function(x, steps, crop){ +tint <- function(x, steps, crop) { tint <- rev(grDevices::colorRampPalette(c(x, "white"))(steps + 1)) - if(crop == 0){ + if (crop == 0) { return(tint) } tint[-(seq_len(crop))] } -check_colour <- function(x){ +check_colour <- function(x) { chk_string(x) - res <- try(grDevices::col2rgb(x),silent = TRUE) - if(class(res) == "try-error") + res <- try(grDevices::col2rgb(x), silent = TRUE) + if (class(res) == "try-error") { stop(x, " is not a valid color", call. = FALSE) + } } - diff --git a/README.Rmd b/README.Rmd index 28fa93d..44f3ac6 100644 --- a/README.Rmd +++ b/README.Rmd @@ -40,23 +40,28 @@ hex <- "#335CAC" ``` ```{r colour, echo = FALSE, fig.width = 2, fig.height = 0.5} -tinter_plot <- function(x){ +tinter_plot <- function(x) { grid <- c(length(x), 1) - width <- 0.9/(max(grid) + 1) - gap <- 1/(max(grid) + 1) - centres <- lapply(grid, function(i) gap * ((max(grid) - - i)/2 + seq_len(i))) + width <- 0.9 / (max(grid) + 1) + gap <- 1 / (max(grid) + 1) + centres <- lapply(grid, function(i) { + gap * ((max(grid) - + i) / 2 + seq_len(i)) + }) centres <- as.matrix(expand.grid(centres)) oldPars <- graphics::par(mai = c(0, 0, 0, 0), bg = "white") on.exit(graphics::par(oldPars)) devSize <- grDevices::dev.size() - devRatio <- devSize[2]/devSize[1] - graphics::plot(NA, NA, xlim = c(-0.1, 1.1), ylim = 0.5 + c(-1, 1) * - devRatio * 0.6, xlab = "", ylab = "", xaxt = "n", yaxt = "n", - bty = "n", asp = 1) - graphics::rect(centres[, 1] - width/2, rev(centres[, 2]) - width/2, - centres[, 1] + width/2, rev(centres[, 2]) + width/2, - col = x, border = "white", lwd = 0.2) + devRatio <- devSize[2] / devSize[1] + graphics::plot(NA, NA, + xlim = c(-0.1, 1.1), ylim = 0.5 + c(-1, 1) * + devRatio * 0.6, xlab = "", ylab = "", xaxt = "n", yaxt = "n", + bty = "n", asp = 1 + ) + graphics::rect(centres[, 1] - width / 2, rev(centres[, 2]) - width / 2, + centres[, 1] + width / 2, rev(centres[, 2]) + width / 2, + col = x, border = "white", lwd = 0.2 + ) } tinter_plot(hex) diff --git a/build.R b/build.R index c623aee..75b782d 100644 --- a/build.R +++ b/build.R @@ -1,11 +1,15 @@ source("hex.R") + +roxygen2md::roxygen2md() + +styler::style_pkg(filetype = c("R", "Rmd")) + devtools::test() devtools::document() -knitr::knit("README.Rmd") -if(TRUE) { +# knitr::knit("README.Rmd") +if(FALSE) { if(file.exists("DESCRIPTION")) unlink("docs", recursive = TRUE) pkgdown::build_site() } -# need to resolve pkgdown path issue before submitting to cran for update - devtools::check() + diff --git a/tests/testthat/test-zzz-package.R b/tests/testthat/test-zzz-package.R index 05ff91b..799bcb3 100644 --- a/tests/testthat/test-zzz-package.R +++ b/tests/testthat/test-zzz-package.R @@ -20,5 +20,4 @@ test_that("package", { expect_length(darken(tinter("blue"), amount = 0.1), 9L) expect_length(lighten(tinter("blue"), amount = 0.1), 9L) - })