我觉得一种可能的解决方案是在颜色的 rgb 定义中添加 alpha 值(透明度)。您可以通过添加alpha 参数的rgb() 函数来实现。如果颜色通道 r、g 和 b 介于 0 到 255 之间,则必须使用 maxColorValue = 255 并在 0 到 255 之间表示 alpha,其中 0 对应于完全透明,255 对应于完全不透明(全彩)
让我展示一下你的案例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"
我会和其他人一起绘制你的颜色,以便更好地了解应用的透明度。
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)
另一个问题是,哪种完整的 RGB 颜色等同于具有透明度级别的给定颜色。 @capcoma 的回答在这里有所帮助。使用
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"
new_rgb 的表达式完全等同于@capcoma 的函数rgba2rgb()。我们可以用上面的其他 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)
第 1 列和第 4 列应具有相同的颜色。希望你觉得这很有用。
由reprex package (v0.3.0) 于 2020 年 12 月 3 日创建