[英]R function for RGBA to HEX color conversion
I am looking for some function in r which can convert a rgba color to Hex eg rgba(240, 177, 76, 0.80)
.我正在r 中寻找一些可以将rgba颜色转换为十六进制的函数,例如
rgba(240, 177, 76, 0.80)
。 There are plethora of functions available from rgb to Hex conversion eg plotrix::rgb.to.hex()
but not for RGBA.从rgb到十六进制转换有很多可用的函数,例如
plotrix::rgb.to.hex()
但不适用于 RGBA。
Really appreciate any pointer.真的很感激任何指针。
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.这个主题已经讨论过将 RGBA 转换为十六进制,但是,我不知道有一个 R 包,它提供了一个随时可用的功能。
You can convert RGBA to RGB:您可以将 RGBA 转换为 RGB:
Since this depends on the background pixel's color ( Convert RGBA color to RGB ) you have to define the background color.由于这取决于背景像素的颜色( 将 RGBA 颜色转换为 RGB ),因此您必须定义背景颜色。
color_RGBA
is your RGBA color and background_RGB
is the background color. color_RGBA
是您的 RGBA 颜色, background_RGB
是背景颜色。 You can take col2rgb("white")
as background_RGB
for example.例如,您可以将
col2rgb("white")
作为background_RGB
。
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:然后将RGB转换为HEX:
rgb2hex <- function(x) rgb(x[1], x[2], x[3], maxColorValue = 255)
I feel one possible solution is to add the alpha value (transparency) in the rgb definition of the color.我觉得一种可能的解决方案是在颜色的 rgb 定义中添加 alpha 值(透明度)。 You can do it with the
rgb()
function adding alpha
parameter.您可以使用
rgb()
函数添加alpha
参数来实现。 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)如果颜色通道 r、g 和 b 介于 0 和 255 之间,则必须使用
maxColorValue = 255
并在 0 和 255 之间表达alpha
,其中 0 对应于完全透明,255 对应于完全不透明(全色)
Let me show the results for your case rgba(240, 177, 76, 0.80)
:让我展示一下您的案例
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.另一个问题是,哪种全 RGB 颜色相当于具有透明度级别的给定颜色。 The answer by @capcoma helps here.
@capcoma 的回答在这里有所帮助。 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. new_rgb
的表达式完全等同于rgba2rgb()
的函数rgba2rgb()
。 We can plot the equivalent rgb color ("eq") with the other 3 above我们可以用上面的其他 3 个绘制等效的 rgb 颜色(“eq”)
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.第 1 列和第 4 列应具有相同的颜色。 Hope you find this useful.
希望您觉得这个有帮助。
Created on 2020-12-03 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 12 月 3 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.