validateAndGetColor function

String? validateAndGetColor(
  1. String colorString
)

Validates and retrieves the color string in hexadecimal format.

If colorString starts with '#', it's assumed to be already in hexadecimal format. Otherwise, attempts to convert colorString to hexadecimal format using supported color formats. Throws ArgumentError if the color format is not supported.

Parameters:

  • colorString: The input color string to validate and convert.

Returns: The validated color string in hexadecimal format.

Example:

print(validateAndGetColor('#ff0000')); // Output: #ff0000
print(validateAndGetColor('rgb(255, 0, 0)')); // Output: #ff0000
print(validateAndGetColor('hsl(0, 100%, 50%)')); // Output: #ff0000

Implementation

String? validateAndGetColor(String colorString) {
  //verify if the color already is a hex
  if (colorString.startsWith('#')) return colorString;
  return colorToHex(colorString);
}