colorToHex function
Decides the color format type and converts it to hexadecimal format.
Detects the type of color from color
and calls the corresponding conversion function:
- If
color
starts with 'rgb(', converts it using rgbToHex. - If
color
starts with 'rgba(', converts it using rgbaToHex. - If
color
starts with 'hsl(', converts it using hslToHex. - If
color
starts with 'hsla(', converts it using hslaToHex. Throws ArgumentError if the color format is not supported.
Parameters:
color
: The input color string to convert to hexadecimal format.
Returns: The converted color string in hexadecimal format.
Example:
print(colorToHex('rgb(255, 0, 0)')); // Output: #ff0000
print(colorToHex('hsla(0, 100%, 50%, 0.5)')); // Output: #ff0000
Implementation
String? colorToHex(String color) {
// Detectar el tipo de color y llamar a la funciĆ³n correspondiente
if (color.startsWith('rgb(')) {
return rgbToHex(color);
} else if (color.startsWith('rgba(')) {
return rgbaToHex(color);
} else if (color.startsWith('hsl(')) {
return hslToHex(color);
} else if (color.startsWith('hsla(')) {
return hslaToHex(color);
} else {
return null;
}
}