colorToHex function

String colorToHex(
  1. Color color, {
  2. bool includeHashSign = false,
  3. bool enableAlpha = true,
  4. bool toUpperCase = true,
})

Converts dart:ui Color to the 6/8 digits HEX String.

Prefixes a hash (#) sign if includeHashSign is set to true. The result will be provided as UPPER CASE, it can be changed via toUpperCase flag set to false (default is true). Hex can be returned without alpha channel information (transparency), with the enableAlpha flag set to false.

Implementation

String colorToHex(
  Color color, {
  bool includeHashSign = false,
  bool enableAlpha = true,
  bool toUpperCase = true,
}) {
  final String hex = (includeHashSign ? '#' : '') +
      (enableAlpha ? _padRadix(color.alpha) : '') +
      _padRadix(color.red) +
      _padRadix(color.green) +
      _padRadix(color.blue);
  return toUpperCase ? hex.toUpperCase() : hex;
}