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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.