← Back to challenges

Is It a Valid RGB(A) Color?

PythonHardregexconditionsvalidation

Instructions

Given an RGB(A) CSS color, determine whether its format is valid or not. Create a function that takes a string (e.g. "rgb(0, 0, 0)") and return True if it's format is correct, otherwise return False.

Examples

valid_color("rgb(0,0,0)") ➞ True

valid_color("rgb(0,,0)") ➞ False

valid_color("rgb(255,256,255)") ➞ False

valid_color("rgba(0,0,0,0.123456789)") ➞ True

Notes

  • Alpha is between 0 and 1.
  • There are a few edge cases (check out the Tests tab to know more).
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.