parseDocumentColor function

Color? parseDocumentColor(
  1. Object? value
)

Reads a colour from a document property.

A document is JSON, so a colour arrives either as the 0xAARRGGBB integer the @DVPage annotation already uses, or as the #RRGGBB string a web editor's colour input produces. Both are accepted; anything else is ignored rather than guessed at, so a typo renders unstyled instead of black.

Implementation

Color? parseDocumentColor(Object? value) {
  if (value is int) return Color(value);
  if (value is! String) return null;
  var text = value.trim();
  if (text.startsWith('#')) text = text.substring(1);
  if (text.startsWith('0x') || text.startsWith('0X')) text = text.substring(2);
  if (text.length == 6) text = 'FF$text';
  if (text.length != 8) return null;
  final parsed = int.tryParse(text, radix: 16);
  return parsed == null ? null : Color(parsed);
}