toColor property
Color
get
toColor
Convert a HEX value encoded (A)RGB string to a Dart Color.
- The string may include the '#' char, but does not have to.
- String may also include '0x' Dart Hex indicator, but does not have to.
- Any '#' '0x' patterns are trimmed out and String is assumed to be Hex.
- The String may start with alpha channel hex value, but does not have to, if alpha value is missing "FF" is used for alpha.
- String may be longer than 8 chars, after trimming out # and 0x, it will be RIGHT truncated to max 8 chars before parsing.
IF the resulting string cannot be parsed to a Color, is empty or null THEN fully opaque black color is returned ELSE the Color is returned.
Implementation
Color get toColor {
if (this == '') return const Color(0xFF000000);
String hexColor = replaceAll('#', '');
hexColor = hexColor.replaceAll('0x', '');
hexColor = hexColor.padLeft(6, '0');
hexColor = hexColor.padLeft(8, 'F');
final int length = hexColor.length;
return Color(int.tryParse('0x${hexColor.substring(length - 8, length)}') ??
0xFF000000);
}