diff --git a/DESCRIPTION b/DESCRIPTION index b0a6d0e..d9a89d9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,7 +8,7 @@ Description: Generate a palette of tints, shades or both from a single colour. URL: https://github.com/poissonconsulting/tinter BugReports: https://github.com/poissonconsulting/tinter/issues Imports: - checkr, + chk, grDevices Suggests: graphics, @@ -17,5 +17,6 @@ Suggests: License: MIT + file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 7.0.2 +RoxygenNote: 7.1.0 Language: en-US +Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index c1d6355..06d81d3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,4 +3,4 @@ export(darken) export(lighten) export(tinter) -import(checkr) +import(chk) diff --git a/R/namespace.R b/R/namespace.R index cf772bd..33ffad1 100644 --- a/R/namespace.R +++ b/R/namespace.R @@ -1,2 +1,2 @@ -#' @import checkr +#' @import chk NULL diff --git a/R/tinter.R b/R/tinter.R index 54686f9..46af02d 100644 --- a/R/tinter.R +++ b/R/tinter.R @@ -12,35 +12,35 @@ #' 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) - check_length(x, length = 1L) - steps <- check_noneg_int(steps, coerce = TRUE) - crop <- check_noneg_int(crop, coerce = TRUE) - check_vector(direction, values = c("shades", "tints", "both")) - check_vector(adjust, c(-1, 1)) + chk_whole_number(steps) + chk_whole_number(crop) + chk_string(direction) + chk_subset(direction, c("shades", "tints", "both")) + 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,11 +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) - check_vector(amount, values = c(0, 1)) - sapply(x, function(x){shade(x, 100, 0)[amount*100]}, USE.NAMES = FALSE) + chk_number(amount) + chk_range(amount) + sapply(x, function(x) { + shade(x, 100, 0)[amount * 100] + }, USE.NAMES = FALSE) } #' Lighten colour. @@ -71,13 +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) - check_vector(amount, values = c(0, 1)) + 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 20806fb..d3db3cf 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,22 +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){ - res <- try(grDevices::col2rgb(x),silent = TRUE) - if(class(res) == "try-error") +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/README.Rmd b/README.Rmd index 1e23a62..44f3ac6 100644 --- a/README.Rmd +++ b/README.Rmd @@ -4,14 +4,16 @@ output: github_document -```{r setup, include = FALSE} +```{r, echo = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", - fig.width=5, fig.height=1 + fig.path = "tools/README-", + fig.width = 5, fig.height = 1 ) ``` + # tinter @@ -38,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) @@ -147,22 +154,20 @@ grDevices::colorRampPalette(colors = c("white", "blue", "black"))(11)[-(c(1:2, 1 ## Installation -To install from CRAN -``` +To install the latest release from [CRAN](https://cran.r-project.org) +```{r, eval=FALSE} install.packages("tinter") ``` -To install the latest development version from [GitHub](https://github.com/poissonconsulting/tinter) -``` -install.packages("devtools") -devtools::install_github("poissonconsulting/err") -devtools::install_github("poissonconsulting/checkr") -devtools::install_github("poissonconsulting/tinter") +To install the developmental version from [GitHub](https://github.com/poissonconsulting/tinter) +```{r, eval=FALSE} +# install.packages("remotes") +remotes::install_github("poissonconsulting/tinter") ``` -To install the latest development version from the Poisson drat [repository](https://github.com/poissonconsulting/drat) -``` -install.packages("drat") +To install the latest developmental release from the Poisson drat [repository](https://github.com/poissonconsulting/drat) +```{r, eval=FALSE} +# install.packages("drat") drat::addRepo("poissonconsulting") install.packages("tinter") ``` diff --git a/README.md b/README.md index d1f9bd2..36a7976 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ library(tinter) hex <- "#335CAC" ``` -![](README_files/figure-gfm/colour-1.png) +![](tools/README-colour-1.png) ``` r tinter(hex) @@ -42,31 +42,31 @@ tinter(hex) #> [8] "#142444" "#0A1222" ``` -![](README_files/figure-gfm/tinter-1.png) +![](tools/README-tinter-1.png) ``` r tinter(hex, direction = "tints") ``` -![](README_files/figure-gfm/tints-1.png) +![](tools/README-tints-1.png) ``` r tinter(hex, steps = 10) ``` -![](README_files/figure-gfm/steps-1.png) +![](tools/README-steps-1.png) ``` r tinter(hex, steps = 10, crop = 7) ``` -![](README_files/figure-gfm/crop-1.png) +![](tools/README-crop-1.png) ``` r tinter(hex, steps = 10, crop = 7, adjust = 0.4) ``` -![](README_files/figure-gfm/darken-1.png) +![](tools/README-darken-1.png) ### Create a choropleth map @@ -84,7 +84,7 @@ ggplot(data = nc) + coord_sf(datum = NA) ``` -![](README_files/figure-gfm/plot-1.png) +![](tools/README-plot-1.png) ### Doesn’t this already exist? @@ -125,24 +125,28 @@ grDevices::colorRampPalette(colors = c("white", "blue", "black"))(11)[-(c(1:2, 1 ## Installation -To install from CRAN +To install the latest release from [CRAN](https://cran.r-project.org) - install.packages("tinter") +``` r +install.packages("tinter") +``` -To install the latest development version from +To install the developmental version from [GitHub](https://github.com/poissonconsulting/tinter) - install.packages("devtools") - devtools::install_github("poissonconsulting/err") - devtools::install_github("poissonconsulting/checkr") - devtools::install_github("poissonconsulting/tinter") +``` r +# install.packages("remotes") +remotes::install_github("poissonconsulting/tinter") +``` -To install the latest development version from the Poisson drat +To install the latest developmental release from the Poisson drat [repository](https://github.com/poissonconsulting/drat) - install.packages("drat") - drat::addRepo("poissonconsulting") - install.packages("tinter") +``` r +# install.packages("drat") +drat::addRepo("poissonconsulting") +install.packages("tinter") +``` ## Contribution diff --git a/README_files/figure-gfm/plot-1.png b/README_files/figure-gfm/plot-1.png deleted file mode 100644 index 1e45e59..0000000 Binary files a/README_files/figure-gfm/plot-1.png and /dev/null differ 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) - }) diff --git a/README_files/figure-gfm/colour-1.png b/tools/README-colour-1.png similarity index 72% rename from README_files/figure-gfm/colour-1.png rename to tools/README-colour-1.png index cf05fb0..a46cabc 100644 Binary files a/README_files/figure-gfm/colour-1.png and b/tools/README-colour-1.png differ diff --git a/README_files/figure-gfm/crop-1.png b/tools/README-crop-1.png similarity index 91% rename from README_files/figure-gfm/crop-1.png rename to tools/README-crop-1.png index 73bd668..0c96a8c 100644 Binary files a/README_files/figure-gfm/crop-1.png and b/tools/README-crop-1.png differ diff --git a/README_files/figure-gfm/darken-1.png b/tools/README-darken-1.png similarity index 92% rename from README_files/figure-gfm/darken-1.png rename to tools/README-darken-1.png index cf39edc..2e3dc9f 100644 Binary files a/README_files/figure-gfm/darken-1.png and b/tools/README-darken-1.png differ diff --git a/tools/README-plot-1.png b/tools/README-plot-1.png new file mode 100644 index 0000000..0e90a61 Binary files /dev/null and b/tools/README-plot-1.png differ diff --git a/README_files/figure-gfm/steps-1.png b/tools/README-steps-1.png similarity index 97% rename from README_files/figure-gfm/steps-1.png rename to tools/README-steps-1.png index 4b8ceb1..3bbb89b 100644 Binary files a/README_files/figure-gfm/steps-1.png and b/tools/README-steps-1.png differ diff --git a/README_files/figure-gfm/tinter-1.png b/tools/README-tinter-1.png similarity index 94% rename from README_files/figure-gfm/tinter-1.png rename to tools/README-tinter-1.png index 7b44656..16e0806 100644 Binary files a/README_files/figure-gfm/tinter-1.png and b/tools/README-tinter-1.png differ diff --git a/README_files/figure-gfm/tints-1.png b/tools/README-tints-1.png similarity index 94% rename from README_files/figure-gfm/tints-1.png rename to tools/README-tints-1.png index 9b5068a..93feb55 100644 Binary files a/README_files/figure-gfm/tints-1.png and b/tools/README-tints-1.png differ