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

hex_to_rgb() returns string not matrix #4

Open
padpadpadpad opened this issue Mar 14, 2024 · 2 comments
Open

hex_to_rgb() returns string not matrix #4

padpadpadpad opened this issue Mar 14, 2024 · 2 comments

Comments

@padpadpadpad
Copy link

Hi!

Super cool package. I used it to extract hex codes for transparent values of a whole palette to use as different layers of a ggplot2, as the colours represent different levels of data (see below).

To do this I passed a colour palette in hex codes to hex_to_rgb() then used rgba_to_hex() with an alpha value to get the new colour palette. However hex_to_rgb() currently returns the string "r = X, g = Y, b = Z", when a vector of the three colours would be more useful maybe?

My code and function are here:

# set colours for plot
colours <- RColorBrewer::brewer.pal(10, 'Spectral')

# function to make points lighter but not transparent
alpha_hex <- function(hex_code, alpha = 1){
  # split by ',' and extract number
  temp <- monochromeR::hex_to_rgb(hex_code) %>%
    str_split(., ',') %>%
    .[[1]] %>%
    parse_number
  
  return(rgba_to_hex(c(temp, alpha)))
}

purrr::map_vec(colours, alpha_hex, alpha = 0.7)

pcoa_all_panel

@cararthompson
Copy link
Owner

Hi @padpadpadpad Thank you for sharing this. I really like what you did with the colours! I'm actually in the process of collaborating with @xx02al to get the code up to speed with new developments, so happy to look into this for the next version of the package!

@xx02al
Copy link
Collaborator

xx02al commented Nov 4, 2024

Hi @padpadpadpad. I recommend using the farver package for modifying colour space channels in hex-encoded colour strings. Here's an example considering what you attempted to do. Sidenote: since I am not able to reproduce your example, you might check whether you need to set value 0.3 or 0.7 in the example you provided.

colours <- c("#200630", "#601390", "#A020F0", "#C679F6", "#ECD2FC")
farver::set_channel(colours, "alpha", 0.3)

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

3 participants