color method

IrColor? color(
  1. DartValue? v
)

Implementation

IrColor? color(DartValue? v) {
  final unwrapped = unwrapStateProperty(v);
  if (unwrapped is RefValue &&
      unwrapped.inProject &&
      unwrapped.resolved != null) {
    final value = color(unwrapped.resolved);
    if (value == null || value.token != null) return value;
    final token = projectToken(unwrapped.path);
    projectColors[token] = value;
    return value.withToken(token);
  }
  if (unwrapped is RefValue) {
    switch (unwrapped.dotted) {
      case 'Colors.transparent':
        return IrColor.transparent;
      case 'Colors.white':
        return IrColor.white;
      case 'Colors.black':
        return IrColor.black;
    }
  }
  final d = deref(unwrapped);
  switch (d) {
    case LiteralValue(value: final int argb):
      return IrColor.fromArgb32(argb);
    case ObjectValue():
      return _colorObject(d);
    case AccessValue(:final target, :final name):
      if (name.startsWith('shade')) {
        final swatch = deref(target);
        final shade = int.tryParse(name.substring(5));
        if (swatch is ObjectValue && shade != null) {
          final map = deref(swatch.arg(1));
          if (map is MapValue) return color(map.lookup(shade));
        }
        return null;
      }
      final scheme = colorScheme(target);
      final argb = scheme?.roles[name];
      if (argb == null) return null;
      // Reading the app's own scheme: bind to the theme token.
      return _sameRoles(scheme!.roles, theme.colorScheme)
          ? theme.color(name)
          : IrColor.fromArgb32(argb);
    case CallValue(:final target, :final method):
      final base = color(target);
      if (base == null) return null;
      switch (method) {
        case 'withOpacity':
          final o = number(d.positional.firstOrNull);
          return o == null ? base : base.withAlpha(o);
        case 'withValues':
          final a = number(d.named['alpha']);
          return a == null ? base : base.withAlpha(a);
        case 'withAlpha':
          final a = number(d.positional.firstOrNull);
          return a == null ? base : base.withAlpha(a / 255);
      }
      return base;
    case ConditionalValue(:final then):
      return color(then);
    default:
      return null;
  }
}