parseHexColor function

ColorUint8 parseHexColor(
  1. String value
)

Parses a #RRGGBB or #RRGGBBAA (or unprefixed) hex string into a ColorUint8. Throws InvalidConfigException for malformed input.

Single source of truth for hex → color conversion across platform writers (iOS background, web background, Android adaptive background).

Implementation

ColorUint8 parseHexColor(String value) {
  final hex = value.startsWith('#') ? value.substring(1) : value;
  if (hex.length != 6 && hex.length != 8) {
    throw InvalidConfigException(
      'background color hex must be 6 or 8 digits (e.g. "FFFFFF"), got '
      '"$value"',
    );
  }
  final byte = int.parse(hex, radix: 16);
  if (hex.length == 8) {
    return ColorUint8.rgba(
      (byte >> 16) & 0xff,
      (byte >> 8) & 0xff,
      byte & 0xff,
      (byte >> 24) & 0xff,
    );
  }
  return ColorUint8.rgba(
    (byte >> 16) & 0xff,
    (byte >> 8) & 0xff,
    byte & 0xff,
    0xff,
  );
}