在 R 编程中将颜色转换为其 rgb 值 – col2rgb()函数
R 语言中的col2rgb()
函数用于将 R 颜色转换为 RGB(红/绿/蓝)。
Syntax: col2rgb(col)
Parameters:
col: vector of any of the three kinds of R color specifications, i.e, either a color name (listed by colors()), a hexadecimal string of the form “#rrggbb” or “#rrggbbaa”, or a positive integer i meaning palette()[i]
示例 1:
# R program to illustrate
# col2rgb function
# Calling the col2rgb() function
col2rgb("# 08a0ff")
col2rgb("peachpuff")
col2rgb(1:8)
col2rgb(paste0("gold", 1:4))
输出:
[, 1]
red 8
green 160
blue 255
[, 1]
red 255
green 218
blue 185
[, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7] [, 8]
red 0 255 0 0 0 255 255 190
green 0 0 205 0 255 0 255 190
blue 0 0 0 255 255 255 0 190
[, 1] [, 2] [, 3] [, 4]
red 255 238 205 139
green 215 201 173 117
blue 0 0 0 0
示例 2:
# R program to illustrate
# col2rgb function
# Calling the col2rgb() function
col2rgb(c(blu = "royalblue", reddish = "tomato"))
col2rgb(c(red = "red", hex = "# abcdef"))
col2rgb(c(palette = 1:3))
输出:
blu reddish
red 65 255
green 105 99
blue 225 71
red hex
red 255 171
green 0 205
blue 0 239
palette1 palette2 palette3
red 0 255 0
green 0 0 205
blue 0 0 0