Skip to content

Commit efa1022

Browse files
authored
Merge pull request #131 from Zverik/color_methods
Add Color methods
2 parents f1b9f94 + 4e65afc commit efa1022

2 files changed

Lines changed: 116 additions & 3 deletions

File tree

lib/src/sky_engine/ui/painting.dart

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,67 @@ class $Color implements Color, $Instance {
144144
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double))),
145145
'colorSpace': BridgeFieldDef(BridgeTypeAnnotation($ColorSpace.$type))
146146
},
147+
methods: {
148+
'toARGB32': BridgeMethodDef(BridgeFunctionDef(
149+
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.int)))),
150+
'withValues': BridgeMethodDef(BridgeFunctionDef(
151+
returns: BridgeTypeAnnotation($Color.$type),
152+
namedParams: [
153+
BridgeParameter('alpha',
154+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true),
155+
BridgeParameter('red',
156+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true),
157+
BridgeParameter('green',
158+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true),
159+
BridgeParameter('blue',
160+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)), true),
161+
BridgeParameter(
162+
'colorSpace', BridgeTypeAnnotation($ColorSpace.$type), true),
163+
],
164+
)),
165+
'withAlpha': BridgeMethodDef(
166+
BridgeFunctionDef(
167+
returns: BridgeTypeAnnotation($Color.$type),
168+
params: [
169+
BridgeParameter(
170+
'a',
171+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)),
172+
false)
173+
]),
174+
),
175+
'withGreen': BridgeMethodDef(
176+
BridgeFunctionDef(
177+
returns: BridgeTypeAnnotation($Color.$type),
178+
params: [
179+
BridgeParameter(
180+
'g',
181+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)),
182+
false)
183+
]),
184+
),
185+
'withRed': BridgeMethodDef(
186+
BridgeFunctionDef(
187+
returns: BridgeTypeAnnotation($Color.$type),
188+
params: [
189+
BridgeParameter(
190+
'r',
191+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)),
192+
false)
193+
]),
194+
),
195+
'withBlue': BridgeMethodDef(
196+
BridgeFunctionDef(
197+
returns: BridgeTypeAnnotation($Color.$type),
198+
params: [
199+
BridgeParameter(
200+
'b',
201+
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)),
202+
false)
203+
]),
204+
),
205+
'computeLuminance': BridgeMethodDef(BridgeFunctionDef(
206+
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.double)))),
207+
},
147208
wrap: true);
148209

149210
/// Wrap a [Color] in an [$Color]
@@ -158,6 +219,57 @@ class $Color implements Color, $Instance {
158219

159220
@override
160221
$Value? $getProperty(Runtime runtime, String identifier) {
222+
switch (identifier) {
223+
case 'a':
224+
return $double($value.a);
225+
case 'r':
226+
return $double($value.r);
227+
case 'g':
228+
return $double($value.g);
229+
case 'b':
230+
return $double($value.b);
231+
case 'colorSpace':
232+
return $ColorSpace.wrap($value.colorSpace);
233+
case 'toARGB32':
234+
return $Function((runtime, target, args) {
235+
return $int((target!.$value as Color).toARGB32());
236+
});
237+
case 'withValues':
238+
return $Function((runtime, target, args) {
239+
final result = (target!.$value as Color).withValues(
240+
alpha: args[0]?.$value,
241+
red: args[1]?.$value,
242+
green: args[2]?.$value,
243+
blue: args[3]?.$value,
244+
colorSpace: args[4]?.$value,
245+
);
246+
return $Color.wrap(result);
247+
});
248+
case 'withAlpha':
249+
return $Function((runtime, target, args) {
250+
final result = (target!.$value as Color).withAlpha(args[0]!.$value);
251+
return $Color.wrap(result);
252+
});
253+
case 'withRed':
254+
return $Function((runtime, target, args) {
255+
final result = (target!.$value as Color).withRed(args[0]!.$value);
256+
return $Color.wrap(result);
257+
});
258+
case 'withGreen':
259+
return $Function((runtime, target, args) {
260+
final result = (target!.$value as Color).withGreen(args[0]!.$value);
261+
return $Color.wrap(result);
262+
});
263+
case 'withBlue':
264+
return $Function((runtime, target, args) {
265+
final result = (target!.$value as Color).withBlue(args[0]!.$value);
266+
return $Color.wrap(result);
267+
});
268+
case 'computeLuminance':
269+
return $Function((runtime, target, args) {
270+
return $double((target!.$value as Color).computeLuminance());
271+
});
272+
}
161273
throw UnimplementedError();
162274
}
163275

test/flutter_eval_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void main() {
204204
BoxDecoration main() {
205205
return BoxDecoration(
206206
border: Border.all(
207-
color: Colors.red,
207+
color: Colors.red.withValues(alpha: 0.8),
208208
width: 2.0,
209209
),
210210
);
@@ -218,8 +218,9 @@ void main() {
218218
expect(result, isNotNull);
219219
expect(result.$value, isA<BoxDecoration>());
220220
expect((result.$value as BoxDecoration).border, isA<Border>());
221-
expect(
222-
(result.$value as BoxDecoration).border!.top.color, equals(Colors.red));
221+
// Comparing colors directly fails for some reason.
222+
expect((result.$value as BoxDecoration).border!.top.color.toARGB32(),
223+
equals(Colors.red.withValues(alpha: 0.8).toARGB32()));
223224
expect((result.$value as BoxDecoration).border!.top.width, equals(2.0));
224225
});
225226

0 commit comments

Comments
 (0)