rgbaToHex function
Parses an RGBA color string to a valid hexadecimal color string.
Converts the RGBA color format string rgba
(e.g., 'rgba(255, 0, 0, 0.5)') to its hexadecimal representation.
Parameters:
rgba
: The RGBA color string to convert to hexadecimal format.
Returns: The converted color string in hexadecimal format.
Example:
print(rgbaToHex('rgba(255, 0, 0, 0.5)')); // Output: #ff000080
Implementation
String rgbaToHex(String rgba) {
rgba = rgba.replaceAll('rgba(', '').replaceAll(')', '');
List<String> rgbaValues = rgba.split(',');
int r = int.parse(rgbaValues[0].trim());
int g = int.parse(rgbaValues[1].trim());
int b = int.parse(rgbaValues[2].trim());
double a = double.parse(rgbaValues[3].trim());
int alpha = (a * 255).round();
return _toHex(r, g, b, alpha);
}