toHex method
Converts the color to a hexadecimal string representation.
@param hashSign If true (default), prepends a '#' sign (e.g., #FF00FF). @param withAlpha If true, includes the alpha channel in the string (e.g., #FF00FF00). @returns The hexadecimal string representation of the color.
Example:
Colors.blue.toHex(); // Returns "#2196F3" (omitting alpha)
Colors.red.withAlpha(128).toHex(withAlpha: true); // Returns "#80F44336"
Colors.black.toHex(hashSign: false); // Returns "000000" (no hash)
Implementation
String toHex({bool hashSign = true, bool withAlpha = false}) {
final alpha = (a * 255).toInt().toRadixString(16).padLeft(2, '0');
final red = (r * 255).toInt().toRadixString(16).padLeft(2, '0');
final green = (g * 255).toInt().toRadixString(16).padLeft(2, '0');
final blue = (b * 255).toInt().toRadixString(16).padLeft(2, '0');
return '${hashSign ? '#' : ''}'
'${withAlpha ? alpha : ''}'
'$red$green$blue'
.toUpperCase();
}