Skip to content
Related Articles

Related Articles

Improve Article

Convert a Color to its rgb value in R Programming – col2rgb() Function

  • Last Updated : 30 Jun, 2020

col2rgb() function in R Language is used to convert R color to RGB (red/green/blue).

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]

Example 1:




# R program to illustrate
# col2rgb function
  
# Calling the col2rgb() function
col2rgb("# 08a0ff")
col2rgb("peachpuff")
col2rgb(1:8)
col2rgb(paste0("gold", 1:4))

Output:

      [, 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

Example 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))

Output:

      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
My Personal Notes arrow_drop_up
Recommended Articles
Page :