diff --git a/doc/flame/rendering/palette.md b/doc/flame/rendering/palette.md index d1e87eca028..80dc7ab8489 100644 --- a/doc/flame/rendering/palette.md +++ b/doc/flame/rendering/palette.md @@ -86,3 +86,6 @@ members: - `paint`: creates a new `Paint` with the color specified. `Paint` is a non-`const` class, so this method actually creates a brand new instance every time it's called. It's safe to cascade mutations to this. + +It also exposes helper methods to derive a new `PaletteEntry` by transforming the underlying color, +such as `withRed` or `lighten`. diff --git a/packages/flame/lib/src/palette.dart b/packages/flame/lib/src/palette.dart index 1bd50214680..7610b112957 100644 --- a/packages/flame/lib/src/palette.dart +++ b/packages/flame/lib/src/palette.dart @@ -1,5 +1,7 @@ import 'dart:ui'; +import 'package:flame/extensions.dart'; + class PaletteEntry { final Color color; @@ -22,6 +24,14 @@ class PaletteEntry { PaletteEntry withBlue(int blue) { return PaletteEntry(color.withBlue(blue)); } + + PaletteEntry darken(double amount) { + return PaletteEntry(color.darken(amount)); + } + + PaletteEntry brighten(double amount) { + return PaletteEntry(color.brighten(amount)); + } } class BasicPalette { diff --git a/packages/flame/test/palette_test.dart b/packages/flame/test/palette_test.dart new file mode 100644 index 00000000000..fa457f40372 --- /dev/null +++ b/packages/flame/test/palette_test.dart @@ -0,0 +1,22 @@ +import 'package:flame/palette.dart'; +import 'package:test/test.dart'; + +void main() { + group('Palette', () { + test('updates color', () { + const entry = BasicPalette.red; + expect(entry.color, const Color(0xFFFF0000)); + expect(entry.withAlpha(100).color, const Color(0x64FF0000)); + expect(entry.withRed(100).color, const Color(0xFF640000)); + expect(entry.withGreen(100).color, const Color(0xFFFF6400)); + expect(entry.withBlue(100).color, const Color(0xFFFF0064)); + }); + + test('darkens and brightens color', () { + const entry = BasicPalette.red; + expect(entry.color, const Color(0xFFFF0000)); + expect(entry.darken(0.5).color, const Color(0xFF800000)); + expect(entry.brighten(0.5).color, const Color(0xFFFF8080)); + }); + }); +}