Skip to content

Commit

Permalink
style files and do roxygen2markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley committed Mar 31, 2020
1 parent b5a76ed commit 6ee4aea
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 39 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
Language: en-US
Roxygen: list(markdown = TRUE)
30 changes: 16 additions & 14 deletions R/tinter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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.
Expand All @@ -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)
}
16 changes: 8 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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)
}
}

29 changes: 17 additions & 12 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions build.R
Original file line number Diff line number Diff line change
@@ -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()

1 change: 0 additions & 1 deletion tests/testthat/test-zzz-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})

0 comments on commit 6ee4aea

Please sign in to comment.