Is It a Valid RGB(A) Color?

Published by Matt in

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).
Watch a quick demo on how Edabit works.