I feel one possible solution is to add the alpha value (transparency) in the rgb definition of the color. You can do it with the rgb()
function adding alpha
parameter. If the color channels r, g, and b are between 0 and 255, you have to use maxColorValue = 255
and express alpha
also between 0 and 255, where 0 corresponds to complete transparency and 255 to totally opaque (full color)
Let me show the results for your case rgba(240, 177, 76, 0.80)
:
your_color <- rgb(240, 177, 76, alpha = 0.8 * 255, maxColorValue = 255)
# equivalent to
# your_color <- rgb(t(c(240, 177, 76)/ 255), alpha = 0.8)
your_color
#> [1] "#F0B14CCC"
I'll plot your color with others to get a better sense of the transparency applied.
full_color <- rgb(240, 177, 76, alpha = 255, maxColorValue = 255)
full_color
#> [1] "#F0B14CFF"
# equivalent to "#F0B14C"
half_transparent <- rgb(t(c(240, 177, 76)/255), alpha = 0.5)
half_transparent
#> [1] "#F0B14C80"
old_par <- par(mar = c(1, 0, 0.5 ,0) + 0.1, mgp = c(1, 0, 0))
barplot(rep(1, 3), names.arg = c("yours", "full", "half"),
col = c(your_color, full_color, half_transparent), axes = FALSE, cex.names = 1)
par(old_par)
Another question is which full RGB color is equivalent to a given color with a level on transparency. The answer by @capcoma helps here. Using
background_color <- col2rgb("white")
background_color
#> [,1]
#> red 255
#> green 255
#> blue 255
your_color_rgb <- col2rgb(your_color, alpha = T)
your_color_rgb
#> [,1]
#> red 240
#> green 177
#> blue 76
#> alpha 204
alpha <- your_color_rgb[4, 1]/255
alpha
#> alpha
#> 0.8
new_rgb <- (1-alpha) * background_color + alpha * your_color_rgb[-4, 1, drop = FALSE]
new_rgb_equivalent <- rgb(t(new_rgb), maxColorValue = 255)
new_rgb_equivalent
#> [1] "#F3C06F"
the expression for new_rgb
is completely equivalent to the function rgba2rgb()
by @capcoma. We can plot the equivalent rgb color ("eq") with the other 3 above
old_par <- par(mar = c(1, 0, 0.5 ,0) + 0.1, mgp = c(1, 0, 0))
barplot(rep(1, 4), names.arg = c("yours", "full", "half", "eq"),
col = c(your_color, full_color, half_transparent,
new_rgb_equivalent), axes = FALSE, cex.names = 0.8)
par(old_par)
Column 1 and 4 should have the same color. Hope you find this useful.
Created on 2020-12-03 by the reprex package (v0.3.0)