Skip to content

Commit

Permalink
merge with joes changes
Browse files Browse the repository at this point in the history
Merge branch 'master' of github.com:poissonconsulting/tinter

# Conflicts:
#	DESCRIPTION
  • Loading branch information
sebdalgarno committed Apr 1, 2020
2 parents b90d146 + c0cb6f4 commit cb2dc4f
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 82 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
export(darken)
export(lighten)
export(tinter)
import(checkr)
import(chk)
2 changes: 1 addition & 1 deletion R/namespace.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#' @import checkr
#' @import chk
NULL
48 changes: 26 additions & 22 deletions R/tinter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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.
Expand All @@ -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)
}
17 changes: 9 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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)
}
}

55 changes: 30 additions & 25 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ output: github_document

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{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 <img src="https://raw.githubusercontent.com/poissonconsulting/tinter/master/docs/logo.png" align="right" width='15%'/>


Expand All @@ -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)
Expand Down Expand Up @@ -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")
```
Expand Down
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,39 @@ library(tinter)
hex <- "#335CAC"
```

![](README_files/figure-gfm/colour-1.png)<!-- -->
![](tools/README-colour-1.png)<!-- -->

``` r
tinter(hex)
#> [1] "#D6DEEE" "#ADBDDD" "#849DCD" "#5B7CBC" "#335CAC" "#284989" "#1E3767"
#> [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

Expand All @@ -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?

Expand Down Expand Up @@ -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

Expand Down
Binary file removed README_files/figure-gfm/plot-1.png
Binary file not shown.
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)

})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tools/README-plot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cb2dc4f

Please sign in to comment.