Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy way to manipulate individual hcl axes? #20

Closed
hadley opened this issue Oct 31, 2020 · 6 comments
Closed

Easy way to manipulate individual hcl axes? #20

hadley opened this issue Oct 31, 2020 · 6 comments
Labels

Comments

@hadley
Copy link

hadley commented Oct 31, 2020

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)

@EmilHvitfeldt
Copy link
Owner

I was waiting for someone to suggest this, so now it is time :)

I like eval steps. Design-wise I think I'm going to split this function (and others) so we have modify_hue(), modify_chroma() etc etc. I would properly still keep modify_hcl() as shorthand, as I think it is important that the function names reflect what we are working with.

@hadley
Copy link
Author

hadley commented Nov 1, 2020

I think having individual functions would be a substantial decrease in usability.

@EmilHvitfeldt
Copy link
Owner

Could you expand on your reasoning?

Adding

  • modify_hue()
  • modify_chroma()
  • modify_ luminance()
  • modify_hcl()

vs

  • modify_hcl()

Is it because it would add many more functions with redundancy?

@hadley
Copy link
Author

hadley commented Nov 1, 2020

Right, this is a lot of typing:

x %>% 
  modify_hue(50) %>%
  modify_chroma(10)

Compared to:

x %>% modify_hcl(h = 50, c = 10)

I think it's reasonable to assume that people know what h, c, and l stand for in a colour manipulation package.

Also note that these two operations don't necessarily produce the same colour:

x %>% 
  modify_hue(50) %>%
  modify_chroma(10)

x %>% 
  modify_chroma(10) %>%
  modify_hue(50)

Really you want to do all the modifications in one transformation to hcl space, rather than going back and forth to and from rgb multiple times.

@EmilHvitfeldt
Copy link
Owner

I happened! It took a while for me to get around to it

@hadley
Copy link
Author

hadley commented Jan 3, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants