3

I am looking for some in which can convert a color to Hex e.g. rgba(240, 177, 76, 0.80). There are plethora of functions available from to Hex conversion e.g. plotrix::rgb.to.hex() but not for RGBA.

Really appreciate any pointer.

massisenergy
  • 1,764
  • 3
  • 14
  • 25
Bogaso
  • 2,838
  • 3
  • 24
  • 54
  • Why not convert from RGBA to RGB (see answers [here](https://stackoverflow.com/questions/2049230/convert-rgba-color-to-rgb)) and then convert from RGB to HEX (using plotrix)? – DSH Apr 01 '20 at 19:46

2 Answers2

3

This topic has been discussed Convert RGBA to HEX however, I am not aware of an R package, that offers a ready to use function.

You can convert RGBA to RGB:

Since this depends on the background pixel's color (Convert RGBA color to RGB) you have to define the background color. color_RGBA is your RGBA color and background_RGB is the background color. You can take col2rgb("white") as background_RGB for example.

rgba2rgb <- function(background_RGB, color_RGBA){

  # get alpha
  alpha=color_RGBA[4]

  # get new color  
  new_col=matrix(c(
    (1 - alpha) * background_RGB[1] + alpha * color_RGBA[1],
    (1 - alpha) * background_RGB[2] + alpha * color_RGBA[2],
    (1 - alpha) * background_RGB[3] + alpha * color_RGBA[3]),
    nrow=3,ncol=1,dimnames=list(c("red","green","blue"))
  )
  return(new_col)
}

and then convert RGB to HEX:

rgb2hex <- function(x) rgb(x[1], x[2], x[3], maxColorValue = 255)
captcoma
  • 1,768
  • 13
  • 29
1

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.

  • The full color
full_color <- rgb(240, 177, 76, alpha = 255, maxColorValue = 255)
full_color
#> [1] "#F0B14CFF"
# equivalent to "#F0B14C"
  • A half transparent color
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)

josep maria porrà
  • 1,198
  • 10
  • 18