parseCssHexColor function
Parses a 3 or 6 digit CSS Hex Color into a dart:ui Color.
Implementation
Color parseCssHexColor(String input) {
// Remove any leading # (and the escaped version to be lenient)
input = input.replaceAll('#', '').replaceAll('%23', '');
// Handle 3/4-digit hex codes (eg. #123 == #112233)
if (input.length == 3 || input.length == 4) {
input = input.split('').map((c) => '$c$c').join();
}
// Pad alpha with FF.
if (input.length == 6) {
input = '${input}ff';
}
// In CSS, alpha is in the lowest bits, but for Flutter's value, it's in the
// highest bits, so move the alpha from the end to the start before parsing.
if (input.length == 8) {
input = '${input.substring(6)}${input.substring(0, 6)}';
}
final value = int.parse(input, radix: 16);
return Color(value);
}