Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
r

What is the col2rgb() function in R?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The col2rgb() function in R is simply converts any color to RGB (red/green/blue).

Syntax

col2rgb(col, alpha = FALSE)

Parameters

The col2rgb() function takes the following parameter values:

col: This is the vector of any of the three kinds of R color specifications:

  • The color name
  • The hexadecimal string of the "#rrggbb"" or "#rrggbbaa"form
  • The positive integer i meaning palette()[i]

alpha: This takes a Boolean value that indicates whether the opacity values of the result should be returned. This is optional.

Return value

The col2rgb() function returns an integer matrix with three or four rows (when alpha=TRUE) and the number of col length. When the col parameter takes the names, they are used as the column names of the return value.

Example

# Code explaining the col2rgb() function
# Using the color specification
col2rgb("pink")
col2rgb("red")
# Using the hexadecimal string
col2rgb("#08a0ff")
col2rgb("#abcdef")
# Using the integer specification
col2rgb(c(palette = 1:3))
# Using the alpha parameter to show the opacity values
col2rgb("blue", alpha = "TRUE")
Implementing the col2rgb() function

Explanation

  • Line 4 to 5: We obtain the RGB values of pink and red color name specifications using the col2rgb() function.
  • Line 9 to 10: We obtain the RGB values of #08a0ff and #abcdef hexadecimal string color specifications using the col2rgb() function.
  • Line 14: We obtain the RGB value of a positive integer color specification using the col2rgb() function.
  • Line 17: We obtain the RGB values of the color blue as, well as its opacity, using the col2rgb() function.

RELATED TAGS

communitycreator
r

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring