parseQuillColor function
Parses a Quill color string ('#RRGGBB' or '#AARRGGBB', case
insensitive, # optional) into a Color.
Six-digit values are treated as fully opaque. Anything unparseable —
wrong length, non-hex digits, named colors, null — returns null
rather than throwing, so malformed documents degrade gracefully.
Implementation
Color? parseQuillColor(String? source) {
if (source == null) {
return null;
}
var hex = source.trim();
if (hex.startsWith('#')) {
hex = hex.substring(1);
}
if (hex.length != 6 && hex.length != 8) {
return null;
}
// int.tryParse(radix: 16) accepts a leading minus sign and 0x prefix;
// reject those explicitly so only bare hex digits pass.
if (!RegExp(r'^[0-9a-fA-F]+$').hasMatch(hex)) {
return null;
}
final value = int.parse(hex, radix: 16);
return Color(hex.length == 6 ? 0xFF000000 | value : value);
}