Name the CSS Function for Red-Green-Blue With Optional Alpha
StringsAuto-graded · 13 test casesLast updated
Recall which CSS color function takes red, green and blue channels plus an optional opacity value, and return its canonical name as a string.
A stylesheet author wants to describe a color by giving its red, green, and blue channel amounts directly. They also want the option to attach a transparency (alpha) value, but they do not want to be forced to supply one.
Write a function that returns the name of the CSS color function that fits this description under the current CSS Color Module Level 4 specification.
Task
The function takes no arguments. It must return the canonical name of the CSS color function that:
- takes red, green, and blue channels as its own named components, and
- accepts an alpha (opacity) component that may be left out.
Output format
Return a plain Python str that matches exactly:
- the function name in lowercase,
- followed immediately by an empty pair of parentheses,
- with no spaces, no arguments, and no other characters.
The comparison is an exact string match.
Notes
- Some other color functions also permit an optional alpha, but only one of them is defined in terms of red, green, and blue channels.
- A legacy alias exists for the older form of this function, in which transparency was a required fourth value. Your answer should be the modern function, not the alias.
Constraints
- The function takes no parameters.
- The return type is
str. - The returned string must be lowercase and include the trailing
(). - Do not return example calls such as a name with channel values filled in.
Examples
Example 1
Input: name_rgb_color_function()
Output: 'rgb()'
Hints
Hint 1
Think about which CSS color functions are built from red, green and blue channels rather than hue, lightness, or a named color space.
Hint 2
In older CSS there were two functions: one for opaque colors and one that required alpha. Modern CSS merged them so alpha is optional on a single function.
Hint 3
Format matters: return the lowercase name followed by empty parentheses, with nothing inside them and no extra whitespace.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a recall question, so the work is identifying the right function and returning it in the exact format.
The answer is rgb(). It is the only CSS color function defined by red, green, and blue channels, and in CSS Color Module Level 4 it accepts an optional alpha value:
color: rgb(255 99 71); /* opaque */
color: rgb(255 99 71 / 0.5); /* 50% opacity */
color: rgb(255, 99, 71, 0.5); /* legacy comma syntax, also valid */
Why it is correct
rgba()used to be the only way to include transparency, but it is now just a legacy alias ofrgb(). Because the problem stresses that alpha is optional, the modernrgb()is the answer.hsl()andhwb()use hue-based channels,lab(),lch(),oklab()andoklch()use perceptual color spaces, andcolor()requires you to name a color space explicitly. None of these are defined by red, green, and blue channels.- Hex notation such as
#RRGGBBAAis not a function.
Formatting pitfalls
The checker compares strings exactly, so return lowercase rgb() with the empty parentheses and no extra whitespace. Do not return rgb, RGB(), or rgba().
Complexity
- Time: O(1)
- Space: O(1)
Reference solution
def name_rgb_color_function() -> str:
"""
Return the CSS color function that takes red, green and blue channels
plus an optional alpha component.
"""
return "rgb()"