diff --git a/lib/src/sky_engine/ui/painting.dart b/lib/src/sky_engine/ui/painting.dart index b647274..badff00 100644 --- a/lib/src/sky_engine/ui/painting.dart +++ b/lib/src/sky_engine/ui/painting.dart @@ -144,6 +144,67 @@ class $Color implements Color, $Instance { BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double))), 'colorSpace': BridgeFieldDef(BridgeTypeAnnotation($ColorSpace.$type)) }, + methods: { + 'toARGB32': BridgeMethodDef(BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.int)))), + 'withValues': BridgeMethodDef(BridgeFunctionDef( + returns: BridgeTypeAnnotation($Color.$type), + namedParams: [ + BridgeParameter('alpha', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true), + BridgeParameter('red', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true), + BridgeParameter('green', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true), + BridgeParameter('blue', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true), + BridgeParameter( + 'colorSpace', BridgeTypeAnnotation($ColorSpace.$type), true), + ], + )), + 'withAlpha': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation($Color.$type), + params: [ + BridgeParameter( + 'a', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), + false) + ]), + ), + 'withGreen': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation($Color.$type), + params: [ + BridgeParameter( + 'g', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), + false) + ]), + ), + 'withRed': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation($Color.$type), + params: [ + BridgeParameter( + 'r', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), + false) + ]), + ), + 'withBlue': BridgeMethodDef( + BridgeFunctionDef( + returns: BridgeTypeAnnotation($Color.$type), + params: [ + BridgeParameter( + 'b', + BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), + false) + ]), + ), + 'computeLuminance': BridgeMethodDef(BridgeFunctionDef( + returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)))), + }, wrap: true); /// Wrap a [Color] in an [$Color] @@ -158,6 +219,57 @@ class $Color implements Color, $Instance { @override $Value? $getProperty(Runtime runtime, String identifier) { + switch (identifier) { + case 'a': + return $double($value.a); + case 'r': + return $double($value.r); + case 'g': + return $double($value.g); + case 'b': + return $double($value.b); + case 'colorSpace': + return $ColorSpace.wrap($value.colorSpace); + case 'toARGB32': + return $Function((runtime, target, args) { + return $int((target!.$value as Color).toARGB32()); + }); + case 'withValues': + return $Function((runtime, target, args) { + final result = (target!.$value as Color).withValues( + alpha: args[0]?.$value, + red: args[1]?.$value, + green: args[2]?.$value, + blue: args[3]?.$value, + colorSpace: args[4]?.$value, + ); + return $Color.wrap(result); + }); + case 'withAlpha': + return $Function((runtime, target, args) { + final result = (target!.$value as Color).withAlpha(args[0]!.$value); + return $Color.wrap(result); + }); + case 'withRed': + return $Function((runtime, target, args) { + final result = (target!.$value as Color).withRed(args[0]!.$value); + return $Color.wrap(result); + }); + case 'withGreen': + return $Function((runtime, target, args) { + final result = (target!.$value as Color).withGreen(args[0]!.$value); + return $Color.wrap(result); + }); + case 'withBlue': + return $Function((runtime, target, args) { + final result = (target!.$value as Color).withBlue(args[0]!.$value); + return $Color.wrap(result); + }); + case 'computeLuminance': + return $Function((runtime, target, args) { + return $double((target!.$value as Color).computeLuminance()); + }); + } throw UnimplementedError(); } diff --git a/test/flutter_eval_test.dart b/test/flutter_eval_test.dart index 84da04b..58e0a57 100644 --- a/test/flutter_eval_test.dart +++ b/test/flutter_eval_test.dart @@ -204,7 +204,7 @@ void main() { BoxDecoration main() { return BoxDecoration( border: Border.all( - color: Colors.red, + color: Colors.red.withValues(alpha: 0.8), width: 2.0, ), ); @@ -218,8 +218,9 @@ void main() { expect(result, isNotNull); expect(result.$value, isA()); expect((result.$value as BoxDecoration).border, isA()); - expect( - (result.$value as BoxDecoration).border!.top.color, equals(Colors.red)); + // Comparing colors directly fails for some reason. + expect((result.$value as BoxDecoration).border!.top.color.toARGB32(), + equals(Colors.red.withValues(alpha: 0.8).toARGB32())); expect((result.$value as BoxDecoration).border!.top.width, equals(2.0)); });