Skip to content

Commit

Permalink
refactor!: Replace Offset with opacityFrom and opacityTo in Col…
Browse files Browse the repository at this point in the history
…orEffect (#2876)

`ColorEffect`'s API was a bit confusing because it used `Offset` for
getting values of opacity start and end from user. This PR changes that
to use optional `opacityFrom` and `opacityTo` double parameters. It also
adds validate checks on these value to make sure that are between 0 and
1.
  • Loading branch information
ufrshubham committed Nov 27, 2023
1 parent f4ff311 commit 0fd2662
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
7 changes: 4 additions & 3 deletions doc/flame/effects.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,14 @@ Usage example:
```dart
final effect = ColorEffect(
const Color(0xFF00FF00),
const Offset(0.0, 0.8),
EffectController(duration: 1.5),
opacityFrom = 0.2,
opacityTo: 0.8,
);
```

The `Offset` argument will determine "how much" of the color that will be applied to the component,
in this example the effect will start with 0% and will go up to 80%.
The `opacityFrom` and `opacityTo` arguments will determine "how much" of the color that will be
applied to the component. In this example the effect will start with 20% and will go up to 80%.

**Note:** Due to how this effect is implemented, and how Flutter's `ColorFilter` class works, this
effect can't be mixed with other `ColorEffect`s, when more than one is added to the component, only
Expand Down
4 changes: 2 additions & 2 deletions doc/flame/examples/lib/color_effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ class ColorEffectExample extends FlameGame {
ember.add(
ColorEffect(
const Color(0xFF00FF00),
const Offset(0.0, 0.6),
EffectController(duration: 1.5),
opacityTo: 0.6,
),
);
} else {
ember.add(
ColorEffect(
const Color(0xFF1039DB),
const Offset(0.0, 0.6),
EffectController(duration: 1.5),
opacityTo: 0.6,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ class CollidableEmber extends Ember with CollisionCallbacks {
add(
ColorEffect(
index < 2 ? Colors.red : Colors.green,
const Offset(0, 0.9),
EffectController(
duration: 0.2,
alternate: true,
),
opacityTo: 0.9,
),
);
}
Expand Down
6 changes: 2 additions & 4 deletions examples/lib/stories/effects/color_effect_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ class ColorEffectExample extends FlameGame with TapDetector {
)..add(
ColorEffect(
Colors.blue,
const Offset(
0.0,
0.8,
), // Means, applies from 0% to 80% of the color
EffectController(
duration: 1.5,
reverseDuration: 1.5,
infinite: true,
),
// Means, applies from 0% to 80% of the color
opacityTo: 0.8,
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class DualEffectRemovalExample extends FlameGame with TapDetector {
);
colorEffect = ColorEffect(
Colors.blue,
const Offset(0.0, 0.8),
colorController,
opacityTo: 0.8,
);
mySprite.add(colorEffect);

Expand Down
12 changes: 10 additions & 2 deletions packages/flame/lib/src/effects/color_effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ class ColorEffect extends ComponentEffect<HasPaint> {

ColorEffect(
this.color,
Offset offset,
EffectController controller, {
double opacityFrom = 0,
double opacityTo = 1,
this.paintId,
void Function()? onComplete,
}) : _tween = Tween(begin: offset.dx, end: offset.dy),
}) : assert(
opacityFrom >= 0 &&
opacityFrom <= 1 &&
opacityTo >= 0 &&
opacityTo <= 1,
'Opacity value should be between 0 and 1',
),
_tween = Tween(begin: opacityFrom, end: opacityTo),
super(controller, onComplete: onComplete);

@override
Expand Down
52 changes: 49 additions & 3 deletions packages/flame/test/effects/color_effect_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
await game.ensureAdd(component);
const color = Colors.red;
await component.add(
ColorEffect(color, const Offset(0, 1), EffectController(duration: 1)),
ColorEffect(color, EffectController(duration: 1)),
);
game.update(0);
expect(
Expand Down Expand Up @@ -43,7 +43,6 @@ void main() {

final effect = ColorEffect(
color,
const Offset(0, 1),
EffectController(duration: 1),
);
await component.add(effect);
Expand Down Expand Up @@ -71,7 +70,6 @@ void main() {

final effect = ColorEffect(
Colors.red,
const Offset(0, 1),
EffectController(duration: 1),
);
await component.ensureAdd(effect);
Expand All @@ -85,5 +83,53 @@ void main() {
);
},
);

test('Validates opacity values', () {
expect(
() => ColorEffect(
Colors.blue,
EffectController(duration: 1),
opacityTo: 1.1,
),
throwsAssertionError,
);

expect(
() => ColorEffect(
Colors.blue,
EffectController(duration: 1),
opacityFrom: 255,
),
throwsAssertionError,
);

expect(
() => ColorEffect(
Colors.blue,
EffectController(duration: 1),
opacityTo: -254,
),
throwsAssertionError,
);

expect(
() => ColorEffect(
Colors.blue,
EffectController(duration: 1),
opacityFrom: -0.5,
),
throwsAssertionError,
);

expect(
() => ColorEffect(
Colors.blue,
EffectController(duration: 1),
opacityFrom: 0.1,
opacityTo: 0.9,
),
returnsNormally,
);
});
});
}

0 comments on commit 0fd2662

Please sign in to comment.