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

Apply lightness, saturation etc. to functions #5

Closed
hughjonesd opened this issue Oct 27, 2018 · 11 comments
Closed

Apply lightness, saturation etc. to functions #5

hughjonesd opened this issue Oct 27, 2018 · 11 comments

Comments

@hughjonesd
Copy link
Contributor

Great package.

It would be nice to be able to write e.g.

my_pal <- scales::colour_ramp(c("red", "blue"))
# my_pal is a function that takes numbers and returns colours 
my_pal <- lightness(my_pal, 100)
my_pal(6) # return the lightened colours.

In other words to allow calling lightness, saturation and friends on functions.

One way to do this is as follows:

lightness <- function (shades, values = NULL) UseMethod("lightness")

# original lightness becomes lightness.default

lightness.function <- function(shades, values = NULL) {
  orig_call <- match.call(expand.dots = FALSE)
  orig_call[[1]] <- quote(lightness)
  shades_q <- substitute(shades)
  wrapper <- function (...) {
    inner_call <- match.call(expand.dots = FALSE)
    inner_call[[1]] <- shades_q
    result <- eval(inner_call)
    orig_call$shades <- result
    eval(orig_call)
  }
  formals(wrapper) <- formals(shades)
  
  return(wrapper)
}

I've got a proof of concept if you think this is a cute idea.

An obvious extension is to e.g. ggplot colour scales.

@jonclayden
Copy link
Owner

Hi David, and thanks for the suggestion. I think the functions returned from scales::colour_ramp expect arguments in the [0,1] range, so my_pal(6) doesn't produce anything useful. But, aside from that, if we have

my_pal <- scales::colour_ramp(c("red", "blue"))
my_pal2 <- lightness.function(my_pal, 100)

(using your lightness.function), then my_pal2(seq(0,1,0.25)) is the same as lightness(my_pal(seq(0,1,0.25)), 100) or my_pal(seq(0,1,0.25)) %>% lightness(100).

Is there a good reason to prefer the two-step higher-order approach? The practical benefit of this isn't obvious to me, so a usage example would be very helpful.

@hughjonesd
Copy link
Contributor Author

A good use case would be tinkering with plots. For example, if you use scale_colour_viridis(), then you may not have direct access to the colours it uses. Similarly, you may want to change a colourRamp and have it affect a number of plots, where it is spitting out different colours each time.

I often work from the command line, adding bits to my plot commands. It would be nice to just do

ggplot(data, aes(foo, bar)) + lightness(scale_colour_whatever(), 50) 

rather than having to hunt down the specific colours and use scale_colour_manual.

@hughjonesd
Copy link
Contributor Author

So, I just came across this very example. Here's a plot I'm working on, and I am thinking, great colours, I wish they were a little darker. This comes from scale_color_brewer(type = "qual").

rplot

@jonclayden
Copy link
Owner

Thanks – I see the need. I'll look into getting this running when I have a bit of time.

@jonclayden
Copy link
Owner

PR #7 is now merged, but I will keep this issue open until a full solution is committed.

@hughjonesd
Copy link
Contributor Author

Drop me a line when it’s up on CRAN, and I’ll blog about it. The package really fills a gap.

@jonclayden
Copy link
Owner

OK, the package is now updated so that all the property functions, including opacity, plus complement, addmix and submix, should accept palette functions or scales as their first arguments. As a bonus, gradient() will now return a function if its second argument is missing or NULL, which can then be manipulated further with the other functions.

I think this should handle your requirements, but if you'd be willing to give it a quick test that would be very helpful.

@hughjonesd
Copy link
Contributor Author

hughjonesd commented Nov 19, 2018 via email

@hughjonesd
Copy link
Contributor Author

This seems to work great. I'll tell you if anything comes up.

@jonclayden
Copy link
Owner

Great! It's ready for CRAN so I'll get it sent off. If you run into any issues I can always do a point release.

@jonclayden
Copy link
Owner

The update is now released as version 1.3.0, and I've added an example of darkening existing colours (as above) to the README.

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

No branches or pull requests

2 participants