To make it easier to test colors in a complex plot, I'd like to use named colors but specify alpha. My first thought was to use col2rgb()
. Something like this
rgb( col2rgb( "red" ), alpha=125, maxColorValue=255 )
But the output of col2rgb() is a matrix. I've tried transposing it, converting it to character, etc., but can't get it in a form that rgb()
can use. Which is strange, since it's called col2rgb
.
The situation is more complex than this, and I have to specify the alpha when giving the color.
We can t
ranspose to create a 3 column matrix
as the col2rgb
is a single column matrix
with row.names ie
col2rgb( "red" )
# [,1]
#red 255
#green 0
#blue 0
t(col2rgb( "red" ))
# red green blue
#[1,] 255 0 0
and according to ?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
rgb(t(col2rgb( "red" )), alpha=125, maxColorValue=255 )
#[1] "#FF00007D"
Or as a 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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.