Skip to content

Commit 0a6d835

Browse files
committed
feat: Expose brighten and darken helpers on palette
1 parent bae5838 commit 0a6d835

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

doc/flame/rendering/palette.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ members:
8686
- `paint`: creates a new `Paint` with the color specified. `Paint` is a non-`const` class, so this
8787
method actually creates a brand new instance every time it's called. It's safe to cascade
8888
mutations to this.
89+
90+
It also exposes helper methods to derive a new `PaletteEntry` by transforming the underlying color,
91+
such as `withRed` or `lighten`.

packages/flame/lib/src/palette.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:ui';
22

3+
import 'package:flame/extensions.dart';
4+
35
class PaletteEntry {
46
final Color color;
57

@@ -22,6 +24,14 @@ class PaletteEntry {
2224
PaletteEntry withBlue(int blue) {
2325
return PaletteEntry(color.withBlue(blue));
2426
}
27+
28+
PaletteEntry darken(double amount) {
29+
return PaletteEntry(color.darken(amount));
30+
}
31+
32+
PaletteEntry brighten(double amount) {
33+
return PaletteEntry(color.brighten(amount));
34+
}
2535
}
2636

2737
class BasicPalette {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:flame/palette.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group('Palette', () {
6+
test('updates color', () {
7+
const entry = BasicPalette.red;
8+
expect(entry.color, const Color(0xFFFF0000));
9+
expect(entry.withAlpha(100).color, const Color(0x64FF0000));
10+
expect(entry.withRed(100).color, const Color(0xFF640000));
11+
expect(entry.withGreen(100).color, const Color(0xFFFF6400));
12+
expect(entry.withBlue(100).color, const Color(0xFFFF0064));
13+
});
14+
15+
test('darkens and brightens color', () {
16+
const entry = BasicPalette.red;
17+
expect(entry.color, const Color(0xFFFF0000));
18+
expect(entry.darken(0.5).color, const Color(0xFF800000));
19+
expect(entry.brighten(0.5).color, const Color(0xFFFF8080));
20+
});
21+
});
22+
}

0 commit comments

Comments
 (0)