cararthompson / monochromeR

A package for creating monochrome colour palettes and easily converting rgba values to hex codes
Other
76 stars 1 forks source link

hex_to_rgb() returns string not matrix #4

Open padpadpadpad opened 8 months ago

padpadpadpad commented 8 months ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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)