[英]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.