What is the gray() function in R?

Overview

We obtain a vector of colors from a vector of gray levels using the gray() function in the R programming language.

Syntax

gray(level, alpha = NULL)

Parameter value

The gray() function takes the following parameter values:

  • level (required): This represents the vector which has the desired gray levels between 0 and 1. It’s worth noting that 0 indicates black while 1 indicates white.
  • alpha (optional): This is an optional value indicating the opacity, if specifed.

Return value

The gray() function returns a vector of the color having the same length as the input parameter level.

Example

# creating a level parameter
mylevel <- c( 0.1, 0.5, 1.0 )
# implementing the gray() function
gray(mylevel, alpha=TRUE)

Explanation

In the code above, we see the following:

  • Line 2: We create a vector object mylevel representing the level parameter.
  • Line 5: We apply the gray() function on the vector object and return the result to the console.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources