Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions lib/src/sky_engine/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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();
}

Expand Down
7 changes: 4 additions & 3 deletions test/flutter_eval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
Expand All @@ -218,8 +218,9 @@ void main() {
expect(result, isNotNull);
expect(result.$value, isA<BoxDecoration>());
expect((result.$value as BoxDecoration).border, isA<Border>());
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));
});

Expand Down