Giter Club home page Giter Club logo

prismatic's Introduction

prismatic

R-CMD-check Codecov test coverage CRAN status Downloads Lifecycle: stable DOI

The goal of prismatic is to provide color manipulation tools in R, in a intuitive, low-dependency and functional way.

  • intuitive All the working functions are prefixed with clr_ (color) allowing for easy autocompletion.
  • low-dependency Only depends on farver.
  • functional All functions have consistent inputs and outputs and are thus fully pipeable.

Installation

You can install the released version of prismatic from CRAN with:

install.packages("prismatic")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("EmilHvitfeldt/prismatic")

Examples

All prismatic functions returns a colors object, which includes a new printing method and plotting method for quickly visualizing the colors.

library(prismatic)
library(magrittr)

terrain10 <- terrain.colors(10)

terrain10
#>  [1] "#00A600" "#2DB600" "#63C600" "#A0D600" "#E6E600" "#E8C32E" "#EBB25E"
#>  [8] "#EDB48E" "#F0C9C0" "#F2F2F2"

terrain_color <- color(terrain10)

terrain_color
#> <colors>
#> #00A600FF #2DB600FF #63C600FF #A0D600FF #E6E600FF #E8C32EFF #EBB25EFF #EDB48EFF #F0C9C0FF #F2F2F2FF

plot(terrain_color)

If cli is available the print method will do its best to represent the colors.

library(prismatic)
library(magrittr)
ddd <- color(terrain.colors(10))

ddd
#> <colors>
#> #00A600FF #2DB600FF #63C600FF #A0D600FF #E6E600FF #E8C32EFF #EBB25EFF #EDB48EFF #F0C9C0FF #F2F2F2FF

plot(ddd)

clr_grayscale(ddd) %>% plot()

clr_lighten(ddd, 0.7) %>% plot()

clr_darken(ddd, 0.5) %>% plot()

clr_saturate(ddd, 0.5) %>% plot()

clr_desaturate(ddd, 0.5) %>% plot()

clr_negate(ddd) %>% plot()

clr_rotate(ddd, 180) %>% plot()

clr_protan(ddd) %>% plot()

clr_tritan(ddd) %>% plot()

clr_deutan(ddd) %>% plot()

Related work

This package is hugely inspired by the JavaScript library Qix-/color.

Code of Conduct

Please note that the β€˜prismatic’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

prismatic's People

Contributors

emilhvitfeldt avatar gadenbuie avatar jack-davison avatar kant avatar olivroy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

prismatic's Issues

Release prismatic 1.1.1

Prepare for release:

  • git pull
  • Check current CRAN check results
  • Polish NEWS
  • devtools::build_readme()
  • urlchecker::url_check()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • revdepcheck::revdep_check(num_workers = 4)
  • Update cran-comments.md
  • git push

Submit to CRAN:

  • usethis::use_version('patch')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted πŸŽ‰
  • git push
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • git push

Use hcl instead of hsl?

I noticed that rotate() uses hsl rather than hcl, but my recollection is that hcl is more perceptually uniform, so I think it would be better to use hcl.

I was also surprised see that "HSL" is the default in clr_lighten() β€” I re-read the colorspace paper to check that I wasn't missing something, but I see:

HLS() (Hue-Lightness-Saturation) is another transformation of (s)RGB that tries to capture the perceptual axes. It does a somewhat better job but the dimensions are still strongly confounded. See Wikipedia (2019f) for more details.

Make `clr_` functions work within mutate?

hi friend! πŸ‘‹ thanks for this package, it's very very useful!

right now it doesn't look like clr_lighten() and clr_darken() (maybe all of them? I'm only looking at the lighten/darken functions though) work well within a mutate, seemingly due to mismatch in length of the colour/shift arguments. curious if this is something you'd be open to supporting, or if I'm missing some way to do it! ty!

library(prismatic)
library(dplyr)

x <- c("#fbbcb8", "#bfdff6")
clr_darken(x)
#> <colors>
#> #CE170CFF #1B7BC0FF

# sad in mutate() though
colours <- tibble(color = x) %>%
  mutate(darker = clr_darken(color))
#> Error: Problem with `mutate()` input `darker`.
#> x The length of `col` must be positive.
#> β„Ή Input `darker` is `clr_darken(color)`.

# rowwise() doesn't help much?
colours <- tibble(color = x) %>%
  rowwise() %>%
  mutate(darker = clr_darken(color))
#> Error: Problem with `mutate()` input `darker`.
#> x The length of `col` must be positive.
#> β„Ή Input `darker` is `clr_darken(color)`.
#> β„Ή The error occured in row 2.

Release prismatic 0.1.0

Prepare for release:

  • Check that description is informative
  • Check licensing of included files
  • usethis::use_cran_comments()
  • devtools::check()
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • Update cran-comments.md
  • Review pkgdown reference index for, e.g., missing topics
  • Draft blog post

Submit to CRAN:

  • usethis::use_version('minor')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted πŸŽ‰
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • usethis::use_news_md()
  • Update install instructions in README
  • Finish blog post
  • Tweet
  • Add link to blog post in pkgdown news menu

Provide contrast ratio function

like colorspace::contrast_ratio("#dc3545", "#f8f9fa")

Not exactly related to the core purpose of the package, but when generating colours it is very useful to be able to check their accessibility.

Upkeep for prismatic

Pre-history

  • usethis::use_readme_rmd()
  • usethis::use_roxygen_md()
  • usethis::use_github_links()
  • usethis::use_pkgdown_github_pages()
  • usethis::use_tidy_labels()
  • usethis::use_tidy_style()
  • usethis::use_tidy_description()
  • urlchecker::url_check()

2020

  • usethis::use_package_doc()
    Consider letting usethis manage your @importFrom directives here.
    usethis::use_import_from() is handy for this.
  • usethis::use_testthat(3) and upgrade to 3e, testthat 3e vignette
  • Align the names of R/ files and test/ files for workflow happiness.
    usethis::rename_files() can be helpful.

2021

  • usethis::use_tidy_dependencies()
  • usethis::use_tidy_github_actions() and update artisanal actions to use setup-r-dependencies
  • Remove check environments section from cran-comments.md
  • Bump required R version in DESCRIPTION to 3.4
  • Use lifecycle instead of artisanal deprecation messages, as described in Communicate lifecycle changes in your functions

Release prismatic 0.2.0

Prepare for release:

  • Check current CRAN check results
  • devtools::check()
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • revdepcheck::revdep_check(num_workers = 4)
  • Update cran-comments.md
  • Polish NEWS
  • Review pkgdown reference index for, e.g., missing topics
  • Draft blog post

Submit to CRAN:

  • usethis::use_version('minor')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted πŸŽ‰
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • Finish blog post
  • Tweet
  • Add link to blog post in pkgdown news menu

Release prismatic 1.1.0

Prepare for release:

  • Check current CRAN check results
  • Polish NEWS
  • devtools::build_readme()
  • urlchecker::url_check()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • revdepcheck::revdep_check(num_workers = 4)
  • Update cran-comments.md
  • Review pkgdown reference index for, e.g., missing topics
  • Draft blog post

Submit to CRAN:

  • usethis::use_version('minor')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted πŸŽ‰
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • Finish blog post
  • Tweet
  • Add link to blog post in pkgdown news menu

Release prismatic 1.1.2

Prepare for release:

  • git pull
  • Check current CRAN check results
  • Polish NEWS
  • urlchecker::url_check()
  • devtools::build_readme()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • revdepcheck::revdep_check(num_workers = 4)
  • Update cran-comments.md
  • git push

Submit to CRAN:

  • usethis::use_version('patch')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted πŸŽ‰
  • usethis::use_github_release()
  • usethis::use_dev_version(push = TRUE)

Use crayon for printing if available

One thing that could be super neat would be to use crayon for colouring the background during printing. You could simply use it if it was available so you'd only need to have it in suggests

Release prismatic 1.0.0

Prepare for release:

  • devtools::build_readme()
  • Check current CRAN check results
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • revdepcheck::revdep_check(num_workers = 4)
  • urlchecker::url_check()
  • Update cran-comments.md
  • Polish NEWS
  • Review pkgdown reference index for, e.g., missing topics
  • Draft blog post

Submit to CRAN:

  • usethis::use_version('major')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted πŸŽ‰
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • Finish blog post
  • Tweet
  • Add link to blog post in pkgdown news menu

Include text contrast function?

It's not a perfect match for this package, but it seems like a reasonable home:

rel_l <- function(x) {
  scale <- function(x) {
    ifelse(x <= 0.03928, x / 12.92, ((x + 0.055) / 1.055)^2.4)
  }
  rgb <- farver::decode_colour(x) / 255
  0.2126 * scale(rgb[, 1]) + 0.7152 * scale(rgb[, 2]) + 0.0722 * scale(rgb[, 3])
}
contrast_ratio <- function(x, y) {
  x_l <- rel_l(x)
  y_l <- rel_l(y)

  (pmax(x_l, y_l) + 0.05) / (pmin(x_l, y_l) + 0.05)
}

This implements the WCAG 2.0 colour contrast computation (https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html) which is useful for checking that colours have sufficient contrast to be easily readable.

Easy way to manipulate individual hcl axes?

Something like this maybe:

modify_hcl <- function(x, h, c, l) {
  hcl <- as.data.frame(farver::decode_colour(x, to = "hcl"))

  if (!missing(h)) {
    h <- eval(substitute(h), envir = hcl)
  } else {
    h <- hcl$h
  }
  if (!missing(c)) {
    c <- eval(substitute(c), envir = hcl)
  } else {
    c <- hcl$c
  }
  if (!missing(l)) {
    l <- eval(substitute(l), envir = hcl)
  } else {
    l <- hcl$l
  }
  
  farver::encode_colour(cbind(h, c, l), from = "hcl")
}

Idea is to make it easy to do (e.g.) modify_hcl(x, h = 25) or modify_hcl(x, h = h + 10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.