简体   繁体   English

在 rgb() 中使用命名的 r 颜色,以便我可以添加 alpha 值

[英]Use named r color in rgb() so I can add alpha value

To make it easier to test colors in a complex plot, I'd like to use named colors but specify alpha.为了更轻松地测试复杂图中的颜色,我想使用命名颜色但指定 alpha。 My first thought was to use col2rgb() .我的第一个想法是使用col2rgb() Something like this像这样的东西

rgb( col2rgb( "red" ), alpha=125, maxColorValue=255 )

But the output of col2rgb() is a matrix.但是 col2rgb() 的输出是一个矩阵。 I've tried transposing it, converting it to character, etc., but can't get it in a form that rgb() can use.我试过转置它,将其转换为字符等,但无法以rgb()可以使用的形式获取它。 Which is strange, since it's called col2rgb .这很奇怪,因为它被称为col2rgb

The situation is more complex than this, and I have to specify the alpha when giving the color.情况比这更复杂,我在给颜色时必须指定alpha。

We can t ranspose to create a 3 column matrix as the col2rgb is a single column matrix with row.names ie我们可以t ranspose创建3列matrix作为col2rgb是单个列matrix与row.names即

col2rgb( "red" )
#       [,1]
#red    255
#green    0
#blue     0
t(col2rgb( "red" ))
#     red green blue
#[1,] 255     0    0

and according to ?rgb并根据?rgb

The colors may be specified by passing a matrix or data frame as argument red, and leaving blue and green missing.可以通过将矩阵或数据框作为参数传递给红色来指定颜色,而忽略蓝色和绿色。 In this case the first three columns of red are taken to be the red, green and blue values.在这种情况下,红色的前三列被视为红色、绿色和蓝色值。

So, we can use a 3 column matrix因此,我们可以使用 3 列matrix

rgb(t(col2rgb( "red" )), alpha=125, maxColorValue=255 )
#[1] "#FF00007D"

Or as a data.frame或者作为data.frame

rgb(as.data.frame(t(col2rgb( "red" ))), alpha=125, maxColorValue=255 )
#[1] "#FF00007D"

which is the same as这与

rgb(red = 255, green = 0, blue = 0, alpha=125, maxColorValue=255 )
#[1] "#FF00007D"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道 R 中某个命名颜色的 rgb 颜色代码? - How to know the rgb colour code for a certain named color in R? Function 采用十六进制颜色并返回带有 RGB 和可能的 alpha 通道值的命名向量 - Function that takes a hex-color and returns a named vector with the values of the RGB and possibly alpha channels 如何在R中获取十六进制颜色的alpha值 - How to get alpha value of a hexadecimal color in R 我可以在 R 中使用列表作为哈希吗? 如果是这样,为什么这么慢? - Can I use a list as a hash in R? If so, why is it so slow? R:如何使用命名参数来匹配和编辑命名对象列表的名称 - R: How can I use named arguments to match & edit the names of a list of named objects R:将alpha值添加到png-image - R: add alpha-value to png-image 忽略 NA,以便我可以为负数添加价值 - Ignore NAs so that I can add value to negative numbers 我可以使用 R 来突出段落中的一些文字背景颜色吗? - Can I use R to highlight some words background color in a paragraph? 如何在一个图例中组合alpha和颜色 - How can I combine alpha and color in one legend 如何在R中的ggplot2中的barplot中添加空间,以便可以读取标签? - How can I add space in barplot from ggplot2 in R so that I can read the labels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM