rgbToHex function
Parses an RGB color string to a valid hexadecimal color string.
Converts the RGB color format string rgb
(e.g., 'rgb(255, 0, 0)') to its hexadecimal representation.
Parameters:
rgb
: The RGB color string to convert to hexadecimal format.
Returns: The converted color string in hexadecimal format.
Example:
print(rgbToHex('rgb(255, 0, 0)')); // Output: #ff0000
Implementation
String rgbToHex(String rgb) {
rgb = rgb.replaceAll('rgb(', '').replaceAll(')', '');
List<String> rgbValues = rgb.split(',');
int r = int.parse(rgbValues[0].trim());
int g = int.parse(rgbValues[1].trim());
int b = int.parse(rgbValues[2].trim());
return _toHex(r, g, b, 255);
}